31_MySQL视图 MySQL存储过程

版本:5.7.28

1.视图的基本使用
把/etc/passwd文件的内容存储到db9库下的user表里
添加新字段id 存储记录的行号(在所有字段的前边)
创建视图v1 结构及数据user表的字段、记录一样。
创建视图v2 只有user表shell是/bin/bash用户信息 。
分别对视图表和基表执行insert update delete 操作。
删除视图v1 和 v2
 
什么是视图:是一种虚拟存在的表
内容与真实的表相似,包含一系列带有名称的列和行数据。
视图并不在数据库中以存储的数据的形式存在。
行和列的数据来自定义视图时查询所引用的基本表,并且在具体引用视图时动态生成。
更新视图的数据,就是更新基表的数据
更新基表数据,视图的数据也会跟着改变
 
1.1 把/etc/passwd文件的内容存储到db9库下的user表里
mysql> CREATE DATABASE db9;
mysql> create table db9.user(username char(20),password char(1),uid \
int(2),gid int(2),comment char(100),homedir char(100),shell char(50));

mysql> desc user;
+----------+-----------+------+-----+---------+-------+
| Field    | Type      | Null | Key | Default | Extra |
+----------+-----------+------+-----+---------+-------+
| username | char(20)  | YES  |     | NULL    |       |
| password | char(1)   | YES  |     | NULL    |       |
| uid      | int(2)    | YES  |     | NULL    |       |
| gid      | int(2)    | YES  |     | NULL    |       |
| comment  | char(100) | YES  |     | NULL    |       |
| homedir  | char(100) | YES  |     | NULL    |       |
| shell    | char(50)  | YES  |     | NULL    |       |
+----------+-----------+------+-----+---------+-------+

mysql> system cp /etc/passwd /var/lib/mysql-files/
mysql> system ls /var/lib/mysql-files/
passwd
mysql> load data infile "/var/lib/mysql-files/passwd" into table db9.user \
    -> fields terminated by ":" lines terminated by "\n";
 
1.2 添加新字段id 存储记录的行号(在所有字段的前边)
mysql> alter table db9.user add id int(2) primary key auto_increment first;
31_MySQL视图 MySQL存储过程_第1张图片
 
1.3 创建视图v1 结构及数据user表的字段、记录一样
mysql> create view v1 as select * from user;
mysql> select * from v1;
31_MySQL视图 MySQL存储过程_第2张图片

1.4 创建视图v2 只有user表shell是/bin/bash用户信息
mysql> create view v2 as select shell from user where shell="/bin/bash";
mysql> select * from v2;
+-----------+
| shell     |
+-----------+
| /bin/bash |
+-----------+
 
1.5 分别对视图表和基表执行insert update delete 操作
mysql> insert into v1(username,uid) values("jarry",9);   //插入记录
mysql> update v1 set uid=9 where username="adm";   //更新记录
mysql> delete from v1 where uid=9;                              //删除记录
 
1.6 删除视图v1 和 v2
mysql> drop view v1;
mysql> drop view v2;
 
2. 视图进阶操作
OR REPLACE的选项使用
WITH LOCAL CHECK OPTION 选项的使用
WITH CASCADED CHECK OPTION 选项的使用
 
2.1 创建视图完全格式
mysql> create table user2 select username,uid,gid from user limit 3;
快速建表(user2表)

mysql> select * from user2;
+----------+------+------+
| username | uid  | gid  |
+----------+------+------+
| root     |    0 |    0 |
| bin      |    1 |    1 |
| daemon   |    2 |    2 |
+----------+------+------+

mysql> create table info select username,uid,homedir,shell from user limit 5;
快速建表(info表)

mysql> select * from info;
+----------+------+----------------+---------------+
| username | uid  | homedir        | shell         |
+----------+------+----------------+---------------+
| root     |    0 | /root          | /bin/bash     |
| bin      |    1 | /bin           | /sbin/nologin |
| daemon   |    2 | /sbin          | /sbin/nologin |
| lp       |    4 | /var/spool/lpd | /sbin/nologin |
| sync     |    5 | /sbin          | /bin/sync     |
+----------+------+----------------+---------------+
 
