一、什么是视图

  • 视图是一个虚拟表,其内容由select查询语句定义。
  • 和真实的表一样,视图也包含行和列,对视图的操作与对表的操作基本一致
  • 视图中的数据是在使用视图时动态生成,视图中的数据都存储在基表中。视图就好像一张或多张基表的对外接口
  • 视图的作用

二、视图的基本操作

  • 视图中的数据都存储在基表中。
  • 基表的数据变化也会影响视图表
  • 视图表的数据变化也会影响到基表,但是尽量不要直接修改视图
  • 语法
#创建视图
CREATE VIEW view_name AS SELECT…;
#修改视图
ALTER VIEW view_name AS SELECT…;
#查看视图创建语句
SHOW CREATE VIEW view_name;
#查看有哪些视图
SHOW TABLE STATUS WHERE comment='view';
#删除视图
DROP VIEW view_name;

三、示例

  • 先看一下基础的数据
mysql> select * from student;
+---------+--------+---------+
| stu_no | name | address |
+---------+--------+---------+
| 2016001 | 张三 | 贵州 |
| 2016002 | 李芳 | 陕西 |
| 2016003 | 张晓燕 | 江西 |
+---------+--------+---------+
3 rows in set (0.14 sec)

mysql> select * from score;
+----+--------+---------+-------+
| id | course | stu_no | score |
+----+--------+---------+-------+
| 1 | 计算机 | 2016001 | 99 |
| 2 | 离散 | 2016001 | 85 |
| 3 | 计算机 | 2016002 | 78 |
+----+--------+---------+-------+
3 rows in set (0.10 sec)
  • 创建单表视图
mysql> create view student_view as select * from student;
Query OK, 0 rows affected (0.52 sec)

# show tables;可以同时看到基表和视图
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| area |
| bank_account |
| employee |
| score |
| student |
| student_view |
+----------------+
6 rows in set (0.00 sec)

# 可以用查表相同的方法查视图
mysql> select * from student_view;
+---------+--------+---------+
| stu_no | name | address |
+---------+--------+---------+
| 2016001 | 张三 | 贵州 |
| 2016002 | 李芳 | 陕西 |
| 2016003 | 张晓燕 | 江西 |
+---------+--------+---------+
3 rows in set (0.30 sec)

# 用show table status where comment = 'view'查看视图
mysql> show table status where comment = 'view'\G;
*************************** 1. row ***************************
Name: student_view
Engine: NULL
Version: NULL
Row_format: NULL
Rows: NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: NULL
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Create_options: NULL
Comment: VIEW
1 row in set (0.00 sec)
  • 对视图进行操作
# 修改视图
mysql> update student_view set name = '老王' where stu_no = '2016003';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

# 视图的修改导致视图表变化
mysql> select * from student_view;
+---------+------+---------+
| stu_no | name | address |
+---------+------+---------+
| 2016001 | 张三 | 贵州 |
| 2016002 | 李芳 | 陕西 |
| 2016003 | 老王 | 江西 |
+---------+------+---------+
3 rows in set (0.00 sec)

# 视图修改导致基表也发生变化
mysql> select * from student;
+---------+------+---------+
| stu_no | name | address |
+---------+------+---------+
| 2016001 | 张三 | 贵州 |
| 2016002 | 李芳 | 陕西 |
| 2016003 | 老王 | 江西 |
+---------+------+---------+
3 rows in set (0.00 sec)
  • 创建多表视图(使用表连接)
# 一个表连接示例
update student_view set name = '老王' where stu_no = '2016003';
mysql> select A.*,B.course,B.score
-> from student A
-> left join score B on(A.stu_no = B.stu_no);
+---------+------+---------+--------+-------+
| stu_no | name | address | course | score |
+---------+------+---------+--------+-------+
| 2016001 | 张三 | 贵州 | 计算机 | 99 |
| 2016001 | 张三 | 贵州 | 离散 | 85 |
| 2016002 | 李芳 | 陕西 | 计算机 | 78 |
| 2016003 | 老王 | 江西 | NULL | NULL |
+---------+------+---------+--------+-------+
4 rows in set (0.31 sec)

# 上面这个表连接创建视图
create view stu_score_view as
mysql> create view stu_score_view as
-> select A.*,B.course,B.score
-> from student A
-> left join score B on(A.stu_no = B.stu_no);
Query OK, 0 rows affected (0.46 sec)

# 查看视图
mysql> select * from stu_score_view;
+---------+------+---------+--------+-------+
| stu_no | name | address | course | score |
+---------+------+---------+--------+-------+
| 2016001 | 张三 | 贵州 | 计算机 | 99 |
| 2016001 | 张三 | 贵州 | 离散 | 85 |
| 2016002 | 李芳 | 陕西 | 计算机 | 78 |
| 2016003 | 老王 | 江西 | NULL | NULL |
+---------+------+---------+--------+-------+
4 rows in set (0.00 sec)

# 修改视图(也会导致基本表被修改)
mysql> update stu_score_view set name = '哈哈' where stu_no = '2016003';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from stu_score_view;
+---------+------+---------+--------+-------+
| stu_no | name | address | course | score |
+---------+------+---------+--------+-------+
| 2016001 | 张三 | 贵州 | 计算机 | 99 |
| 2016001 | 张三 | 贵州 | 离散 | 85 |
| 2016002 | 李芳 | 陕西 | 计算机 | 78 |
| 2016003 | 哈哈 | 江西 | NULL | NULL |
+---------+------+---------+--------+-------+
4 rows in set (0.00 sec)
  • 注意:虽然这个修改视图成功了,但是对于复杂的视图,往往不能直接修改视图
  • 不建议修改视图,因为视图只是相当于对基本表的一个访问接口