当前位置:网站首页>存储过程的介绍与基本使用
存储过程的介绍与基本使用
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;
边栏推荐
猜你喜欢

MongoDB内部的存储原理

ISPRS2021/遥感影像云检测:一种地理信息驱动的方法和一种新的大规模遥感云/雪检测数据集

日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则

Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules

Adopt a cow to sprint A shares: it plans to raise 1.85 billion yuan, and Xu Xiaobo holds nearly 40%

COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~

JS缓动动画原理教学(超细节)

Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set

单片机原理期末复习笔记

飞桨EasyDL实操范例:工业零件划痕自动识别
随机推荐
Cinnamon Applet 入门
[untitled]
JS function 返回多个值
Unity build error: the name "editorutility" does not exist in the current context
聊聊伪共享
ESP32系列专栏
MySQL入门尝鲜
MongoDB优化的几点原则
RealBasicVSR测试图片、视频
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
[Presto profile series] timeline use
What if the xshell evaluation period has expired
关于 appium 启动 app 后闪退的问题 - (已解决)
Pcap learning notes II: pcap4j source code Notes
PHP calls the pure IP database to return the specific address
Lingyunguang of Dachen and Xiaomi investment is listed: the market value is 15.3 billion, and the machine is implanted into the eyes and brain
[learning notes] segment tree selection
【Presto Profile系列】Timeline使用
RecyclerView的数据刷新
Practical case: using MYCAT to realize read-write separation of MySQL





