dcsxlh

导航

 

存储过程

====================

1、什么存储过程?

存储过程就是实现某个特定功能的sql语句的集合,编译后的存储过程会保存在数据库中,通过存储过程的名称可以反复的调用执行。

 

2、存储过程的优点?

(1)存储创建后,可以反复的调用和使用,不需要重新写复杂的sql语句

(2)创建,修改存储过程不会对数据有任何的影响zhi

(3)存储过程可以通过输入参数返回输入值

(4)通过存储过程中加入控制语句,可以加强sql语句的功能和灵活性

(5)防止sql注入

(6)造数据(重点)

3、存储引擎版本?

在mysql5.0之后支持存储过程

=======================================

实战:

(1)

delimiter // #分隔符  
create (创建)    procedure (存储)  hz1()   存储名      #创建一个存储,
BEGIN #开始
       select * from emp ; #SQL 存储体
       SELECT * from dept;
end #结束
// #分隔符

call             hz15()     调用存储  存储名

=======================================

 1、查看单个存储过程的详情

格式:show create procedure  存储名称

案例:show create procedure hz1

 

 

 2、查看所有已经建好的存储过程

格式:show procedure status ;
案例:show procedure status ;

3、查询数据库中创建了哪些存存过程

格式:show procedure status where db="库名"

案例:show procedure status where db="abc"

4、删除一个存储过程

格式:drop procedure      存储名称

案例:drop  procedure   hz2;

 

=======================================

(1)无参数的存储过程

案例:

delimiter // #分隔符
create procedure wcs() #创建一个存储,
BEGIN #开始
select * from emp ; #SQL 存储体
end #结束
// #分隔符

call wcs()

多行注释:ctrl+/      取消多行注释  shift+ctrl+/

 

----------------------------------------------------------

(2)in  输入参数

delimiter // #分隔符
create procedure cs_in(in x int(10)) #创建一个存储,       in是输入参数要接字符类型

BEGIN #开始
select * from emp where deptno=x; #SQL 存储体
end #结束
// #分隔符

call cs_in(30)

----------------------------------------------------------

(3)out 输出参数

out  字符类型(字符长度)

delimiter // #分隔符
create procedure cs_out(out y char(10)) #创建一个存储,
BEGIN #开始
select ename into y from emp where empno=7521; #SQL 存储体
end #结束
// #分隔符

call cs_out(@y) #
select @y

----------------------------------------------------------

in    输入    out输出

select *from emp ;
delimiter // #分隔符
create procedure cs_in_out(in x int(10),out y char(10)) #创建一个存储,
BEGIN #开始
select ename into y from emp where empno=x; #SQL 存储体
end #结束
// #分隔符

call cs_in_out(7521,@y) #
select @y

----------------------------------------------------------

inout  输入输出   是同一个变量

delimiter  //  #分隔符

create   procedure  cs_inout(inout n int(10))   #创建一个存储, 

BEGIN  #开始

   set n:=n+n;

end   #结束

//  #分隔符

 

set @n=2

call cs_inout(@n)

select @n

 

----------------------------------------------------------

用户变量:定义语法:

(1)set  @ 变量名 :=值  或  set  @变量名=值;  # 设置变量

         select  @变量名:=值      #查询所有的变量

(2)   通过查询结果为变量赋值

 select   字段名 |表达式    into   变量    from   表名    where   条件
 select  @变量名:=值      #查询所有的变量

 

---------------------------------------------------------

while      条件  do

  执行语句

end  while

msyql中循环语句的三种格式:

WHILE……DO……END WHILE   (讲)
REPEAT……UNTIL END REPEAT
LOOP……END LOOP

---------------------------------------------------------

声明变量   :

案例1:DECLARE i int DEFAULT 0 ;

案例2:DECLARE i   int DEFAULT (select count(sid)from  emp ) ;

----------------------------------------------------------

造数:

(1) 造的数据是指定在存储中

delimiter //
drop procedure if exists zs; #加强健壮性 ,判断是否存在这个存储
drop table if EXISTS ss;
CREATE procedure zs()
BEGIN
DECLARE i int DEFAULT 0 ; #变量名称i 字符类型int 默认值0
create table ss(id int(10) PRIMARY key , incoming int(10));
while (i<100) DO
INSERT into ss VALUES (i,i+1);
set i=i+1;
end WHILE;
select * from ss;
END
//

call zs()

(2)插入的数据可以灵活指定具体的数据

delimiter //
drop procedure if exists zs; #加强健壮性 ,判断是否存在这个存储
CREATE procedure zs(in x int(10))
BEGIN
DECLARE i int DEFAULT 0 ; #变量名称i 字符类型int 默认值0
drop table if EXISTS ss;
create table ss(id int(10) PRIMARY key , incoming int(10));
while (i<x) DO
INSERT into ss VALUES (i,i+1);
set i=i+1;
end WHILE;
select * from ss;
END
//

call zs(50)

面试题:

1、你会存储吗?

2、存储的结构?

3、存储你用干什么? 造数

4、具体怎么去造数

----------------------------------------------------------

if 语句 

(1)if  单分支

if   条件   then

    执行sql1

else

  执行sql2

end if

案例:

delimiter //

drop  procedure  if exists  if_fz;  #加强健壮性 ,判断是否存在这个存储

CREATE  procedure  if_fz(in  x int(10))

BEGIN

   if   x>10  THEN

         select  * from  dept;

    ELSE

         select *  from emp;

    end if;

END

//

call  if_fz(1)

注意点:一个if    对应一个end  if 

----------------------------------------------------------

if多分支

if    条件1   then

    sql语句1

else  if  条件2  then

    sql语句2

else   if  条件3  then

 sql语句3

else

  执行语句4

end if;

end  if;

end  if;

案例:

delimiter //

drop  procedure  if exists  if_fz;  #加强健壮性 ,判断是否存在这个存储

CREATE  procedure  if_fz(in  x int(10))

BEGIN

   if   x>10 and x<100  THEN

         select  * from  dept;

   ELSE if x>100 then

         select *  from emp;

   else if x<10  then

         select  * from  student;

    ELSE

     select * from  dept,emp where  dept.deptno=emp.deptno;

end  if;

end IF;

end if;

END

//

 

call  if_fz(10)

 

----------------------------------------------------------

create table student2(
id int primary key ,
name char(20),
sex char(10),
age int(3),
mobile char(20),
class char(10),
english int(10),
chinese int(10),
math int(10)
)engine=innodb default charset=utf8;
insert into student2 values
(1,'小红','女',23,'13813828824','1719',77,88,98),
(2,'小明','男',23,'13713713711','1720',56,66,55),
(3,'小李','男',23,'15915913911','1719',78,64,87),
(4,'小张','男',23,'15915913912','1720',77,76,77),
(5,'小白','女',24,'15915913913','1719',90,89,98),
(6,'小陈','女',19,'15915913914','1719',84,100,81),
(7,'小钱','女',20,'15915913915',null,45,99,93);

练习题:
delimiter //
drop procedure if exists if_lx;
create procedure if_lx(in x int(10) )
begin
declare i int default(select count(*) from student);
if x>0 and x<=i THEN
select sum(chinese+english+math) from student GROUP BY class ;
else if x>i then
select count(*) from student ;
ELSE
select * from student ;
end IF ;
end IF;
end
//

call if_lx(-1)

----------------------------------------------------------

posted on 2021-12-21 18:06  多测师_肖sir  阅读(223)  评论(0编辑  收藏  举报