2.2 查询user2.username(3)=info.username(4)的字段
mysql> select * from user2 left join info on user2.username=info.username;
+----------+------+------+----------+------+---------+---------------+
| username | uid  | gid  | username | uid  | homedir | shell         |
+----------+------+------+----------+------+---------+---------------+
| root     |    0 |    0 | root     |    0 | /root   | /bin/bash     |
| bin      |    1 |    1 | bin      |    1 | /bin    | /sbin/nologin |
| daemon   |    2 |    2 | daemon   |    2 | /sbin   | /sbin/nologin |
+----------+------+------+----------+------+---------+---------------+

mysql> select * from info left join user2 on info.username=user2.username;
+----------+------+----------------+---------------+----------+------+------+
| username | uid  | homedir        | shell         | username | uid  | gid  |
+----------+------+----------------+---------------+----------+------+------+
| root     |    0 | /root          | /bin/bash     | root     |    0 |    0 |
| bin      |    1 | /bin           | /sbin/nologin | bin      |    1 |    1 |
| daemon   |    2 | /sbin          | /sbin/nologin | daemon   |    2 |    2 |
| lp       |    4 | /var/spool/lpd | /sbin/nologin | NULL     | NULL | NULL |
| sync     |    5 | /sbin          | /bin/sync     | NULL     | NULL | NULL |
+----------+------+----------------+---------------+----------+------+------+

mysql> select * from user2 right join info on user2.username=info.username;
+----------+------+------+----------+------+----------------+---------------+
| username | uid  | gid  | username | uid  | homedir        | shell         |
+----------+------+------+----------+------+----------------+---------------+
| root     |    0 |    0 | root     |    0 | /root          | /bin/bash     |
| bin      |    1 |    1 | bin      |    1 | /bin           | /sbin/nologin |
| daemon   |    2 |    2 | daemon   |    2 | /sbin          | /sbin/nologin |
| NULL     | NULL | NULL | lp       |    4 | /var/spool/lpd | /sbin/nologin |
| NULL     | NULL | NULL | sync     |    5 | /sbin          | /bin/sync     |
+----------+------+------+----------+------+----------------+---------------+

mysql> select * from info right join user2 on info.username=user2.username;
+----------+------+---------+---------------+----------+------+------+
| username | uid  | homedir | shell         | username | uid  | gid  |
+----------+------+---------+---------------+----------+------+------+
| root     |    0 | /root   | /bin/bash     | root     |    0 |    0 |
| bin      |    1 | /bin    | /sbin/nologin | bin      |    1 |    1 |
| daemon   |    2 | /sbin   | /sbin/nologin | daemon   |    2 |    2 |
+----------+------+---------+---------------+----------+------+------+


2.3 关联查询建的视图 默认不允许修改视图字段的值
mysql> create view v4 as select a.username as ausername,b.username as busername,a.uid as auid,b.uid as buid from user2 a left join info b on a.username=b.username;

mysql> select * from v4;
+-----------+-----------+------+------+
| ausername | busername | auid | buid |
+-----------+-----------+------+------+
| root      | root      |    0 |    0 |
| bin       | bin       |    1 |    1 |
| daemon    | daemon    |    2 |    2 |
+-----------+-----------+------+------+

总结:
LEFT JOIN:
  将前者与后者连接排列输出,以前者为准,后者数据多则删除,少则NULL补充;
RIGHT JOIN:
  将前者与后者连接排列输出,以后者为准,前者数据多则删除,少则NULL补充;
 
2.4 OR REPLACE的选项使用
创建时,若视图已存在,会替换已有的视图
语法格式:create or replace view视图名as select 查询; //达到修改已有视图的目的
mysql> create or replace view v4 as select a.username as ausername,b.username as busername,a.uid as auid,b.uid as buid  from user2 a left join info b on a.username=b.username;

mysql> SELECT * FROM v4;                                                                                                          +-----------+-----------+------+------+
| ausername | busername | auid | buid |
+-----------+-----------+------+------+
| root      | root      |    0 |    0 |
| bin       | bin       |    1 |    1 |
| daemon    | daemon    |    2 |    2 |
+-----------+-----------+------+------+
 
