【数据库-存储过程】
1、.什么是存储过程
存储过程是完成特定功能的sql语句集合。通过编译后存储在数据
库中,通过指定的存储过程名称调用执行它。
存储过程=sql语句集合+控制语句
2、使用存储过程的优点
1)存储过程创建可以多次调用,不需要重新编写存储过程语句。
2)假如要往表中插入大量的测试数据,用insert into语法执行速
度太慢,效率较低,那么这个时候可以通过编写存储过程,把需要
插入的数据sql集合放入存储过程的函数体当中,创建存储过程,
下次即可调用。
3)存储过程加快程序的运行速度
4)存储过程增加sql语句的功能和灵活性、创建的存储过程可以重复使用。
3、存储过程的格式
第一步:
create procedure 存储过程名称(参数 数据类型)
begin 存储过程的开始
存储过程体(sql语句集合)
end 存储过程的结束
call 存储过程名(); 调用存储过程
第二步 删除一个存储过程
drop procedure 存储过程名;
drop procedure if exists +存储名称; 存储过程存在现删除 ==》增强代码的健壮性
第三步 drop table if exists +表; 表存在就删除 ==》增强存储的健壮性
第四 步 不用参数查询数据
例子1 不用参数
drop procedure if exists dcs; #存储过程存在现删除 ==》增强代码的健壮性
create procedure dcs() #创建一个dcs存储过程
begin #存储过程的开始
#编写sql语句集合
drop table if exists nn; #如果nn表存在现删除 ==》增强存储的健壮性
create table nn(id int(10) primary key auto_increment,score int(10));
insert into nn values (1,88),(2,99),(3,66),(4,89);
select * from nn;
desc nn;
end #存储过程的结束
call dcs(); #调用存储过程
第五步:在定义存储的时候带参数,用参数查询
基本格式:create procedure 存储过程名字(n INT)
drop procedure if exists dcs; #存储过程存在现删除 ==》增强代码的健壮性
create procedure dcs(n int) #创建一个dcs存储过程 n表示形式参数 数据类型是整型
begin #存储过程的开始
#编写sql语句集合
drop table if exists nn; #如果nn表存在现删除 ==》增强存储的健壮性
create table nn(id int(10) primary key auto_increment,score int(10));
insert into nn values (1,88),(2,99),(3,66),(4,89);
select * from nn where id = n;
desc nn;
end #存储过程的结束
call dcs(2); #调用存储过程 上面定义了参数这里一定要入实际参数
第六步:往存储中加入if语句,判断语句
if语句的格式: (有多少的if 就需要写多少个end if)
1、单分支
if 条件 THEN
执行sql
else
执行sql
end if
drop procedure if exists dcs; #存储过程存在现删除 ==》增强代码的健壮性
create procedure dcs(n int) #创建一个dcs存储过程 n表示形式参数 数据类型是整型
begin #存储过程的开始
#编写sql语句集合
drop table if exists nn; #如果nn表存在现删除 ==》增强存储的健壮性
create table nn(id int(10) primary key auto_increment,score int(10));
insert into nn values (1,88),(2,99),(3,66),(4,89);
if n = 0 then #当n 为0 时 查询全表数据
select * from nn ;
else #除了0之外就是查询表结构
desc nn;
end if;
end #存储过程的结束
call dcs(2); #调用存储过程 上面定义了参数这里一定要入实际参数
2.多分支 (有多少的if 就需要写多少个end if)
if 条件 THEN
执行sql
else if 条件 THEN
执行sql
else if 条件 THEN
执行sql
else
执行sql
end if;
end if;
end if;
drop procedure if exists dcs; #存储过程存在现删除 ==》增强代码的健壮性
create procedure dcs(n int) #创建一个dcs存储过程 n表示形式参数 数据类型是整型
begin #存储过程的开始
#编写sql语句集合
drop table if exists nn; #如果nn表存在现删除 ==》增强存储的健壮性
create table nn(id int(10) primary key auto_increment,score int(10));
insert into nn values (1,88),(2,99),(3,66),(4,89);
if n = 0 then #当n 为0 时 查询全表数据
select * from nn ;
else if n >0 and n <=4 THEN # 当n 大于0 且n 小于等于4 执行排序
select * from nn order by score desc;
else if n >4 THEN #当n 大于4 执行统计
select count(*) from nn;
else #除了以上条件之外就是查询表结构
desc nn;
end if;
end if;
end if;
end #存储过程的结束
call dcs(-1); #调用存储过程 上面定义了参数这里一定要入实际参数
第七步:DECLARE i int DEFAULT语句可以自动检测统计数据行数
drop procedure if exists dcs; #存储过程存在现删除 ==》增强代码的健壮性
create procedure dcs(n int) #创建一个dcs存储过程 n表示形式参数 数据类型是整型
begin #存储过程的开始
declare i int default (select count(*) from nn); #定义一个变量i 默认值是表的统计数(i = 4)
#编写sql语句集合
drop table if exists nn; #如果nn表存在现删除 ==》增强存储的健壮性
create table nn(id int(10) primary key auto_increment,score int(10));
insert into nn values (1,88),(2,99),(3,66),(4,89);
if n = 0 then #当n 为0 时 查询全表数据
select * from nn ;
else if n >0 and n <= i THEN # 当n 大于0 且n 小于等于4 执行排序
select * from nn order by score desc;
else if n >i THEN #当n 大于4 执行统计
select count(*) from nn;
else #除了以上条件之外就是查询表结构
desc nn;
end if;
end if;
end if;
end #存储过程的结束
call dcs(-1); #调用存储过程 上面定义了参数这里一定要入实际参数
第八步:往存储过程中加while语句
如果传入得参数大于表中实际总记录的数量,则往表中补齐
往存储过程中加while语句(循环语句)
while 语句的格式:
while 条件 do
执行循环体(sql)
end while;
注意:
什么时候进入循环:当条件成立时,进入循环
什么时候退出循环:当条件不成立时,退出循环
drop procedure if exists dcs; #存储过程存在现删除 ==》增强代码的健壮性
create procedure dcs(n int) #创建一个dcs存储过程 n表示形式参数 数据类型是整型
begin #存储过程的开始
declare i int default (select count(*) from nn); #定义一个变量i 默认值是表的统计数(i = 4)
#编写sql语句集合
/*drop table if exists nn; #如果nn表存在现删除 ==》增强存储的健壮性
create table nn(id int(10) primary key auto_increment,score int(10));
insert into nn values (1,88),(2,99),(3,66),(4,89); */
if n = 0 then #当n 为0 时 查询全表数据
select * from nn ;
else if n >0 and n <= i THEN # 当n 大于0 且n 小于等于4 执行排序
select * from nn order by score desc;
else #除了以上条件之外就是查询表结构
while n> i DO
insert into nn (score)values(66);
set i = i+1; #每插入一条数据,表中行数加一
end while;
select * from nn;
end if;
end if;
end #存储过程的结束
call dcs(1000); #调用存储过程 上面定义了参数这里一定要入实际参
【面试题】你之前写过存储过程?什么场景下写的?怎么写的,可以讲下吗?
场景:我要造1000个用户进行新增,通过循环过程循环导入用户添加
首先创建一个存储,名字起一个test,create procedure+存储过程名称,
接下来是begin,end是表的基本结构,中间接的叫整个sql语句块,也就是代码,
接下来我要设置一个while循环,那么前面我就要设置一个变量,declare i int(20)deafult 0,while循环中,
while n>i do,这里要注意do后面不能加分号,因为我两年前写存储过程的时候加了,
会报错,现在基本上不会犯了,下一步 set i=i+1;这个表示循环变量的自增模式,接下来
insert into+表格名称,end while结束循环,我创建这个存储过程就要执行,调用的时候用 call test(),
会报错,显示我这个存储已存在,因为同一个项目组里面,其他同事也会写存储过程,所以这个时候我会
先检查一下这个存储是否已存在,用 drop proceduer if exists 来检查,大概我的存储过程就这几种情况。
|
|