NSD DBA2 DAY05
1 案例1:创建存储过程
1.1 问题
- 存储过程名称为p1
- 功能显示user表中 shell是/bin/bash的用户个数
- 调用存储过程p1
1.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:存储过程基本使用
1)创建存储过程
- mysql> delimiter // //定义定界符
- mysql> create procedure say() //say()随便写括号一定要有
- -> begin
- -> select * from user where id<=10;
- -> end
- -> //
- Query OK, 0 rows affected (0.01 sec)
- mysql> delimiter ; //把命令的定界符改回来,分号前有空格
- mysql> call say(); //调用存储过程名,在括号里面不写参数时,可以不加括号
2)查看存储过程
方法一:
- mysql> show procedure status\G;
方法二:
- mysql> select db,name,type from mysql.proc where name= "say";
3)删除存储过程
- mysql> drop procedure say;
- Query OK, 0 rows affected (0.00 sec)
4)创建存储过程名称为p1
功能显示user表中 shell是/bin/bash的用户
调用存储过程p1
- mysql> delimiter //
- mysql> create procedure p1()
- -> begin
- -> select count(username) from user where shell="/bin/bash";
- -> end
- -> //
- mysql> delimiter ;
- mysql> call p1();
- +-----------+
- | shell |
- +-----------+
- | /bin/bash |
- | /bin/bash |
- +-----------+
- 2 rows in set (0.01 sec)
- Query OK, 0 rows affected (0.01 sec)
2 案例2:存储过程参数的使用
2.1 问题
- 创建名为p2的存储过程
- 可以接收用户输入shell的名字
- 统计user表中用户输入shell名字的个数
2.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:存储过程参数的使用
1)参数类型
MySQL存储过程,共有三种参数类型IN,OUT,INOUT
Create procedure 名称(
类型 参数名 数据类型,
类型 参数名 数据类型
)
in 输入参数 传递值给存储过程,必须在调用存储过程时指定,在存储过程中修改该参数的值不能;默认类型是in
out 输出参数 该值可在存储过程内部被改变,并可返回
inout 输入/输出参数 调用时指定,并且可被改变和返回
- mysql> delimiter //
- mysql> create procedure say2(in username char(10))
- -> begin
- -> select username;
- -> select * from user where username=username;
- -> end
- -> //
- Query OK, 0 rows affected (0.00 sec)
- mysql> delimiter ;
- mysql> call say2("tom");
2)创建名为p2的存储过程,可以接收用户输入shell的名字,统计user表中用户输入shell名字的个数
- mysql> delimiter //
- mysql> create procedure p2(out number int)
- -> begin
- -> select count(username) into @number from user where shell!="/bin/bash";
- -> select @number;
- -> end
- -> //
- Query OK, 0 rows affected (0.01 sec)
- mysql> delimiter ;
- mysql> call p2(@number);
- +---------+
- | @number |
- +---------+
- | 38 |
- +---------+
- 1 row in set (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
3 案例3:使用循环结构
3.1 问题
- 定义名称为p3的存储过程
- 用户可以自定义显示user表记录的行数
- 若调用时用户没有输入行数,默认显示第1条记录
3.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:算数运算
1)算数运算符号,如图-1所示:

图-1
- mysql> set @z=1+2;select @z;
- Query OK, 0 rows affected (0.00 sec)
- +------+
- | @z |
- +------+
- | 3 |
- +------+
- 1 row in set (0.00 sec)
- mysql> set @x=1; set @y=2;set @z=@x*@y; select @z;
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- +------+
- | @z |
- +------+
- | 2 |
- +------+
- 1 row in set (0.00 sec)
- mysql> set @x=1; set @y=2;set @z=@x-@y; select @z;
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- +------+
- | @z |
- +------+
- | -1 |
- +------+
- 1 row in set (0.00 sec)
- mysql> set @x=1; set @y=2;set @z=@x/@y; select @z;
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- +-------------+
- | @z |
- +-------------+
- | 0.500000000 |
- +-------------+
- 1 row in set (0.00 sec)
declare调用变量不需要@其他都需要
调用变量时,有@符号的变量 如@x:调用的是用户自定义变量
没有@符号的变量 如x:调用的是存储过程的参数变量
- mysql> delimiter //
- mysql> create procedure say5(in bash char(20), in nologin char(25), out x int , out y int)
- -> begin
- -> declare z int ;
- -> set z=0;
- -> select count(username) into @x from user where shell=bash;
- -> select count(username) into @y from user where shell=nologin;
- -> set z=@x+@y;
- -> select z;
- -> end
- -> //
- Query OK, 0 rows affected (0.00 sec)
- mysql> delimiter ;
- mysql> call say5("/bin/bash","/sbin/nologin",@x,@y);
- +------+
- | z |
- +------+
- | 36 |
- +------+
- 1 row in set (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
2)条件判断,数值的比较如图-2所示:

图-2
逻辑比较、范围、空、非空、模糊、正则,如图-3所示:

图-3
顺序结构(if判断)当“条件成立”时执行命令序列,否则,不执行任何操作
- mysql> delimiter //
- mysql> create procedure say6(in x int(1) )
- -> begin
- -> if x <= 10 then
- -> select * from user where id <=x;
- -> end if;
- -> end
- -> //
- Query OK, 0 rows affected (0.01 sec)
- mysql> delimiter ;
- mysql> call say6(1); //条件判断成立,等于1是否成立
- +----+----------+----------+------+------+---------+---------+-----------+
- | id | username | password | uid | gid | comment | homedir | shell |
- +----+----------+----------+------+------+---------+---------+-----------+
- | 1 | root | x | 0 | 0 | root | /root | /bin/bash |
- +----+----------+----------+------+------+---------+---------+-----------+
- 1 row in set (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- mysql> call say6(2);
- +----+----------+----------+------+------+---------+---------+---------------+
- | id | username | password | uid | gid | comment | homedir | shell |
- +----+----------+----------+------+------+---------+---------+---------------+
- | 1 | root | x | 0 | 0 | root | /root | /bin/bash |
- | 2 | bin | x | 1 | 1 | bin | /bin | /sbin/nologin |
- +----+----------+----------+------+------+---------+---------+---------------+
- 2 rows in set (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
3)定义名称为p3的存储过程,用户可以自定义显示user表记录的行数,若调用时用户没有输入行数,默认显示第1条记录
- mysql> delimiter //
- mysql> create procedure p3(in linenum char(10) )
- -> begin
- -> if linenum is null then
- -> set @linenum=1;
- -> select * from user where id=@linenum;
- -> else
- -> select linenum;
- -> select * from user where id=linenum;
- -> end if;
- -> end
- -> //
- Query OK, 0 rows affected (0.00 sec)
- mysql> delimiter ;
- mysql> call p3(null); //不输入查看的行数
- +----+----------+----------+------+------+---------+---------+-----------+
- | id | username | password | uid | gid | comment | homedir | shell |
- +----+----------+----------+------+------+---------+---------+-----------+
- | 1 | root | x | 0 | 0 | root | /root | /bin/bash |
- +----+----------+----------+------+------+---------+---------+-----------+
- 1 row in set (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- mysql> call p3(3); //输入查看的行数
- +---------+
- | linenum |
- +---------+
- | 3 |
- +---------+
- 1 row in set (0.00 sec)
- +----+----------+----------+------+------+---------+---------+---------------+
- | id | username | password | uid | gid | comment | homedir | shell |
- +----+----------+----------+------+------+---------+---------+---------------+
- | 3 | daemon | x | 2 | 2 | daemon | /sbin | /sbin/nologin |
- +----+----------+----------+------+------+---------+---------+---------------+
- 1 row in set (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)