2.5 WITH LOCAL CHECK OPTION
LOCAL和CASCADED关键字决定检查的范围
LOCAL 仅检查当前视图的限制
CASCADED 同时要满足基表的限制(默认值)

mysql> create table user1 select username,uid,shell from user where uid>=5 and uid <=40;
mysql> select * from user1;
+----------+------+----------------+
| username | uid  | shell          |
+----------+------+----------------+
| sync     |    5 | /bin/sync      |
| shutdown |    6 | /sbin/shutdown |
| halt     |    7 | /sbin/halt     |
| mail     |    8 | /sbin/nologin  |
| operator |   11 | /sbin/nologin  |
| games    |   12 | /sbin/nologin  |
| ftp      |   14 | /sbin/nologin  |
| mysql    |   27 | /bin/false     |
+----------+------+----------------+

mysql> create view v1 as select username,uid from user1 where uid<=20;
mysql> select * from v1;
+----------+------+
| username | uid  |
+----------+------+
| sync     |    5 |
| shutdown |    6 |
| halt     |    7 |
| mail     |    8 |
| operator |   11 |
| games    |   12 |
| ftp      |   14 |
+----------+------+
 
mysql> update v1 set uid=21 where username="sync";
操作超过视图表的条件限制(uid<=20)之后,在视图表v1里面查看不到,在基表user1里可以查看到,也就是说,更新视图表数据会直接更改基表数据;更新视图表数据超出视图限制,会修改成功,但是会被限制在要求之外。
 
mysql> update user1 set uid=41 where username="ftp";
mysql> select * from user1;
+----------+------+----------------+
| username | uid  | shell          |
+----------+------+----------------+
| sync     |   21 | /bin/sync      |
| shutdown |    6 | /sbin/shutdown |
| halt     |    7 | /sbin/halt     |
| mail     |    8 | /sbin/nologin  |
| operator |   11 | /sbin/nologin  |
| games    |   12 | /sbin/nologin  |
| ftp      |   41 | /sbin/nologin  |
| mysql    |   27 | /bin/false     |
+----------+------+----------------+

基表在超过条件限制(uid>=5 and uid <=40),在基表里依然可以查看到
也就是说,基表随便修改都能成功

mysql> select * from v1;
+----------+------+
| username | uid  |
+----------+------+
| shutdown |    6 |
| halt     |    7 |
| mail     |    8 |
| operator |   11 |
| games    |   12 |
+----------+------+

v1里面没有,ftp移除
也就是说,修改基表数据,会直接影响视图数据,
进而说明,视图数据是动态生成,数据来源相当于于查询基表数据时产生的动态缓存。
 
mysql> create table a select * from user where uid < 10;
mysql> select * from a;
+----+----------+----------+------+------+----------+-----------------+----------------+
| 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  |
|  3 | daemon   | x        |    2 |    2 | daemon   | /sbin           | /sbin/nologin  |
|  5 | lp       | x        |    4 |    7 | lp       | /var/spool/lpd  | /sbin/nologin  |
|  6 | sync     | x        |    5 |    0 | sync     | /sbin           | /bin/sync      |
|  7 | shutdown | x        |    6 |    0 | shutdown | /sbin           | /sbin/shutdown |
|  8 | halt     | x        |    7 |    0 | halt     | /sbin           | /sbin/halt     |
|  9 | mail     | x        |    8 |   12 | mail     | /var/spool/mail | /sbin/nologin  |
+----+----------+----------+------+------+----------+-----------------+----------------+
 
mysql> create view v3 as select * from a where uid < 10 with check option;
//不写默认为CASCADED检查自己和a都要满足要求
mysql> select * from v3;
+----+----------+----------+------+------+----------+-----------------+----------------+
| 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  |
|  3 | daemon   | x        |    2 |    2 | daemon   | /sbin           | /sbin/nologin  |
|  5 | lp       | x        |    4 |    7 | lp       | /var/spool/lpd  | /sbin/nologin  |
|  6 | sync     | x        |    5 |    0 | sync     | /sbin           | /bin/sync      |
|  7 | shutdown | x        |    6 |    0 | shutdown | /sbin           | /sbin/shutdown |
|  8 | halt     | x        |    7 |    0 | halt     | /sbin           | /sbin/halt     |
|  9 | mail     | x        |    8 |   12 | mail     | /var/spool/mail | /sbin/nologin  |
+----+----------+----------+------+------+----------+-----------------+----------------+
 
