当前位置:网站首页>存储过程的介绍与基本使用
存储过程的介绍与基本使用
2022-07-07 11:19:00 【汤键.TJ】
目录
介绍
- 存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句的集合
好处:
- 提高代码的复用性
- 减少数据在数据库和应用服务器之间的传输,提高效率
- (对于数据库来说,后期要定义在一台或多台服务器上,后面再传输的话肯定会影响效率)
- 减少代码层面的业务处理
存储过程和函数的区别(几乎没有区别)
- 存储函数必须有返回值
- 存储过程可以没有返回值
创建和调用存储过程
创建存储过程
- -- 修改结束分隔符
- delimiter $
- -- 创建存储过程
- create procedure 存储过程名称(参数列表)
- begin
- SQL语句列表
- end$
- -- 修改结束分隔符
- delimiter ;
调用存储过程
- call 存储过程名称(实际参数)
实例演示
数据准备
-- 创建学生表 CREATE TABLE student( id INT PRIMARY KEY auto_increment, -- 学生id name VARCHAR(20), -- 学生姓名 age INT, -- 学生年龄 gender VARCHAR(5), -- 学生性别 score INT -- 学生成绩 ); -- 添加数据 INSERT INTO student VALUES (NULL,'张三',23,'男',95),(NULL,'李四',24,'男',98),(NULL,'王五',25,'女',100),(NULL,'赵六',26,'女',90);
创建和调用存储过程
-- 创建stu_group()存储过程,封装 分组查询总成绩,并按照总成绩升序排序的功能 delimiter $ CREATE PROCEDURE stu_group() BEGIN SELECT gender,SUM(score) getsum FROM student GROUP BY gender ORDER BY getsum ASC; END$ delimiter ;
-- 调用stu_group()存储过程 CALL stu_group;
存储过程的查看和删除
查看数据库中所有的存储过程
- select * from mysql.proc where db='数据库名称'
删除存储过程
- drop procedure [if exists] 存储过程名称
实例操作
-- 查看dp1数据库中所有的存储过程 SELECT * FROM mysql.proc WHERE db='dp1';
-- 删除存储过程 DROP PROCEDURE IF EXISTS stu_group;
边栏推荐
- ESP32构解工程添加组件
- JS determines whether an object is empty
- Common text processing tools
- Why can basic data types call methods in JS
- [untitled]
- Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images
- 测试下摘要
- 飞桨EasyDL实操范例:工业零件划痕自动识别
- 关于 appium 启动 app 后闪退的问题 - (已解决)
- Milkdown 控件图标
猜你喜欢
将数学公式在el-table里面展示出来
- Oui. Migration entièrement automatisée de la Sous - base de données des tableaux d'effets sous net
Sed of three swordsmen in text processing
Go language learning notes - structure
【Presto Profile系列】Timeline使用
【无标题】
ESP32构解工程添加组件
leecode3. 无重复字符的最长子串
Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion
About how appium closes apps (resolved)
随机推荐
《ASP.NET Core 6框架揭秘》样章[200页/5章]
API query interface for free mobile phone number ownership
TPG x AIDU|AI领军人才招募计划进行中!
详细介绍六种开源协议(程序员须知)
HZOJ #236. Recursive implementation of combinatorial enumeration
[learning notes] agc010
抓细抓实抓好安全生产各项工作 全力确保人民群众生命财产安全
RealBasicVSR测试图片、视频
What are the benefits of ip2long?
ClickHouse(03)ClickHouse怎么安装和部署
JNA学习笔记一:概念
[untitled]
ESP32构解工程添加组件
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
单片机原理期末复习笔记
自定义线程池拒绝策略
国泰君安证券开户怎么开的?开户安全吗?
How to continue after handling chain interruption / sub chain error removed from scheduling
博文推荐|Apache Pulsar 跨地域复制方案选型实践