注意:如果视图定义中有以下几种情况都不能更新
1、分组(使用group by 和 having)
2、联结,多表组成的
3、子查询
4、并
5、聚集函数(min(),count(),sum() 等)
6、distinct
7、导出(计算)列
因此本文中例子都不能更新,而且这个也基本用不到,因为视图就是用于检索数据,不用于更新(insert,update,delete)
为什么用左外连接的视图不能在此基础上创建行列子集视图
Microsoft Windows [版本 10.0.19042.928]
(c) Microsoft Corporation。保留所有权利。
C:\Users\虚无>mysql -u root -p
Enter password: **
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 121
Server version: 5.7.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql> use school;
Database changed
mysql> create view a1 as select from student left join sc using(sno);
Query OK, 0 rows affected (0.01 sec)
mysql> select from a1;
+———-+—————-+——————+———+———+———-+———+———-+
| Sno | sname | birthday | sage | Ssex | Sdept | Cno | Grade |
+———-+—————-+——————+———+———+———-+———+———-+
| 08001 | 张力 | 2021-04-01 | 21 | 男 | cs | 002 | 0104 |
| 08001 | 张力 | 2021-04-01 | 21 | 男 | cs | 003 | 0095 |
| 08001 | 张力 | 2021-04-01 | 21 | 男 | cs | 004 | 0090 |
| 08001 | 张力 | 2021-04-01 | 21 | 男 | cs | 006 | 0100 |
| 08002 | 李丽 | NULL | 20 | 女 | is | 002 | 0102 |
| 08004 | 张那 | NULL | 21 | 女 | cs | 001 | 0090 |
| 08005 | 刘晨 | NULL | 19 | 男 | is | 007 | 0097 |
| 08008 | 王江 | NULL | 21 | 男 | cs | 003 | 0080 |
| 08009 | 高晓 | NULL | 20 | 男 | is | 001 | 0089 |
| 08009 | 高晓 | NULL | 20 | 男 | is | 004 | 0090 |
| 08003 | 赵海 | NULL | 21 | 男 | ma | NULL | NULL |
| 08006 | 刘丹丹 | NULL | 18 | 女 | ma | NULL | NULL |
| 08007 | 刘立 | NULL | 21 | 男 | cs | NULL | NULL |
| 08010 | 张丽 | NULL | 21 | 女 | cs | NULL | NULL |
| 08000 | 郭校长 | NULL | 21 | 男 | cs | NULL | NULL |
| 08011 | 尹延龙 | NULL | 20 | 男 | ma | NULL | NULL |
| 08012 | 张家兴 | NULL | 20 | 男 | is | NULL | NULL |
| 08013 | 赵博润 | NULL | 21 | 男 | cs | NULL | NULL |
| 08014 | 付宇 | NULL | 21 | 女 | cs | NULL | NULL |
| 08015 | 张国栋 | NULL | 21 | 男 | cs | NULL | NULL |
| 08016 | 付宇 | NULL | 21 | 男 | is | NULL | NULL |
| 08017 | 汪科 | NULL | 21 | 男 | yx | NULL | NULL |
| 08018 | 程广彬 | NULL | 21 | 男 | yx | NULL | NULL |
| 08019 | 张三 | NULL | 22 | 男 | yx | NULL | NULL |
| 08020 | 闫莹莹 | NULL | 21 | 女 | yx | NULL | NULL |
| 08021 | 郭雪 | NULL | 21 | 女 | yy | NULL | NULL |
| 08022 | 郭陆振 | 2021-04-07 | 22 | 男 | ma | NULL | NULL |
+———-+—————-+——————+———+———+———-+———+———-+
27 rows in set (0.00 sec)
mysql> create view a2 as select from a2 where sdept=’cs’ with check opion;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘opion’ at line 1
mysql> create view a2 as select from a2 where sdept=’cs’ with check option;
ERROR 1146 (42S02): Table ‘school.a2’ doesn’t exist
mysql> create view a_1 as select from a2 where sdept=’cs’ with check option;
ERROR 1146 (42S02): Table ‘school.a2’ doesn’t exist
mysql> create view a_1 as select from a1 where sdept=’cs’ with check option;
ERROR 1368 (HY000): CHECK OPTION on non-updatable view ‘school.a_1’
mysql>