mysql> update v3 set uid=9 where username="bin";  //更改成功
mysql> update v3 set uid=11 where username="bin"; //失败
ERROR 1369 (HY000): CHECK OPTION failed 'db9.v3'
 
mysql> create view v2 as select * from v1 where uid >= 5 with local check option;
//满足自身v2的要求
mysql> select * from v2;
+----------+------+
| username | uid  |
+----------+------+
| shutdown |    6 |
| halt     |    7 |
| mail     |    8 |
| operator |   11 |
| games    |   12 |
+----------+------+

mysql> update v2 set uid=4 where username="mail";
ERROR 1369 (HY000): CHECK OPTION failed 'db9.v2'
自身限制,修改失败

mysql> update v2 set uid=9 where username="mail";
mysql> select * from v2;
+----------+------+
| username | uid  |
+----------+------+
| shutdown |    6 |
| halt     |    7 |
| mail     |    9 |
| operator |   11 |
| games    |   12 |
+----------+------+
v2更新成功

mysql> select * from v1;
+----------+------+
| username | uid  |
+----------+------+
| shutdown |    6 |
| halt     |    7 |
| mail     |    9 |
| operator |   11 |
| games    |   12 |
+----------+------+
v1也更改了

mysql> select * from user1;
+----------+------+----------------+
| username | uid  | shell          |
+----------+------+----------------+
| sync     |   21 | /bin/sync      |
| shutdown |    6 | /sbin/shutdown |
| halt     |    7 | /sbin/halt     |
| mail     |    9 | /sbin/nologin  |
| operator |   11 | /sbin/nologin  |
| games    |   12 | /sbin/nologin  |
| ftp      |   41 | /sbin/nologin  |
| mysql    |   27 | /bin/false     |
+----------+------+----------------+
基表也修改了
 
2.6 WITH CASCADED CHECK OPTION
mysql> create view v5 as select * from v1 where uid >= 5 with cascaded check option;
mysql> select * from v5;
+----------+------+
| username | uid  |
+----------+------+
| shutdown |    6 |
| halt     |    7 |
| mail     |    9 |
| operator |   11 |
| games    |   12 |
+----------+------+

mysql> update v5 set uid=4 where username="mail";
ERROR 1369 (HY000): CHECK OPTION failed 'db9.v5'

mysql> update v5 set uid=10 where username="mail";
成功,v1和基表user1都更新

3.创建存储过程
存储过程名称为p1
功能显示user表中 shell是/bin/bash的用户个数
调用存储过程p1
 
3.1 创建存储过程
mysql> delimiter //     //定义定界符
mysql> create procedure say()  //say随便写括号一定要有
    -> begin
    -> select * from user where id<=10;
    -> end
     -> //
mysql> delimiter ;    //把命令的定界符改回来,分号前有空格
mysql> call say();    //调用存储过程名,在括号里面不写参数时,可以不加括号
+----+----------+----------+------+------+----------+-----------------+----------------+
| 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  |
|  3 | daemon   | x        |    2 |    2 | daemon   | /sbin           | /sbin/nologin  |
|  5 | lp       | x        |    4 |    7 | lp       | /var/spool/lpd  | /sbin/nologin  |
|  6 | sync     | x        |    5 |    0 | sync     | /sbin           | /bin/sync      |
|  7 | shutdown | x        |    6 |    0 | shutdown | /sbin           | /sbin/shutdown |
|  8 | halt     | x        |    7 |    0 | halt     | /sbin           | /sbin/halt     |
|  9 | mail     | x        |    8 |   12 | mail     | /var/spool/mail | /sbin/nologin  |
| 10 | operator | x        |   11 |    0 | operator | /root           | /sbin/nologin  |
+----+----------+----------+------+------+----------+-----------------+----------------+
 
创建存储过程名称为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();
+-----------------+
| count(username) |
+-----------------+
|               1 |
+-----------------+
 
mysql> select username from user where shell="/bin/bash";
+----------+
| username |
+----------+
| root     |
+----------+

