1)创建视图:
语法格式:
create view 视图名称 as SQL查询;
create view 视图名称(字段名称列表) as SQL查询;
2)查看视图:
查看当前库下所有表的状态信息
show table status;
show table status where ccomment=“view”\G;
查看创建视图具体命令:
show create view 视图名;
例子:
先创建数据库数据的库和表做模版
mysql> create database db9;
mysql>create table user(
> name char(30),
> password char(1),
> uid int,
> gid int,
> comment char(150),
> homedir char(150),
> shell char(50),
> index(name));
系统命令system
mysql> system cp /etc/passwd /mydir //使用系统命令拷贝/etc/passwd到/mydir
mysql> system ls /mydir
mysql> load data infile "/mydir/passwd"
> into table db9.user
> fields terminated by ":"
> lines terminated by "\n";
创建视图:
mysql> creare view u(v_name,v_uid) as select * from user where name="mail" and uid=8;
mysql> select * from u;
+--------+-------+
| v_name | v_uid |
+--------+-------+
| mail | 8 |
+--------+-------+
3)视图的查询、插入、更新、删除
语法格式:
select 字段名列表 from 视图名 where 条件;
insert into 视图名(字段名列表) values(字段值列表);
update 视图名 set 字段名=值 where 条件;
delete from 视图名 where 条件;
视图即虚拟表与物理表两者可为相互同步,一旦物理表删除则虚拟表无法进行增删改查。
3)删除视图
语法格式:
drop view 视图名;
mysql> create table user02 select name,uid,gid from user limit 3; //使用user表前3行快速建user02表
mysql> select * from user02;
+--------+------+------+
| name | uid | gid |
+--------+------+------+
| root | 0 | 0 |
| bin | 1 | 1 |
| daemon | 2 | 2 |
+--------+------+------+
mysql> create table user03 select name,uid,homedir,shell from user limit 5; //使用user表前5行快速建user03表
mysql> select * from user03;
+--------+------+----------------+---------------+
| name | uid | homedir | shell |
+--------+------+----------------+---------------+
| root | 0 | /root | /bin/bash |
| bin | 1 | /bin | /sbin/nologin |
| daemon | 2 | /sbin | /sbin/nologin |
| adm | 10 | /var/adm | /sbin/nologin |
| lp | 4 | /var/spool/lpd | /sbin/nologin |
+--------+------+----------------+---------------+
查询user02.name=user03.name的字段
mysql> select * from user02 left join user03 on user02.name=user03.name;
+--------+------+------+--------+------+---------+---------------+
| name | uid | gid | name | 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 |
+--------+------+------+--------+------+---------+---------------+
2)关联查询建的视图,默认不允许修改视图字段的值
mysql> create view v01 as select * from user02 left join user03 on user02.name=user03.name; //创建失败
ERROR 1060 (42S21): Duplicate column name 'name'
//默认不允许修改视图字段的值
3)创建视图时指定字段别名
方法一:
mysql> create view v01(a,b,c,d,e,f,g) as select * from
> user02,user03 where
> user02.name=user03.name and user02.uid=user03.uid;
Query OK, 0 rows affected (0.05 sec)
mysql> select * from v01;
±-------±-----±-----±-------±-----±------±--------------+
| a | b | c | d | e | f | g |
±-------±-----±-----±-------±-----±------±--------------+
| 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 user02.name as a,
-> user03.name as d,
-> user03.homedir as f,user03.shell as g from user02,user03
-> where user02.name=user03.name;
+--------+--------+-------+---------------+
| a | d | f | g |
+--------+--------+-------+---------------+
| root | root | /root | /bin/bash |
| bin | bin | /bin | /sbin/nologin |
| daemon | daemon | /sbin | /sbin/nologin |
+--------+--------+-------+---------------+
查询时给表起别名:
mysql> create view v02 as select
-> a.name as aname,b.name as bname,a.uid as auid,
-> b.uid as buid
-> from user02 a left join user03 b on a.uid=b.uid;
Query OK, 0 rows affected (0.04 sec)
mysql> select * from v02;
+--------+--------+------+------+
| aname | bname | auid | buid |
+--------+--------+------+------+
| root | root | 0 | 0 |
| bin | bin | 1 | 1 |
| daemon | daemon | 2 | 2 |
+--------+--------+------+------+
3 rows in set (0.00 sec)
存储过程,相当于MySQL语句组成的脚本
—指的是数据库中保存的一系列SQL命令的集合
—可以在存储过程中使用变量、条件判断、流程控制等存储过程优点
提高性能
可减轻网络负担
可以防止对表的直接访问
避免重复编写SQL操作
存储过程的基本使用
1)创建存储过程
语法格式:
mysql> delimiter //
mysql> create procedure 名称()
mysql> begin
mysql> …功能代码
mysql> end
mysql> // //结束存储过程
mysql> Delimiter;
例如:
mysql> delimiter //
mysql> create procedure db9.say()
-> begin
-> select name,uid from db9.user where id<=7;
-> select * from db9.t1;
-> end
-> //
Query OK, 0 rows affected (0.03 sec)
mysql> delimiter ;
2)调用
语法格式:
call 存储过程名();
mysql> call db9.say();
+----------+------+
| name | uid |
+----------+------+
| root | 0 |
| bin | 1 |
| daemon | 2 |
| adm | 10 |
| lp | 4 |
| sync | 5 |
| shutdown | 6 |
+----------+------+
7 rows in set (0.00 sec)
+--------+------+---------------+
| name | uid | shell |
+--------+------+---------------+
| root | 0 | /bin/bash |
| bin | 1 | /sbin/nologin |
| daemon | 3 | /sbin/nologin |
+--------+------+---------------+
3 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
3)删除
语法格式:
mysql> drop procedure 存储过程名;
mysql> drop procedure say;
Query OK, 0 rows affected (0.00 sec)
mysql> call say;
ERROR 1305 (42000): PROCEDURE db9.say does not exist
4)查看存储过程
方法一:
mysql> show procedure status\G;
方法二:
格式:
mysql> select db,name,type from mysql.proc where name=“存储过程名”;
例如:
mysql> select db,name,type from mysql.proc where name="say";
+-----+------+-----------+
| db | name | type |
+-----+------+-----------+
| db9 | say | PROCEDURE |
+-----+------+-----------+
1 row in set (0.00 sec)
5)查看过程的具体代码
mysql> select db,name,type from mysql.proc
> where type="PROCEDURE" and name="say";
6)查看有哪些存储过程
mysql> select db,name,type from mysql.proc
> where type="PROCEDURE" and name="say";
练习:
存储过程名称为p1
功能显示user表中shell 是/bin/bash和/bin/nologin的用户个数
调用存储过程p1
mysql> delimiter //
mysql> create procedure p1()
-> begin
-> select count(*) from user where shell="/bin/bash";
-> select count(*) from user where shell="/bin/nologin";
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call p1;
+----------+
| count(*) |
+----------+
| 2 |
+----------+
1 row in set (0.00 sec)
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables; //查看全局变量
mysql> show session variables; //查看会话变量
mysql> set session sort_buffer_size = 4000; //设置会话变量
mysql> show session variables like "sort_buffer_size"; //查看会话变量
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| sort_buffer_size | 262144 |
+------------------+--------+
1 row in set (0.00 sec)
mysql> show global variables like "%关键字%"; //查看全局变量
mysql> show global variables like "%host%";
+-------------------------------+----------+
| Variable_name | Value |
+-------------------------------+----------+
| host_cache_size | 279 |
| hostname | mysql155 |
| performance_schema_hosts_size | -1 |
| report_host | |
+-------------------------------+----------+
mysql> set @y=3; //用户自定义变量,直接赋值
mysql> select @y;
+------+
| @y |
+------+
| 3 |
+------+
mysql> select max(uid) into @y from user; //使用SQL命令查询结果赋值,将user表中uid的最大值赋值给自定义变量
mysql> select @y;
+-------+
| @y |
+-------+
| 65534 |
+-------+
调用参数时,名称前也不需要加@
–create procedure 名称(
类型 参数名 数据类型,
类型 参数名 数据类型
)
begin
…
in关键字:
mysql> create procedure conf(in a int,in b int)
-> begin
-> declare x int;
-> declare y int;
-> declare z int;
-> set x = a;
-> set y = b;
-> set z = x + y;
-> select z;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call conf(10,10);
+------+
| z |
+------+
| 20 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
out关键字:
mysql> create procedure sy(out x int)
-> begin
-> select count(uid) into x from db9.user;
-> select x;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> select @j;
+------+
| @j |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
mysql> call sy(@j);
+------+
| x |
+------+
| 41 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select @j;
+------+
| @j |
+------+
| 41 |
+------+
1 row in set (0.00 sec)
inout关键字连用:
mysql> delimiter //
mysql> create procedure er(inout x int)
-> begin
-> select x;
-> select count(name) into x from db9.user where shell="/bin/bash";
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> select @i;
+------+
| @i |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
mysql> call er(@i);
+------+
| x |
+------+
| 2 |
+------+
1 row in set (0.01 sec)
Query OK, 1 row affected (0.01 sec)
mysql> select @i;
+------+
| @i |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
练习:使用inout指定查看用户
mysql> delimiter //
mysql> create procedure qw(in username varchar(10))
-> begin
-> select username;
-> select * from user where name=username;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call qw("root");
+----------+
| username |
+----------+
| root |
+----------+
1 row in set (0.00 sec)
+----+------+----------+------+------+---------+---------+-----------+
| id | name | password | uid | gid | comment | homedir | shell |
+----+------+----------+------+------+---------+---------+-----------+
| 1 | root | x | 0 | 0 | root | /root | /bin/bash |
+----+------+----------+------+------+---------+---------+-----------+
1 row in set (0.00 sec)
3)算数运算
定义局部变量x,y,z要求如下:
—x统计字段shell=/bin/bash的个数
—y统计字段shell=/sbin/nologin的个数
—z统计x,y的总数。
mysql> delimiter //
mysql> create procedure co()
-> begin
-> declare x int default 0;
-> declare y int default 0;
-> declare z int default 0;
-> select count(*) into x from db9.user where shell="/bin/bash";
-> select count(*) into y from db9.user where shell="/sbin/nologin";
-> set z = x + y;
-> select z;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call co;
+------+
| z |
+------+
| 37 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
create procedure if not exits … //覆盖创建