找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

1、什么是存储过程 存储过程是完成特定功能的sql语句集合。通过编译后存储在数据库中,
通过指定的存储过程名称调用执行它。 存储过程=sql语句集合+控制语句
2、使用存储过程的优点
1)存储过程创建可以多次调用,不需要重新编写存储过程语句。
2)假如要往表中插入大量的测试数据,用insert into语法执行速 度太慢,效率较低,
那么这个时候可以通过编写存储过程,把需要 插入的数据sql集合放入存储过程的函数体当中,
创建存储过程, 下次即可调用
3)存储过程加快程序的运行速度
4)存储过程增加sql语句的功能和灵活性、创建的存储过程可以重复使用。
3、第一步 创建一个存储过程
create procedure 存储过程名(参数 数据类型)
BEGIN
存储过程体(sql语句集合)
END
#begin....end   ==》代表的是存储过程的开始和结束
call 存储过程名();    ==》调用存储过程
4、第二步  删除一个存储过程
drop procedure 存储过程名
DROP procedure if EXISTS 存储过程名;  ==》加强代码的健壮性
5、第三步
drop table if exists 表名    ==》增强存储的健壮性
6、 第四步 不用参数查询语句
例子1:不带参数
drop procedure if exists dcs88;     #如果存在dcs88这个存储过程先删除,加强代码的健壮性
create procedure dcs88()            #创建一个存储过程dcs88
begin     #存储过程的开始
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,89),(4,98); select * from nn; desc nn;
end
call dcs88();    #调用存储过程dcs88
7、在定义存储的时候带参数,用参数查询
drop procedure if exists dcs88;        #如果存在dcs88这个存储过程先删除,加强代码的健壮性
create procedure dcs88(t int)            #创建一个存储过程dcs88 t表示形式参数 int整型
begin     #存储过程的开始
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,89),(4,98); select * from nn; desc nn;
end
call dcs88(10);     #调用时需要参数  这个地方需要入一个实际参数输入一个整型
8、往存储中加入if语句,判断语句 if语句的格式: (有多少的if 就需要写多少个end if)
1)单分支
if 条件 THEN
执行sql
else
执行sql
end if;
例子1:
drop procedure if exists dcs88;        #如果存在dcs88这个存储过程先删除,加强代码的健壮性
create procedure dcs88(t int)            #创建一个存储过程dcs88 t表示形式参数 int整型
begin     #存储过程的开始
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,89),(4,98); if t = 0 then        #当t = 0 时执行全表查询    select * from nn; else                #当t!=0 执行查询表结构    desc nn; end if;
end
call dcs88(2);
2)if 多分支
if 条件 THEN
执行sql
else if 条件 THEN
执行sql
else if 条件 THEN
执行sql .............
else
执行sql
end if;
end if;
end if;
例子:
drop procedure if exists dcs88;        #如果存在dcs88这个存储过程先删除,加强代码的健壮性
create procedure dcs88(t int)            #创建一个存储过程dcs88 t表示形式参数 int整型
begin     #存储过程的开始
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,89),(4,98); if t = 0 then        #当t = 0 时执行全表查询    select * from nn; else if t>0 and t<=8 THEN   #当t大于0小于等于8执行查询第一条数据    select * from nn where id =1; else if t > 8 THEN          #当t大于8时执行查询以分数降序    select * from nn order by score desc; else                        #当负整数 执行查询表结构    desc nn; end if; end if; end if;
end
call dcs88(9);
9、DECLARE i int DEFAULT语句可以自动检测统计数据行数
例子
drop procedure if exists dcs88;        #如果存在dcs88这个存储过程先删除,加强代码的健壮性
create procedure dcs88(t int)            #创建一个存储过程dcs88 t表示形式参数 int整型
begin     #存储过程的开始
declare i int default(select count(*) from nn);   #定义一个变量i 默认值是表的统计数为4 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,89),(4,98); if t = 0 then        #当t = 0 时执行全表查询    select * from nn; else if t>0 and t<=i THEN   #当t大于0小于等于i执行查询第一条数据    select * from nn where id =1; else if t > i THEN          #当t大于i时执行查询以分数降序    select * from nn order by score desc; else                        #当负整数 执行查询表结构    desc nn; end if; end if; end if;
end
call dcs88(5);
10、往存储过程中加while语句(循环语句)
while 语句的格式:
while 条件 do
执行循环体(sql)
end while;
注意: 什么时候进入循环:当条件成立时,进入循环
什么时候退出循环:当条件不成立时,退出循环
往存储过程中加while语句 如果传入得参数大于表中实际总记录的数量,则往表中补齐
drop procedure if exists dcs88;        #如果存在dcs88这个存储过程先删除,加强代码的健壮性
create procedure dcs88(t int)            #创建一个存储过程dcs88 t表示形式参数 int整型
begin     #存储过程的开始
declare i int default(select count(*) from nn);   #定义一个变量i 默认值是表的统计数为4  /*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,89),(4,98); */ if t = 0 then        #当t = 0 时执行全表查询    select * from nn; else if t>0 and t<=i THEN   #当t大于0小于等于i执行查询第一条数据    select count(*) from nn; ELSE     while t > i DO        #当输入的t的数字大于i=4 时进入到while循环语句           insert into nn(score) VALUES(88);           set i = i+1;       #每循环一次,数据库表中加一行 补齐                          end while;       end if; end if;     select * from nn;
end
call dcs88(10);
11、补充的数据在最小或者最大的成绩上逐次加1分
drop procedure if exists dcs88;        #如果存在dcs88这个存储过程先删除,加强代码的健壮性
create procedure dcs88(t int)            #创建一个存储过程dcs88 t表示形式参数 int整型
begin     #存储过程的开始
declare i int default(select count(*) from nn);   #定义一个变量i 默认值是表的统计数为4 declare j int default(select max(score) from nn);  #定义了一个最大值的变量j 初始时99 /*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,89),(4,98); */ if t = 0 then        #当t = 0 时执行全表查询    select * from nn; else if t>0 and t<=i THEN   #当t大于0小于等于i执行查询第一条数据    select count(*) from nn; ELSE     while t > i DO        #当输入的t的数字大于i=4 时进入到while循环语句           set j = j+1;    #每循环一个,最大值分数加1           insert into nn(score) VALUES(j);           set i = i+1;       #每循环一次,数据库表中加一行 补齐                          end while;       end if; end if;     select * from nn;
end
call dcs88(10);
存储过程题目:现在有一个user用户表,需要往user表中插入1000个登录 用户要求如下:
1、在插入用户前先判断是否存在1000个登录用户,如果存在则统计表中实 际的行数、如若不存在则自动补齐剩余的数据
2、表名为user,存储过程名字随意取,表中字段有id user_name user_pwd verify 格式如下(1,user1,123456,W4E38J),
且id、用户名不能重复,verify 验证码字段为随机生成6位数验证码 CONCAT函数 可以把2个字符串进行连接。
drop procedure if exists dcs66;
create procedure dcs66(t int)
begin
  declare i int default(select count(*) from mm);  declare user_name varchar(10)default '';  declare verify  varchar (10)default '';  /*drop table if exists mm;  create table mm(id int(10) PRIMARY key auto_increment,user_name varchar(10),user_pwd int(10) default 123456  ,verify varchar(10));  insert into mm(id,user_name,user_pwd,verify) VALUES(1,'user1',123456,'W4E38J'); */  if t < i THEN     select * from mm;  else       while t > i DO         set i = i+1;         set user_name = (select CONCAT('user',i));         set verify = (select SUBSTRING(md5(rand()),1,6));         insert into mm(user_name,verify) VALUES(user_name,verify);               #每循环一次,数据库表中加一行 补齐           end while;  end if;  select * from mm;
end
call dcs66(1000);
select CONCAT('user',3);
select SUBSTRING(md5(rand()),1,6)

【面试题】你之前写过存储过程?什么场景下写的?怎么写的,可以讲下吗?
场景:我要造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 来检查,大概我的存储过程就这几种情况。

分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册