查看存储过程
方法一:
mysql> show procedure status\G
方法二:
mysql> select db,name,type from mysql.proc where name= "say";
+-----+------+-----------+
| db  | name | type      |
+-----+------+-----------+
| db9 | say  | PROCEDURE |
+-----+------+-----------+
 
3.3 删除存储过程
mysql> drop procedure say;
 
3.4 查看已有过程
mysql> select db,name,type from mysql.proc;
+-----+-------------------------------------+-----------+
| db  | name                                | type      |
+-----+-------------------------------------+-----------+
| db9 | p1                                  | PROCEDURE |
| sys | create_synonym_db                   | PROCEDURE |
| sys | diagnostics                         | PROCEDURE |
| sys | execute_prepared_stmt               | PROCEDURE |
| sys | extract_schema_from_file_name       | FUNCTION  |
| sys | extract_table_from_file_name        | FUNCTION  |
| sys | format_bytes                        | FUNCTION  |
| sys | format_path                         | FUNCTION  |
| sys | format_statement                    | FUNCTION  |
| sys | format_time                         | FUNCTION  |
| sys | list_add                            | FUNCTION  |
| sys | list_drop                           | FUNCTION  |
| sys | ps_is_account_enabled               | FUNCTION  |
| sys | ps_is_consumer_enabled              | FUNCTION  |
| sys | ps_is_instrument_default_enabled    | FUNCTION  |
| sys | ps_is_instrument_default_timed      | FUNCTION  |
| sys | ps_is_thread_instrumented           | FUNCTION  |
| sys | ps_setup_disable_background_threads | PROCEDURE |
| sys | ps_setup_disable_consumer           | PROCEDURE |
| sys | ps_setup_disable_instrument         | PROCEDURE |
| sys | ps_setup_disable_thread             | PROCEDURE |
| sys | ps_setup_enable_background_threads  | PROCEDURE |
| sys | ps_setup_enable_consumer            | PROCEDURE |
| sys | ps_setup_enable_instrument          | PROCEDURE |
| sys | ps_setup_enable_thread              | PROCEDURE |
| sys | ps_setup_reload_saved               | PROCEDURE |
| sys | ps_setup_reset_to_default           | PROCEDURE |
| sys | ps_setup_save                       | PROCEDURE |
| sys | ps_setup_show_disabled              | PROCEDURE |
| sys | ps_setup_show_disabled_consumers    | PROCEDURE |
| sys | ps_setup_show_disabled_instruments  | PROCEDURE |
| sys | ps_setup_show_enabled               | PROCEDURE |
| sys | ps_setup_show_enabled_consumers     | PROCEDURE |
| sys | ps_setup_show_enabled_instruments   | PROCEDURE |
| sys | ps_statement_avg_latency_histogram  | PROCEDURE |
| sys | ps_thread_account                   | FUNCTION  |
| sys | ps_thread_id                        | FUNCTION  |
| sys | ps_thread_stack                     | FUNCTION  |
| sys | ps_thread_trx_info                  | FUNCTION  |
| sys | ps_trace_statement_digest           | PROCEDURE |
| sys | ps_trace_thread                     | PROCEDURE |
| sys | ps_truncate_all_tables              | PROCEDURE |
| sys | quote_identifier                    | FUNCTION  |
| sys | statement_performance_analyzer      | PROCEDURE |
| sys | sys_get_config                      | FUNCTION  |
| sys | table_exists                        | PROCEDURE |
| sys | version_major                       | FUNCTION  |
| sys | version_minor                       | FUNCTION  |
| sys | version_patch                       | FUNCTION  |
+-----+-------------------------------------+-----------+
 
