MySQL由浅入深探究存储过程

2022-07-26

目录
  • 什么是存储过程
  • 存储过程和存储函数的区别
    • 优点
    • 缺点
  • 存储过程的创建和调用
    • 带in参数模式的存储过程
      • out参数模式的存储过程
        • inout参数模式存储过程和删除查看存储过程

          什么是存储过程

          存储过程(Stored Procedure)也成为存储程序,是一种在数据库中存储复杂程序,以便外部程序调用的一种数据库对象。即预先编辑好SQL语句的集合,这个集合完成了某项具体的功能集合,需要这个功能的时候,只要调用这个过程就好。在业务开发工过程中,一般不要求使用存储过程实现业务流程,编写的存储过程不方便调试和扩展,同时没有移植性。

          简单来说存储过程就是具有名字的一段代码,用来完成一个特定的功能。他和函数很像,但是他不是函数,

          MySQL 5.0 版本开始支持存储过程。

          存储过程和存储函数的区别

          存储函数的限制比较多,例如不能用临时表,只能用表变量,而存储过程的限制较少,存储过程的实现功能要复杂些,而函数的实现功能针对性比较强。

          返回值不同。存储函数必须有返回值,且仅返回一个结果值;存储过程可以没有返回值,但是能返回结果集(out,inout)。

          调用时的不同。存储函数嵌入在SQL中使用,可以在select 存储函数名(变量值);存储过程通过call语句调用 call 存储过程名。

          参数的不同。存储函数的参数类型类似于IN参数,没有类似于OUT和INOUT的参数。存储过程的参数类型有三种,in、out和inout:

          • in:数据只是从外部传入内部使用(值传递),可以是数值也可以是变量
          • out:只允许过程内部使用(不用外部数据),给外部使用的(引用传递:外部的数据会被先清空才会进入到内部),只能是变量
          • inout:外部可以在内部使用,内部修改的也可以给外部使用,典型的引用 传递,只能传递变量。

          优点

          • 存储过程可封装,并隐藏复杂的商业逻辑。
          • 存储过程可以回传值,并可以接受参数。
          • 存储过程无法使用 SELECT 指令来运行,因为它是子程序,与查看表,数据表或用户定义函数不同。
          • 存储过程可以用在数据检验,强制实行商业逻辑等。

          缺点

          • 存储过程,往往定制化于特定的数据库上,因为支持的编程语言不同。当切换到其他厂商的数据库系统时,需要重写原有的存储过程。
          • 存储过程的性能调校与撰写,受限于各种数据库系统。

          存储过程的创建和调用

          创建的存储过程保存在数据库的数据字典中。

          创建语法:

          create procedure 存储过程的名字(参数列表)
          begin
          存储过程体(SQL语句的集合);
          end

          注意:

          ①参数列表包含三个部分:

          参数模式    参数名    参数类型    

          (比如: in s_name varchar(20) )

          声明存储过程:

          CREATE PROCEDURE demo_in_parameter(IN s_name varchar(20))      
          

          参数模式:

          in : 该参数可以作为输入,需要调用方传入值来给存储过程

          out : 该参数可以作为输出,该参数可以作为返回值给调用方

          inout : 该参数既可以做输入,也可以作为输出

          存储过程开始和结束符号:

          BEGIN .... END    

          ②如果存储体只要一句SQL语句,begin和end可以省略,存储体里的sql语句结尾处必须加分号,避免数据库误判为存储过程的结束标记,所以需要我们自定义命令的结尾符号:

          delimiter 结尾标记 比如:

          delimiter $

          如果没有下面用表,先创建

          drop table ages;
          drop table students;
          create table ages(id int,age int);
          create table students(id int,name varchar(4),ta_id int);
          insert into ages(id,age) values(1,12);
          insert into ages(id,age) values(2,22);
          insert into ages(id,age) values(3,32);
          insert into ages(id,age) values(4,42);
          insert into ages(id,age) values(5,52);
          insert into ages(id,age) values(6,62);
          insert into students(id,name,ta_id) values(1,'任波涛',2);
          insert into students(id,name,ta_id) values(2,'田兴伟',1);
          insert into students(id,name,ta_id) values(3,'唐崇俊',3);
          insert into students(id,name,ta_id) values(4,'夏铭睿',8);
          insert into students(id,name,ta_id) values(5,'包琪',1);
          insert into students(id,name,ta_id) values(6,'夏雨',10);
          insert into students(id,name,ta_id) values(7,'夏铭雨',10);
          insert into students(id,name,ta_id) values(8,'白芳芳',6);
          

          无参数存储过程:

          delimiter $		#将语句的结束符号从分号;临时改为两个$(可以是自定义)
          create procedure myp1()
          begin
          insert into ages(id,`age`) values (11,'12');
          insert into ages(id,`age`) values (21,'13');
          insert into ages(id,`age`) values (31,'14');
          insert into ages(id,`age`) values (41,'15');
          end $
          delimiter ;		#将语句的结束符号恢复为分号

          存储过程的调用:

          call 存储过程名(参数列表);

          调用:

          call myp1();

          带in参数模式的存储过程

          案例:通过学生名查询对应的年龄

          delimiter $
          create procedure myp2(in s_name varchar(10))
          begin
          select s.name, a.age from students s
          inner join ages a
          on s.ta_id = a.id
          where s.name=s_name;
          end $
          

          调用:call myp2(‘任波涛’) $

          out参数模式的存储过程

          案例:根据学生姓名,返回对应的年龄

          create procedure myp3(in sname varchar(10),out age int)
          begin
          select a.age into age
          from students s
          inner join ages a
          on s.ta_id = a.id
          where s.name=sname;
          end $
          

          调用:

          call myp3(‘任波涛’,@age) $ #把值取出来放到变量里去

          select @age $ #查看值了

          案例:根据学生姓名,返回对应的年龄和学生编号

          create procedure myp4(in sname varchar(10),out age int,out sid int)
          begin
          select a.age ,s.id into age,sid
          from students s
          inner join ages a
          on s.ta_id = a.id
          where s.name=sname;
          end $
          

          调用:

          call myp4(‘任波涛’,@age,@sid) $

          select @age,@sid $

          inout参数模式存储过程和删除查看存储过程

          案例:传入a和b两个数,然后让a和b都乘以2后返回

          create procedure myp5(inout a int , inout b int)
          begin
          set a=a*2;
          set b=b*2;
          end $
          

          调用:

          set @a=10$
          set @b=20$
          call myp5(@a,@b)$
          select @a,@b $
          delimiter ;

          #查看存储过程

          show procedure status like 'myp%';

          删除存储过程:

          drop procedure 存储过程名;
          drop procedure myp1; #每次只能删除一个

          查看存储过程的信息:

          show create procedure 存储名;
          show create procedure myp1;

          到此这篇关于MySQL由浅入深探究存储过程的文章就介绍到这了,更多相关MySQL存储过程内容请搜索北冥有鱼以前的文章或继续浏览下面的相关文章希望大家以后多多支持北冥有鱼!

          《MySQL由浅入深探究存储过程.doc》

          下载本文的Word格式文档,以方便收藏与打印。