视图(View)是一个虚拟表,其内容由查询定义。同真实的表一样,视图包含一系列带有名称的列和行数据。但是,视图并不在数据库中以存储的数据值集形式存在。行和列数据来自由定义视图的查询所引用的表,并且在引用视图时动态生成。
视图其实就是一个select返回的结果集,用于方便我们查询而创建的"临时表",简化我们的查询语句。
Tips:使用视图的大部分情况是为了保障数据安全性,提高查询效率;
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
insert into `class`(`id`,`name`) values
(1,'Java'),
(2,'UI'),
(3,'产品');
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`class_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
/*Data for the table `student` */
insert into `student`(`id`,`name`,`class_id`) values
(1,'张三',1),
(2,'李四',1),
(3,'王五',2),
(4,'赵六',1),
(5,'孙七',3),
(6,'王八',3);
CREATE [OR REPLACE]
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
-- 创建视图
create view test1 as select * from student;
-- 创建视图,如果该视图存在那么就覆盖
create or replace view test1 as select * from student;
mysql> select * from test1;
+----+------+----------+
| id | name | class_id |
+----+------+----------+
| 1 | 张三 | 1 |
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 孙七 | 3 |
| 6 | 王八 | 3 |
+----+------+----------+
6 rows in set (0.00 sec)
mysql>
需要注意的是:对视图进行任何的新增、修改、删除将会对直接作用到源表
mysql> update test1 set name='zhangsan' where id=1; # 修改视图中的记录
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from test1; # 查看视图
+----+----------+----------+
| id | name | class_id |
+----+----------+----------+
| 1 | zhangsan | 1 |
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 孙七 | 3 |
| 6 | 王八 | 3 |
+----+----------+----------+
6 rows in set (0.00 sec)
mysql> select * from student; # 查看源表,发现源表的数据也修改了
+----+----------+----------+
| id | name | class_id |
+----+----------+----------+
| 1 | zhangsan | 1 |
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 孙七 | 3 |
| 6 | 王八 | 3 |
+----+----------+----------+
6 rows in set (0.00 sec)
mysql>
mysql> delete from test1 where id=1; # 删除视图中的数据
Query OK, 1 row affected (0.01 sec)
mysql> select * from test1;
+----+------+----------+
| id | name | class_id |
+----+------+----------+
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 孙七 | 3 |
| 6 | 王八 | 3 |
+----+------+----------+
5 rows in set (0.00 sec)
mysql> select * from student; # 源表的数据也被删除了
+----+------+----------+
| id | name | class_id |
+----+------+----------+
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 孙七 | 3 |
| 6 | 王八 | 3 |
+----+------+----------+
5 rows in set (0.00 sec)
mysql>
mysql> insert into test1 values(1,'张三',1); # 在视图中插入一条新的记录
Query OK, 1 row affected (0.01 sec)
mysql> select * from test1;
+----+------+----------+
| id | name | class_id |
+----+------+----------+
| 1 | 张三 | 1 |
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 孙七 | 3 |
| 6 | 王八 | 3 |
+----+------+----------+
6 rows in set (0.00 sec)
mysql> select * from student; # 源表中也新增了一条记录
+----+------+----------+
| id | name | class_id |
+----+------+----------+
| 1 | 张三 | 1 |
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 孙七 | 3 |
| 6 | 王八 | 3 |
+----+------+----------+
6 rows in set (0.00 sec)
mysql>
Tips:视图实质上是对源表的引用,对视图的任何操作都会落实到对应的表,如insert、update、delete等语句,drop删除视图不会影响源表,但源表如果被删除了(drop),那么视图将变得不可用。
同样,对源表进行任何的insert、update、delete等语句,对应的视图上的记录也会发生变化;
mysql> select * from test1; # 查询test1视图中的记录
+----+------+----------+
| id | name | class_id |
+----+------+----------+
| 1 | 张三 | 1 |
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 孙七 | 3 |
| 6 | 王八 | 3 |
+----+------+----------+
6 rows in set (0.00 sec)
mysql> update student set name='zs' where id=1; # 对student(源表)进行修改
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> insert into student values(100,'Test',3); # 对student(源表)进行插入
Query OK, 1 row affected (0.00 sec)
mysql> delete from student where id=6; # 对student(源表)进行删除
Query OK, 1 row affected (0.00 sec)
mysql> select * from test1; # 查询test1视图
+-----+------+----------+
| id | name | class_id |
+-----+------+----------+
| 1 | zs | 1 |
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 孙七 | 3 |
| 100 | Test | 3 |
+-----+------+----------+
6 rows in set (0.00 sec)
mysql>
CREATE [OR REPLACE]
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
create or replace view test2 as select * from student where class_id=1;
重新插入测试数据:
truncate student;
insert into `student`(`id`,`name`,`class_id`) values
(1,'张三',1),
(2,'李四',1),
(3,'王五',2),
(4,'赵六',1),
(5,'孙七',3),
(6,'王八',3);
ALTER
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { USER | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
alter view test2 as select * from student where class_id=3;
alter view test2 as select id,name from student;
alter view test2 as select * from student where name='张三';
mysql> select * from test2;
+----+------+----------+
| id | name | class_id |
+----+------+----------+
| 1 | 张三 | 1 |
+----+------+----------+
1 row in set (0.00 sec)
mysql> select * from student;
+----+------+----------+
| id | name | class_id |
+----+------+----------+
| 1 | 张三 | 1 |
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 孙七 | 3 |
| 6 | 王八 | 3 |
+----+------+----------+
6 rows in set (0.00 sec)
mysql> update test2 set name='zs' where id=1; # 修改视图,源表和视图都会更新
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from test2;
Empty set (0.00 sec)
mysql> select * from student;
+----+------+----------+
| id | name | class_id |
+----+------+----------+
| 1 | zs | 1 |
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 孙七 | 3 |
| 6 | 王八 | 3 |
+----+------+----------+
6 rows in set (0.00 sec)
mysql>
注意:创建视图时,有下列内容之一,视图将不能做DML操作:
删除视图时,只能删除视图的定义,不会删除数据,也就是说不动基表
DROP VIEW [IF EXISTS] view_name [, view_name] ...
drop view test1,test2;
重新插入测试数据:
truncate student;
insert into `student`(`id`,`name`,`class_id`) values
(1,'张三',1),
(2,'李四',1),
(3,'王五',2),
(4,'赵六',1),
(5,'孙七',3),
(6,'王八',3);
CREATE [OR REPLACE]
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
create view a1 as select * from student; -- 创建视图
create algorithm=merge view a2 as select * from a1; -- 创建视图
create algorithm=temptable view a3 as select * from a1; -- 创建视图
需要注意的是,视图创建算法如果是tamptable,那么该视图不允许修改:
mysql> update a2 set name='zs' where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update a3 set name='ls' where id=2; # 不允许修改
ERROR 1288 (HY000): The target table a3 of the UPDATE is not updatable
mysql>
-- 创建d1视图,创建者默认为当前用户
create view d1 as select * from student;
-- 创建用户
create user 'xiaohui'@'localhost' identified by 'admin';
-- 创建视图,指定创建者为xiaohui用户
create definer='xiaohui'@'localhost' view d2 as select * from student;
-- 查看视图d1的创建语句
show create view d1;
-- 查看视图d2的创建语句
show create view d2;
create definer='root'@'localhost' sql security invoker view test_01 as -- invoker权限
select * from student;
create definer='root'@'localhost' sql security definer view test_02 as -- definer权限
select * from student;
-- 创建zhangsan用户
create user 'zhangsan'@'%' identified by 'admin';
-- 赋予访问视图的权限
grant select on db04.test_01 to zhangsan;
grant select on db04.test_02 to zhangsan;
-- 查看zhangsan用户的权限
show grants for 'zhangsan'@'%';
mysql> select * from test_01; # 不能访问test_01视图
ERROR 1356 (HY000): View 'test.test_01' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
mysql> select * from test_02; # 可以访问test02视图
+----+------+----------+
| id | name | class_id |
+----+------+----------+
| 1 | 张三 | 1 |
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 钱七 | 3 |
| 6 | 王八 | 3 |
+----+------+----------+
5 rows in set (0.00 sec)
Tips:发现不能访问test_01视图;因为zhangsan用户并没有student表的访问权限;
grant select on test.student to zhangsan;
mysql> select * from test_01;
+----+------+----------+
| id | name | class_id |
+----+------+----------+
| 1 | 张三 | 1 |
| 2 | 李四 | 1 |
| 3 | 王五 | 2 |
| 4 | 赵六 | 1 |
| 5 | 钱七 | 3 |
| 6 | 王八 | 3 |
+----+------+----------+
5 rows in set (0.00 sec)
测试完毕删除用户:
drop user 'zhangsan'@'%';
重新插入测试数据:
truncate student;
insert into `student`(`id`,`name`,`class_id`) values
(1,'张三',1),
(2,'李四',1),
(3,'王五',2),
(4,'赵六',1),
(5,'孙七',3),
(6,'王八',3);
【普通检查策略】:
-- 创建一个普通视图
create view v1 as select * from student where class_id=1;
-- 创建一个带数据检查的视图
create view v2 as select * from student where class_id=1 with check option;
-- 修改v1视图,修改之后的数据已经不符合当初创建v1视图的条件了,但依旧可以修改
update v1 set class_id=2 where id=1;
-- 修改v2视图,修改之后的数据已经不符合当初创建v1视图的条件了,修改失败
update v2 set class_id=2 where id=2;
重新创建表:
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(11),
`class_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
/*Data for the table `student` */
insert into `student` values
(1,'张三',20,1),
(2,'李四',25,1),
(3,'王五',30,2),
(4,'赵六',35,1),
(5,'孙七',40,3),
(6,'王八',45,3);
【CASCADED/LOCAL检查策略】:
-- 没有检查策略
create view v1 as select * from student where class_id=1;
-- 普通检查策略
create view v2 as select * from v1 where age<=40 with check option;
-- local检查策略
create view v3 as select * from v1 where age<=40 with local check option;
-- cascaded检查策略
create view v4 as select * from v1 where age<=40 with cascaded check option;
-- v1视图没有任何检查策略,任意的insert都可以执行成功
insert into v1 values(100,'aa',80,2);
-- v2具备普通检查策略,插入的数据需要符合当初创建v2时的条件,也要符合创建v1的条件
insert into v2 values(300,'aa',20,1); -- 既符合v1,也符合v2,成功
insert into v2 values(400,'aa',20,2); -- 不符合v1的条件,错误
insert into v2 values(400,'aa',50,1); -- 不符合v2的条件,错误
-- v3具备local检查策略,插入的数据需要符合创建自身视图时的条件即可
insert into v3 values(500,'aa',20,1); -- 既符合v1,也符合v3,成功
insert into v3 values(600,'aa',20,2); -- 只符合v3,成功
insert into v3 values(700,'aa',50,1); -- 不符合v3,错误
-- v4具备cascaded检查策略,插入视图的数据需要当初创建v4的条件,也要符合创建v1的条件
insert into v4 values(800,'aa',20,1); -- 既符合v1,也符合v4,成功
insert into v4 values(900,'aa',20,2); -- 不符合v1,错误
insert into v4 values(1000,'aa',50,1); -- 不符合v4,错误