4. 存储过程参数的使用
参数类型
MySQL存储过程,共有三种参数类型IN,OUT,INOUT
Create procedure 名称(
类型 参数名 数据类型,
类型 参数名 数据类型

in输入参数 传递值给存储过程,必须在调用存储过程时指定,在存储过程中不能修改该参数的值;默认类型是in
out输出参数该值可在存储过程内部被改变,并可返回
inout输入/输出参数调用时指定,并且可被改变和返回
 
案例1:
mysql> delimiter //
mysql> create procedure say2(in name char(10))
    -> begin
    -> select name;  //输出变量
    -> select * from user where username=name;
    -> end
    -> //
mysql> delimiter ;
mysql> call say2("ftp");
+------+
| name |
+------+
| ftp  |
+------+

+----+----------+----------+------+------+----------+----------+---------------+
| id | username | password | uid  | gid  | comment  | homedir  | shell         |
+----+----------+----------+------+------+----------+----------+---------------+
| 12 | ftp      | x        |   14 |   50 | FTP User | /var/ftp | /sbin/nologin |
+----+----------+----------+------+------+----------+----------+---------------+

案例2:
mysql> delimiter //
mysql> create procedure p2(out number int)
    -> begin
    -> select count(username) into @number from user where shell!="/bin/bash";
    -> select @number;  
    -> end
    -> //
mysql> delimiter ;
mysql> call p2(@number);
+---------+
| @number |
+---------+
|      17 |
+---------+

5.使用循环结构
5.1 算数运算
mysql> set @z=1+2;select @z;
+------+
| @z   |
+------+
|    3 |
+------+
 
mysql> set @x=1; set @y=2;set @z=@x*@y; select @z;
+------+
| @z   |
+------+
|    2 |
+------+
 
mysql> set @x=1; set @y=2;set @z=@x/@y; select @z;
+-------------+
| @z          |
+-------------+
| 0.500000000 |
+-------------+
 
declare调用变量不需要@,其他都需要
调用变量时,有@符号的变量 如@x:调用的是用户自定义变量(out的)
没有@符号的变量 如x:调用的是存储过程的参数变量
mysql> delimiter //
mysql> create procedure say5(in bash char(20), in nologin char(25), out x int , out y int)
//定义参数变量bash,nologin,用户自定义变量x,y,存储过程的参数变量z
    -> 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;     //输出z
    -> end
    ->  //
mysql> delimiter ;
mysql> call say5("/bin/bash","/sbin/nologin",@x,@y);
+------+
| z    |
+------+
|   14 |
+------+
 
5.2 条件判断,数值的比较如图-2所示:
31_MySQL视图 MySQL存储过程_第3张图片

5.3 逻辑比较、范围、空、非空、模糊、正则,如图-3所示:
31_MySQL视图 MySQL存储过程_第4张图片

顺序结构(if判断)当“条件成立”时执行命令序列,否则,不执行任何操作
mysql> delimiter //
mysql> create procedure say7(in x int(1) )
    -> begin
    -> if x <= 10 then
    -> select * from user where id <=x;
    -> end if;
    -> end
    -> //
mysql> delimiter ;
mysql> call say7(1);   //条件判断成立,等于1是否成立
+----+----------+----------+------+------+---------+---------+-----------+
| id | username | password | uid  | gid  | comment | homedir | shell     |
+----+----------+----------+------+------+---------+---------+-----------+
|  1 | root     | x        |    0 |    0 | root    | /root   | /bin/bash |
+----+----------+----------+------+------+---------+---------+-----------+
 
mysql> call say7(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 |
+----+----------+----------+------+------+---------+---------+---------------+\

call say7(11);  //查询不到
Query OK, 0 rows affected (0.00 sec)
 
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
    -> //
mysql> delimiter ;
mysql> call p3(null);   //不输入查看的行数
+----+----------+----------+------+------+---------+---------+-----------+
| id | username | password | uid  | gid  | comment | homedir | shell     |
+----+----------+----------+------+------+---------+---------+-----------+
|  1 | root     | x        |    0 |    0 | root    | /root   | /bin/bash |
+----+----------+----------+------+------+---------+---------+-----------+
 
mysql> call p3(3);
+---------+
| linenum |
+---------+
| 3       |
+---------+

+----+----------+----------+------+------+---------+---------+---------------+
| id | username | password | uid  | gid  | comment | homedir | shell         |
+----+----------+----------+------+------+---------+---------+---------------+
|  3 | daemon   | x        |    2 |    2 | daemon  | /sbin   | /sbin/nologin |
+----+----------+----------+------+------+---------+---------+---------------+

你可能感兴趣的:(31_MySQL视图 MySQL存储过程)