1.什么是存储过程
很多时候,SQL语句都是针对一个或多个表的单条语句。但是也有时候有的查询语句需要进行多次联表查询才能完成,此时就需要用到存储过程了。
存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL语句集,存储在数据库中经过第一次编译后,再次调用不需要再次编译。用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。
存储过程是数据库中的一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程。
2.为什么要使用存储过程
1.存储过程增强了SQL语言的功能和灵活性。存储过程可以用流程控制语句编写,有很强的灵活性,可以完成复杂的判断和较复杂的运算。
2.存储过程允许标准组件是编程。存储过程被创建后,可以在程序中被多次调用,而不必重新编写该存储过程的SQL语句。而且数据库专业人员可以随时对存储过程进行修改,对应用程序源代码毫无影响。
3.存储过程能实现较快的执行速度。如果某一操作包含大量的Transaction-SQL代码或分别被多次执行,那么存储过程要比批处理的执行速度快很多。因为存储过程是预编译的。在首次运行一个存储过程时查询,优化器对其进行分析优化,并且给出最终被存储在系统表中的执行计划。而批处理的Transaction-SQL语句在每次运行时都要进行编译和优化,速度相对要慢一些。
4.存储过程能过减少网络流量。针对同一个数据库对象的操作(如查询、修改),如果这一操作所涉及的Transaction-SQL语句被组织程存储过程,那么当在客户计算机上调用该存储过程时,网络中传送的只是该调用语句,从而大大增加了网络流量并降低了网络负载。
5.存储过程可被作为一种安全机制来充分利用。系统管理员通过执行某一存储过程的权限进行限制,能够实现对相应的数据的访问权限的限制,避免了非授权用户对数据的访问,保证了数据的安全。
3.定义存储过程
语法:
create procedure 过程名(参数1,参数2....)
begin
sql语句;
end
创建存储过程之前我们必须修改mysql语句默认结束符;
3.1 使用delimiter可以修改执行符号
DELIMITER是分割符的意思,因为MySQL默认以";"为分隔符,如果我们没有声明分割符,那么编译器会把存储过程当成SQL语句进行处理,则存储过程的编译过程会报错。
所以要事先用DELIMITER关键字申明当前段分隔符,这样MySQL才会将";"当做存储过程中的代码,不会执行这些代码,用完了之后要把分隔符还原。
例子:
# delimiter 新执行符号
MariaDB [book]> delimiter % # 使用delimter后,每条SQL语句的结束符就变为%
MariaDB [book]> create procedure selCg()
-> begin
-> select * from category;
-> end %
Query OK, 0 rows affected (0.08 sec)
4.调用存储过程
语法:
call 过程名(参数1,参数2);
例子:
MariaDB [book]> call selCg() % # 调用上面定义的存储过程 selCg
+---------+---------------+
| bTypeId | bTypeName |
+---------+---------------+
| 1 | windows应用 |
| 2 | 网站 |
| 4 | linux学习 |
| 5 | Delphi学习 |
| 6 | 黑客 |
| 7 | 网络技术 |
| 8 | 安全 |
| 9 | 平面 |
| 10 | AutoCAD技术 |
+---------+---------------+
9 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> delimiter ; # 使用delimter把SQL语句的结束符改回原来的';'
5.存储过程参数类型
5.1 In参数
特点:读取外部变量值,且有效范围仅限存储过程内部
例子一:
MariaDB [book]> delimiter // # 使用delimter把SQL语句的结束符改为的'//'
MariaDB [book]> create procedure pin(in p_in int) # 定义存储过程,读取外部的整型变量p_in
-> begin
-> select p_in; # 读取参数中传递的p_in的值,p_in来自存储过程外部
-> set p_in = 2; # 更改参数p_in的值为2
-> select p_in; # 再次查询参数p_in的值
-> end//
Query OK, 0 rows affected (0.01 sec)
MariaDB [book]> delimiter ; # 使用delimter把SQL语句的结束符改回原来的';'
MariaDB [book]> set @num = 1; # 在console中设定一个变量num,值为1
Query OK, 0 rows affected (0.01 sec)
MariaDB [book]> call pin(@num); # 把前面定义的变量num做为参数,调用存储过程pin
+------+ # 第一次查询到的值为1,经过存储过程为参数重新赋值,第二次查询到的值为2
| p_in |
+------+
| 1 |
+------+
1 row in set (0.01 sec)
+------+
| p_in |
+------+
| 2 |
+------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
MariaDB [book]> call pin(6); # 再次调用存储过程pin,传入参数为整数6
+------+ # 同样第一次查到的值为6,第二次查到的值改为2
| p_in |
+------+
| 6 |
+------+
1 row in set (0.00 sec)
+------+
| p_in |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> select @num; # 在console中查询num变量的值,仍为1,存储过程的作用范围只在存储过程内部有效
+------+
| @num |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
例子二:定义存储过程 getOneBook,当输入某书籍 id 后,可以调出对应书籍记录
MariaDB [book]> delimiter // # 修改SQL语句结束符为'//'
MariaDB [book]> create procedure get_book_by_id(in b int) # 定义存储过程get_book_by_id,传入book的id,查询对应book的信息记录
-> begin
-> select * from books where bId=b;
-> end//
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> delimiter ; # 修改SQL语句结束符为';'
MariaDB [book]> call get_book_by_id(4); # 查询book的id为4的记录
+-----+---------------------------------+---------+-----------------------+-------+------------+-----------+------------+
| bId | bName | bTypeId | publishing | price | pubDate | author | ISBN |
+-----+---------------------------------+---------+-----------------------+-------+------------+-----------+------------+
| 4 | pagemaker 7.0短期培训教程 | 9 | 中国电力出版社 | 43 | 2005-01-01 | 孙利英 | 7121008947 |
+-----+---------------------------------+---------+-----------------------+-------+------------+-----------+------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> call get_book_by_id(9); # 查询book的id为9的记录
+-----+---------------------------+---------+-----------------------+-------+------------+--------+------------+
| bId | bName | bTypeId | publishing | price | pubDate | author | ISBN |
+-----+---------------------------+---------+-----------------------+-------+------------+--------+------------+
| 9 | Dreamweaver 4网页制作 | 2 | 清华大学出版社 | 45 | 2004-04-01 | 黄宇 | 7505380796 |
+-----+---------------------------+---------+-----------------------+-------+------------+--------+------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
5.2 Out参数
特点:不读取外部变量值,在存储过程执行完毕后保留新值
例子一:
MariaDB [book]> delimiter //
MariaDB [book]> create procedure pout(out p_out int)
-> begin
-> select p_out;
-> set p_out=2;
-> select p_out;
-> end;
-> //
Query OK, 0 rows affected (0.09 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> set @out_num = 1;
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> call pout(@out_num);
+-------+
| p_out |
+-------+
| NULL |
+-------+
1 row in set (0.00 sec)
+-------+
| p_out |
+-------+
| 2 |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> set @num1=1;
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> call pout(@num1);
+-------+
| p_out |
+-------+
| NULL |
+-------+
1 row in set (0.00 sec)
+-------+
| p_out |
+-------+
| 2 |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
例子二:
MariaDB [book]> delimiter //
MariaDB [book]> create procedure pout1(out p_out int)
-> begin
-> set p_outs=8;
-> select p_outs;
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> set @out_num1=1;
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> select @out_num1;
+-----------+
| @out_num1 |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)
MariaDB [book]> call pout1(@out_num1);
+-------+
| p_out |
+-------+
| 8 |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> select @out_num1;
+-----------+
| @out_num1 |
+-----------+
| 8 |
+-----------+
1 row in set (0.00 sec)
不论你怎么赋值都是,存储过程的执行结果都是8。
In传入参数,是外部将值传给存储过程来使用的,而out传出参数是为了讲存储过程的执行结果回传给调用他的程序来使用的.
MariaDB [book]> delimiter //
MariaDB [book]> create procedure demo(out pa varchar(200))
-> begin
-> select bName into pa from books where bId=4;
-> end//
Query OK, 0 rows affected (0.01 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> call demo(@a);
Query OK, 1 row affected (0.07 sec)
MariaDB [book]> select @a;
+---------------------------------+
| @a |
+---------------------------------+
| pagemaker 7.0短期培训教程 |
+---------------------------------+
1 row in set (0.00 sec)
MariaDB [book]> select * from books where bId=4;
+-----+---------------------------------+---------+-----------------------+-------+------------+-----------+------------+
| bId | bName | bTypeId | publishing | price | pubDate | author | ISBN |
+-----+---------------------------------+---------+-----------------------+-------+------------+-----------+------------+
| 4 | pagemaker 7.0短期培训教程 | 9 | 中国电力出版社 | 43 | 2005-01-01 | 孙利英 | 7121008947 |
+-----+---------------------------------+---------+-----------------------+-------+------------+-----------+------------+
1 row in set (0.01 sec)
5.3 Inout参数
特点:读取外部变量,在存储过程执行完后保留新值<类似银行存款>
MariaDB [book]> delimiter //
MariaDB [book]> create procedure p_in_out (inout p_inout_num int)
-> begin
-> select p_inout_num;
-> set p_inout_num=100;
-> select p_inout_num;
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> set @num2=11;
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> call p_in_out(@num2);
+-------------+
| p_inout_num |
+-------------+
| 11 |
+-------------+
1 row in set (0.01 sec)
+-------------+
| p_inout_num |
+-------------+
| 100 |
+-------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
MariaDB [book]> select @num2;
+-------+
| @num2 |
+-------+
| 100 |
+-------+
1 row in set (0.00 sec)
5.4 不加参数的情况
如果在创建存储过程时没有指定参数类型,则需要在调用的时候指定参数值
MariaDB [book]> create table demo2(id int(11));
Query OK, 0 rows affected (0.06 sec)
MariaDB [book]> delimiter //
MariaDB [book]> create procedure insert_table(n1 int)
-> begin
-> set @x=0;
-> repeat set @x=@x+1;
-> insert into demo2 values(@x);
-> until @x>n1
-> end repeat;
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> call insert_table(8);
Query OK, 1 row affected (0.05 sec)
MariaDB [book]> select * from demo2;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+------+
9 rows in set (0.00 sec)
5.5 存储过程变量的使用
MySQL中使用declare进行变量定义
变量定义
DECLARE variable_name [,variable_name...] datatype [DEFAULT value];
datatype为MySQL的数据类型,如:int, float, date, varchar(length)
变量赋值:
SET 变量名 = 表达式值 [,variable_name = expression ...]
变量赋值可以在不同的存储过程中继承
MariaDB [book]> delimiter //
MariaDB [book]> create procedure decl()
-> begin
-> declare name varchar(200);
-> set name=(select bName from books where bId=6);
-> select name;
-> end//
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> call decl;
+------------------------------+
| name |
+------------------------------+
| Dreamweaver 4入门与提高 |
+------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> select bName from books where bId=6;
+------------------------------+
| bName |
+------------------------------+
| Dreamweaver 4入门与提高 |
+------------------------------+
1 row in set (0.00 sec)
5.6 存储过程语句的注释
做过开发的都知道,写注释是个利人利己的事情。便于理解维护
MySQL注释有两种风格
“--“:单行注释
“/*…..*/”:一般用于多行注释
例子:
MariaDB [book]> delimiter //
MariaDB [book]> create procedure p1() -- procedure name is p1
-> /* procedure body */
-> /* start begin */
-> begin
-> declare name varchar(200);
-> set name=(select bName from books where bId=6);
-> select name;
-> end//
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> call p1;
+------------------------------+
| name |
+------------------------------+
| Dreamweaver 4入门与提高 |
+------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
5.7 存储过程流程控制语句
变量作用域:
内部的变量在其作用域范围内享有更高的优先权,当执行到end变量时,内部变量消失,此时已经在其作用域外,变量不再可见了。因为在存储过程外再也不能找到这个申明的变量,但是你可以通过out参数或者将其值指派给会话变量来保存其值。
例子:
MariaDB [book]> delimiter //
MariaDB [book]> create procedure proc2()
-> begin
-> declare x1 varchar(5) default "outer";
-> begin
-> declare x1 varchar(5) default "inner";
-> select x1;
-> end;
-> select x1;
-> end//
Query OK, 0 rows affected (0.01 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> call proc2;
+-------+
| x1 |
+-------+
| inner |
+-------+
1 row in set (0.00 sec)
+-------+
| x1 |
+-------+
| outer |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> select @x1;
+------+
| @x1 |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
5.7.1 if-then -else条件语句
例子:
MariaDB [book]> select * from demo2;
Empty set (0.00 sec)
MariaDB [book]> delimiter //
MariaDB [book]> create procedure proc1(in args int)
-> begin
-> declare var int;
-> set var = args + 1;
-> if var = 0 then
-> insert into demo2 values(17);
-> end if;
-> if args = 0 then
-> update demo2 set id=id+1;
-> else
-> update demo2 set id=id+2;
-> end if;
-> end //
Query OK, 0 rows affected (0.01 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> call proc1(2);
Query OK, 0 rows affected (0.02 sec)
MariaDB [book]> select * from demo2;
Empty set (0.00 sec)
MariaDB [book]> call proc1(-1);
Query OK, 1 row affected (0.01 sec)
MariaDB [book]> select * from demo2;
+------+
| id |
+------+
| 19 |
+------+
1 row in set (0.00 sec)
5.7.2 case语句
例子:
MariaDB [book]> delimiter //
MariaDB [book]> create procedure proc3(in args int)
-> begin
-> declare var int;
-> set var =args + 1;
-> case var
-> when 0 then
-> insert into demo2 values(20);
-> when 1 then
-> insert into demo2 values(30);
-> else
-> insert into demo2 values(40);
-> end case;
-> end //
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> call proc3(1);
Query OK, 1 row affected (0.01 sec)
MariaDB [book]> select * from demo2;
+------+
| id |
+------+
| 19 |
| 40 |
+------+
2 rows in set (0.01 sec)
MariaDB [book]> call proc3(6);
Query OK, 1 row affected (0.01 sec)
MariaDB [book]> select * from demo2;
+------+
| id |
+------+
| 19 |
| 40 |
| 40 |
+------+
3 rows in set (0.00 sec)
5.7.3 while ···· end while循环语句
例子:
MariaDB [book]> delimiter //
MariaDB [book]> create procedure proc4()
-> begin
-> declare var int;
-> set var=0;
-> while var < 6 do
-> insert into demo2 values(var);
-> set var = var + 1;
-> end while;
-> end//
Query OK, 0 rows affected (0.01 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> select * from demo2;
+------+
| id |
+------+
| 19 |
| 40 |
| 40 |
+------+
3 rows in set (0.00 sec)
MariaDB [book]> call proc4;
Query OK, 1 row affected (0.03 sec)
MariaDB [book]> select * from demo2;
+------+
| id |
+------+
| 19 |
| 40 |
| 40 |
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
9 rows in set (0.00 sec)
5.7.4 repeat···· end repeat
执行操作后检查结果,而while则是执行前进行检查。
例子:
MariaDB [book]> delimiter //
MariaDB [book]> create procedure proc5()
-> begin
-> declare v int;
-> set v = 0;
-> repeat
-> insert into demo2 values(v);
-> set v=v+1;
-> until v> 5
-> end repeat;
-> end//
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> truncate table demo2;
Query OK, 0 rows affected (0.02 sec)
MariaDB [book]> select * from demo2;
Empty set (0.01 sec)
MariaDB [book]> call proc5;
Query OK, 1 row affected (0.05 sec)
MariaDB [book]> select * from demo2;
+------+
| id |
+------+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
6 rows in set (0.00 sec)
5.7.5 loop ·····end loop
loop循环不需要初始条件,这点和while 循环相似,同时和repeat循环一样不需要结束条件, leave语句的意义是离开循环。
例子:
MariaDB [book]> delimiter //
MariaDB [book]> truncate table demo2//
Query OK, 0 rows affected (0.02 sec)
MariaDB [book]> create procedure proc6()
-> begin
-> declare v int;
-> set v=0;
-> loop_lable:loop
-> insert into demo2 values(v);
-> set v=v+1;
-> if v>=5 then
-> leave loop_lable;
-> end if;
-> end loop;
-> end//
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> select * from demo2;
Empty set (0.00 sec)
MariaDB [book]> call proc6;
Query OK, 1 row affected (0.05 sec)
MariaDB [book]> select * from demo2;
+------+
| id |
+------+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
+------+
5 rows in set (0.00 sec)
5.7.6 LABLES 标号
标号可以用在begin repeat while 或者loop 语句前,语句标号只能在合法的语句前面使用。可以跳出循环,使运行指令达到复合语句的最后一步。
5.7.7 ITERATE迭代
通过引用复合语句的标号,来从新开始复合语句
例子:
MariaDB [book]> delimiter //
MariaDB [book]> create procedure proc7()
-> begin
-> declare v int;
-> set v=0;
-> loop_lable:loop
-> if v =3 then
-> set v=v+1;
-> iterate loop_lable;
-> end if;
-> insert into demo2 values(v);
-> set v=v+1;
-> if v>=10 then
-> leave loop_lable;
-> end if;
-> end loop;
-> end //
Query OK, 0 rows affected (0.01 sec)
MariaDB [book]> truncate table demo2//
Query OK, 0 rows affected (0.01 sec)
MariaDB [book]> select * from demo2//
Empty set (0.00 sec)
MariaDB [book]> delimiter ;
MariaDB [book]> call proc7;
Query OK, 1 row affected (0.03 sec)
MariaDB [book]> select * from demo2;
+------+
| id |
+------+
| 0 |
| 1 |
| 2 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+------+
9 rows in set (0.00 sec)
6. 查看存储过程
语法:
show create procedure 存储过程名 \G
例子:
MariaDB [book]> show create procedure proc7 \G
*************************** 1. row ***************************
Procedure: proc7
sql_mode:
Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `proc7`()
begin
declare v int;
set v=0;
loop_lable:loop
if v =3 then
set v=v+1;
iterate loop_lable;
end if;
insert into demo2 values(v);
set v=v+1;
if v>=10 then
leave loop_lable;
end if;
end loop;
end
character_set_client: utf8
collation_connection: utf8_unicode_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec)
MariaDB [book]> show create procedure proc6 \G
*************************** 1. row ***************************
Procedure: proc6
sql_mode:
Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `proc6`()
begin
declare v int;
set v=0;
loop_lable:loop
insert into demo2 values(v);
set v=v+1;
if v>=5 then
leave loop_lable;
end if;
end loop;
end
character_set_client: utf8
collation_connection: utf8_unicode_ci
Database Collation: utf8_general_ci
1 row in set (0.01 sec)
7.查看存储过程状态
语法:
show procedure status \G 查看所有存储过程
例子:
MariaDB [book]> show procedure status \G
*************************** 1. row ***************************
Db: book
Name: decl
Type: PROCEDURE
Definer: root@localhost
Modified: 2018-08-29 08:51:15
Created: 2018-08-29 08:51:15
Security_type: DEFINER
Comment:
character_set_client: utf8
collation_connection: utf8_unicode_ci
Database Collation: utf8_general_ci
*************************** 2. row ***************************
Db: book
Name: demo
Type: PROCEDURE
Definer: root@localhost
Modified: 2018-08-29 08:42:24
Created: 2018-08-29 08:42:24
Security_type: DEFINER
Comment:
character_set_client: utf8
collation_connection: utf8_unicode_ci
Database Collation: utf8_general_ci
*************************** 3. row ***************************
Db: book
Name: get_book_by_id
Type: PROCEDURE
Definer: root@localhost
Modified: 2018-08-29 08:09:44
Created: 2018-08-29 08:09:44
Security_type: DEFINER
Comment:
character_set_client: utf8
collation_connection: utf8_unicode_ci
Database Collation: utf8_general_ci
16 rows in set (0.04 sec)
MariaDB [book]> show procedure status ;
+------+----------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+------+----------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| book | decl | PROCEDURE | root@localhost | 2018-08-29 08:51:15 | 2018-08-29 08:51:15 | DEFINER | | utf8 | utf8_unicode_ci | utf8_general_ci |
| book | demo | PROCEDURE | root@localhost | 2018-08-29 08:42:24 | 2018-08-29 08:42:24 | DEFINER | | utf8 | utf8_unicode_ci | utf8_general_ci |
| book | get_book_by_id | PROCEDURE | root@localhost | 2018-08-29 08:09:44 | 2018-08-29 08:09:44 | DEFINER | | utf8 | utf8_unicode_ci | utf8_general_ci |
| book | insert_table | PROCEDURE | root@localhost | 2018-08-29 08:48:54 | 2018-08-29 08:48:54 | DEFINER | | utf8 | utf8_unicode_ci | utf8_general_ci |
| book | pin | PROCEDURE | root@localhost | 2018-08-29 08:07:00 | 2018-08-29 08:07:00 | DEFINER | | utf8 | utf8_unicode_ci | utf8_general_ci |
| book | pout | PROCEDURE | root@localhost | 2018-08-29 08:31:36 | 2018-08-29 08:31:36 | DEFINER | | utf8 | utf8_unicode_ci | utf8_general_ci |
| book | pout1 | PROCEDURE | root@localhost | 2018-08-29 08:36:39 | 2018-08-29 08:36:39 | DEFINER | | utf8 | utf8_unicode_ci | utf8_general_ci |
+------+----------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
7 rows in set (0.02 sec)
7. 修改存储过程:
使用alter语句修改存储过程
语法:
ALTER {PROCEDURE | FUNCTION} sp_name [characteristic ...]
characteristic:
{ CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'
参数说明:
sp_name参数表示存储过程或函数的名称
characteristic参数指定存储函数的特性
CONTAINS SQL表示子程序包含SQL语句,但不包含读或写数据的语句;
NO SQL表示子程序中不包含SQL语句
READS SQL DATA表示子程序中包含读数据的语句
MODIFIES SQL DATA表示子程序中包含写数据的语句
SQL SECURITY { DEFINER | INVOKER }指明谁有权限来执行
DEFINER表示只有定义者自己才能够执行
INVOKER表示调用者可以执行
COMMENT 'string'是注释信息。
8.删除存储过程
方法一:
DROP PROCEDURE 存储过程名
语法:
MariaDB [book]> drop procedure demo;
Query OK, 0 rows affected (0.00 sec)
方法二:
DROP PROCEDURE IF EXISTS 存储过程名
这个语句被用来移除一个存储程序。
需要注意的是:
不能在一个存储过程中删除另一个存储过程,只能调用另一个存储过程
9.存储过程的缺点:
1)可移植性差
2)对于简单的SQL语句,存储过程没什么优势
3)如果存储过程中不一定会减少网络传输
4)如果只有一个用户使用数据库,那么存储过程对安全也没什么影响
5)团队开发时需要先统一标准,否则后期维护成本大
6)在大并发量访问的情况下,不宜写过多涉及运算的存储过程
7)业务逻辑复杂时,特别是涉及到对很大的表进行操作的时候,不如在前端先简化业务逻辑