视图是什么?
一张虚表,和真实的表一样。视图包含一系列带有名称的行和列数据。视图是从一个或多个表中导出来的,我们可以通过insert,update,delete来操作视图。当通过视图看到的数据被修改时,相应的原表的数据也会变化。同时原表发生变化,则这种变化也可以自动反映到视图中。
视图的优点
- 简单化:看到的就是需要的。视图不仅可以简化用户对数据的理解,也可以简化操作。经常被使用的查询可以制作成一个视图
- 安全性:通过视图用户只能查询和修改所能见到的数据,数据库中其他的数据既看不见也取不到。数据库授权命令可以让每个用户对数据库的检索限制到特定的数据库对象上,但不能授权到数据库特定的行,列上。
- 逻辑数据独立性:视图可帮助用户屏蔽真实表结构变化带来的影响
创建视图
语法格式:
1. ALTER 2. [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] 3. [DEFINER = user] 4. VIEW view_name [(column_list)] 5. AS select_statement 6. [WITH [CASCADED | LOCAL] CHECK OPTION]
注释:
Undefined:不常用。
merge :表示将使用的视图语句与视图定义合并起来,使视图定义的某一部分取代语句对应的部分
temptable :表示将视图的结果存入临时表,然后用临时表来执行语句with[cascaded|local]
Cascaded :默认为cascaded,表示更新视图时,满足所有相关视图和表的条件
Local :表示更新视图时,满足该视图本身定义的条件即可
创建单表视图
1. mysql> create table t(quantity int,price int); 2. Query OK, 0 rows affected (0.02 sec) 3. 4. mysql> insert into t values(3,50); 5. Query OK, 1 row affected (0.00 sec) 6. 7. mysql> create view view_t as select quantity,price,quantity*price from t; #创建view_t视图 8. Query OK, 0 rows affected (0.01 sec)
查看
1. mysql> select * from view_t; 2. +----------+-------+----------------+ 3. | quantity | price | quantity*price | 4. +----------+-------+----------------+ 5. | 3 | 50 | 150 | 6. +----------+-------+----------------+ 7. 1 row in set (0.00 sec) 8. mysql> create view view_t2(qty,price,total) as 9. -> select quantity,price,quantity*price 10. -> from t; 11. Query OK, 0 rows affected (0.00 sec) 12. 13. mysql> select * from view_t2; 14. +------+-------+-------+ 15. | qty | price | total | 16. +------+-------+-------+ 17. | 3 | 50 | 150 | 18. +------+-------+-------+ 19. 1 row in set (0.00 sec)
创建多表视图
1. mysql> create table student( 2. -> s_id int primary key, 3. -> s_name varchar(30), 4. -> s_age int, 5. -> s_sex varchar(8)); 6. Query OK, 0 rows affected (0.00 sec) 7. 8. mysql> create table stu_info( 9. -> s_id int, 10. -> class varchar(50), 11. -> addr varchar(100)); 12. Query OK, 0 rows affected (0.00 sec) 13. 14. mysql> insert into stu_info(s_id,class,addr) 15. -> values 16. -> (1,'erban','anhui'), 17. -> (2,'sanban','chongqing'), 18. -> (3,'yiban','shangdong'); 19. Query OK, 3 rows affected (0.01 sec) 20. Records: 3 Duplicates: 0 Warnings: 0 21. 22. mysql> create view stu_class(id,name,class) as select student.s_id,student.s_name,stu_info.class from student,stu_info where student.s_id=stu_info.s_id; 23. Query OK, 0 rows affected (0.00 sec)
查看视图的结构信息
语法格式:Describe 视图名
1. mysql> desc stu_class; 2. +-------+-------------+------+-----+---------+-------+ 3. | Field | Type | Null | Key | Default | Extra | 4. +-------+-------------+------+-----+---------+-------+ 5. | id | int(11) | NO | | NULL | | 6. | name | varchar(30) | YES | | NULL | | 7. | class | varchar(50) | YES | | NULL | | 8. +-------+-------------+------+-----+---------+-------+ 9. 3 rows in set (0.01 sec)
查看视图的基本信息
语法格式:Show table status like ‘视图名’\G;
1. mysql> show table status like 'stu_class'\G 2. *************************** 1. row *************************** 3. Name: stu_class 4. Engine: NULL 5. Version: NULL 6. Row_format: NULL 7. Rows: NULL 8. Avg_row_length: NULL 9. Data_length: NULL 10. Max_data_length: NULL 11. Index_length: NULL 12. Data_free: NULL 13. Auto_increment: NULL 14. Create_time: NULL 15. Update_time: NULL 16. Check_time: NULL 17. Collation: NULL 18. Checksum: NULL 19. Create_options: NULL 20. Comment: VIEW 21. 1 row in set (0.01 sec)
查看视图的详细信息
语法格式:Show create view 视图名;
或show create view 视图名\G
1. mysql> show create view view_t\G 2. *************************** 1. row *************************** 3. View: view_t 4. Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_t` AS select `t`.`quantity` AS `quantity`,`t`.`price` AS `price`,(`t`.`quantity` * `t`.`price`) AS `quantity*price` from `t` 5. character_set_client: utf8 6. collation_connection: utf8_general_ci 7. 1 row in set (0.00 sec)
在mysql中,information schema 数据库下的views表中存储了所有视图的定义。通过对views表的查询,可以查看数据库中所有视图的详细信息。
mysql> select * from information_schema.views\G
修改视图
语法格式:
1. ALTER 2. VIEW view_name [(column_list)] 3. AS select_statement 4. [WITH [CASCADED | LOCAL] CHECK OPTION] 5. mysql> create or replace view view_t as select * from t; 6. Query OK, 0 rows affected (0.00 sec)
1. mysql> desc view_t; 2. +----------+---------+------+-----+---------+-------+ 3. | Field | Type | Null | Key | Default | Extra | 4. +----------+---------+------+-----+---------+-------+ 5. | quantity | int(11) | YES | | NULL | | 6. | price | int(11) | YES | | NULL | | 7. +----------+---------+------+-----+---------+-------+ 8. 2 rows in set (0.00 sec)
修改视图
1. mysql> alter view view_t as select quantity from t; 2. Query OK, 0 rows affected (0.00 sec) 3. 4. mysql> desc view_t; 5. +----------+---------+------+-----+---------+-------+ 6. | Field | Type | Null | Key | Default | Extra | 7. +----------+---------+------+-----+---------+-------+ 8. | quantity | int(11) | YES | | NULL | | 9. +----------+---------+------+-----+---------+-------+ 10. 1 row in set (0.00 sec)
更新视图
- update
1. mysql> select *from t; 2. +----------+-------+ 3. | quantity | price | 4. +----------+-------+ 5. | 3 | 50 | 6. +----------+-------+ 7. 1 row in set (0.00 sec) 8. 9. mysql> select * from view_t; 10. +----------+ 11. | quantity | 12. +----------+ 13. | 3 | 14. +----------+ 15. 1 row in set (0.00 sec) 16. 17. mysql> update view_t set quantity=5; #这个操作是quantity列全部都为5,加where可以指定某行 18. Query OK, 1 row affected (0.00 sec) 19. Rows matched: 1 Changed: 1 Warnings: 0 20. 21. mysql> select *from view_t; 22. +----------+ 23. | quantity | 24. +----------+ 25. | 5 | 26. +----------+ 27. 1 row in set (0.00 sec) 28. 29. mysql> select * from t; 30. +----------+-------+ 31. | quantity | price | 32. +----------+-------+ 33. | 5 | 50 | 34. +----------+-------+ 35. 1 row in set (0.00 sec)
- Insert
1. mysql> insert into t values(3,5); 2. Query OK, 1 row affected (0.00 sec) 3. 4. mysql> select * from t; 5. +----------+-------+ 6. | quantity | price | 7. +----------+-------+ 8. | 5 | 50 | 9. | 3 | 5 | 10. +----------+-------+ 11. 2 rows in set (0.00 sec) 12. 13. mysql> select * from view_t2; 14. +------+-------+-------+ 15. | qty | price | total | 16. +------+-------+-------+ 17. | 5 | 50 | 250 | 18. | 3 | 5 | 15 | 19. +------+-------+-------+ 20. 2 rows in set (0.00 sec)
- delete
1. mysql> select * from view_t2; 2. +------+-------+-------+ 3. | qty | price | total | 4. +------+-------+-------+ 5. | 5 | 50 | 250 | 6. | 3 | 5 | 15 | 7. +------+-------+-------+ 8. 2 rows in set (0.00 sec) 9. 10. mysql> delete from view_t2 where price=5; 11. Query OK, 1 row affected (0.00 sec) 12. 13. mysql> select * from view_t2; 14. +------+-------+-------+ 15. | qty | price | total | 16. +------+-------+-------+ 17. | 5 | 50 | 250 | 18. +------+-------+-------+ 19. 1 row in set (0.00 sec)
视图存在以下情况时,更新操作无法执行
- 视图中不包含基表中被定义为非空的列
- 在定义视图的select语句后的字段列表中使用了数学表达式
- 在定义视图的select 语句后字段列表中使用了聚合函数时不接受更新操作
- select中,使用了union \top \group by 或having 无法接受
删除视图
语法格式:
DROP VIEW [IF EXISTS]
view_name1 [, view_name2] ...
1. mysql> drop view if exists stu_class; 2. Query OK, 0 rows affected (0.00 sec) 3. 4. mysql> drop view if exists stu_class; 5. Query OK, 0 rows affected, 1 warning (0.00 sec) 6. 7. mysql> show warnings; 8. +-------+------+-------------------------------+ 9. | Level | Code | Message | 10. +-------+------+-------------------------------+ 11. | Note | 1051 | Unknown table 'bbs.stu_class' | 12. +-------+------+-------------------------------+ 13. 1 row in set (0.00 sec) 14. 15. mysql> show create view stu_class; 16. ERROR 1146 (42S02): Table 'bbs.stu_class' doesn't exist
扩展
MySQL中视图和表的区别以及联系是什么?
1、两者的区别:
(1)视图是已经编译好的SQL语句,是基于SQL语句的结果集的可视化的表,而表不是。
(2)视图没有实际的物理记录,而表有。
(3)表是内容,视图窗口
(4)表和视图虽然都占用物理空间,但是视图只是逻辑概念存在,而表可以及时对数据进行修改,但是视图只能用创建语句来修改
(5)视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL 语句的集合。从安全角度来说,视图可以防止用户接触数据表,因而不知道表结构
(6)表属于全局模式中的表,是实表。而视图属于局部模式的表,是虚表
(7)视图的建立和删除只影响视图本身,而不影响对应表的基本表
2、两者的联系
视图是在基本表之上建立的表,它的结构和内容都来自于基本表,它依赖基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基本的抽象和逻辑意义上建立的关系。