本节目标
1.了解什么是视图
2.掌握视图的基本操作【重点】
什么是视图?
视图是一个虚拟表,其内容由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;
创建一个学生表的视图
create view student_view as select * from student;
查看学生表视图,结果与select * from student;一样。
select * from student_view;
mysql> select * from student_view;
+---------+--------+----------+
| stu_no | name | address |
+---------+--------+----------+
| 2016001 | 张三 | 贵州贵阳 |
| 2016002 | 李芳 | 陕西兴平 |
| 2016003 | 张晓燕 | 江西南昌 |
+---------+--------+----------+
3 rows in set (0.04 sec)
mysql> select * from student;
+---------+--------+----------+
| stu_no | name | address |
+---------+--------+----------+
| 2016001 | 张三 | 贵州贵阳 |
| 2016002 | 李芳 | 陕西兴平 |
| 2016003 | 张晓燕 | 江西南昌 |
+---------+--------+----------+
3 rows in set (0.00 sec)
show tables指令可以看到表,也可以看到视图
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| area |
| bank_account |
| contacts |
| employee |
| footprint |
| person |
| score |
| stu |
| stu_course |
| student |
| student_view |
| test_user |
| user_range |
+----------------+
13 rows in set (0.03 sec)
此时无法区分哪张表是视图,故使用命令
show table status where comment='view'\G;
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: 2020-04-09 14:29:39
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Create_options: NULL
Comment: VIEW
1 row in set (0.02 sec)
可以看到有一个视图,Name: student_view
假如修改一下张晓燕的名字为张晓燕2,再进行查看视图和基表。
update student_view set name='张晓燕2' where stu_no='2016003';
mysql> update student_view set name='张晓燕2' where stu_no='2016003';
Query OK, 1 row affected (0.81 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student_view;
+---------+---------+----------+
| stu_no | name | address |
+---------+---------+----------+
| 2016001 | 张三 | 贵州贵阳 |
| 2016002 | 李芳 | 陕西兴平 |
| 2016003 | 张晓燕2 | 江西南昌 |
+---------+---------+----------+
mysql> select * from student;
+---------+---------+----------+
| stu_no | name | address |
+---------+---------+----------+
| 2016001 | 张三 | 贵州贵阳 |
| 2016002 | 李芳 | 陕西兴平 |
| 2016003 | 张晓燕2 | 江西南昌 |
+---------+---------+----------+
3 rows in set (0.00 sec)
基表也发生了相应的变化
反过来,我们修改基表,会不会影响视图呢?
mysql> update student set name='张晓燕' where stu_no='2016003';
Query OK, 1 row affected (0.20 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+---------+--------+----------+
| stu_no | name | address |
+---------+--------+----------+
| 2016001 | 张三 | 贵州贵阳 |
| 2016002 | 李芳 | 陕西兴平 |
| 2016003 | 张晓燕 | 江西南昌 |
+---------+--------+----------+
3 rows in set (0.00 sec)
mysql> select * from student_view;
+---------+--------+----------+
| stu_no | name | address |
+---------+--------+----------+
| 2016001 | 张三 | 贵州贵阳 |
| 2016002 | 李芳 | 陕西兴平 |
| 2016003 | 张晓燕 | 江西南昌 |
+---------+--------+----------+
3 rows in set (0.00 sec)
答案是会的
若将两表连接,并创建一个视图
select A.*,B.course,B.score
from student A
left join score B
on (A.stu_no=B.stu_no);
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.72 sec)
mysql> create view 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.62 sec)
mysql> select * from score_view;
+---------+--------+----------+----------+-------+
| stu_no | name | address | course | score |
+---------+--------+----------+----------+-------+
| 2016001 | 张三 | 贵州贵阳 | 计算机 | 99 |
| 2016001 | 张三 | 贵州贵阳 | 离散数学 | 85 |
| 2016002 | 李芳 | 陕西兴平 | 计算机 | 78 |
| 2016003 | 张晓燕 | 江西南昌 | NULL | NULL |
+---------+--------+----------+----------+-------+
4 rows in set (0.01 sec)
与基表的数据一模一样
如果再吧张晓燕改成张晓燕2,还可以吗?
update score_view set name='张晓燕2' where stu_no='2016003';
mysql> update score_view set name='张晓燕2' where stu_no='2016003';
ERROR 1288 (HY000): The target table score_view of the UPDATE is not updatable
结果报错,说明基表和视图影响仅存在于单标情况。
如果是基于一个以多表连接出来的视图,我们是不能进行数据的
变更操作的。因为视图的本意就是进行封装,我们也不会对其进行增删改查。
若需要测试表,我的资源下载处就有,免积分。
2万+

被折叠的 条评论
为什么被折叠?



