在可更新视图的情况下,我们很可能更新通过视图不可见的数据,因为我们创建的视图仅显示表的部分数据。这种更新使得视图不一致。我们在创建或修改视图时可以通过使用WITH CHECK OPTION来保证视图的一致性。虽然WITH CHECK OPTION子句是CREATE VIEW语句的可选部分,但它对于使视图保持一致非常有用。
基本上,WITH CHECK OPTION子句可以防止我们更新或插入以下行:通过视图不可见。简单来说,我们可以说,使用WITH CHECK OPTION子句后,MySQL确保插入或更新操作得到视图定义的确认。以下是WITH CHECK OPTION 子句的语法 -
语法
CREATE OR REPLACE VIEW view_name AS Select_statement WITH CHECK OPTION;
登录后复制
示例
为了说明上述概念,我们使用表“Student_info”中的以下数据 -
mysql> Select * from student_info;
+------+---------+------------+------------+
| id | Name | Address | Subject |
+------+---------+------------+------------+
| 101 | YashPal | Amritsar | History |
| 105 | Gaurav | Chandigarh | Literature |
| 125 | Raman | Shimla | Computers |
| 130 | Ram | Jhansi | Computers |
+------+---------+------------+------------+
4 rows in set (0.08 sec)
登录后复制
现在,借助以下查询,我们将创建视图名称“Info”。这里我们没有使用WITH CHECK OPTION。
mysql> Create OR Replace VIEW Info AS Select Id, Name, Address, Subject from student_info WHERE Subject = 'Computers';
Query OK, 0 rows affected (0.46 sec)
mysql> Select * from info;
+------+-------+---------+-----------+
| Id | Name | Address | Subject |
+------+-------+---------+-----------+
| 125 | Raman | Shimla | Computers |
| 130 | Ram | Jhansi | Computers |
+------+-------+---------+-----------+
2 rows in set (0.00 sec)
登录后复制
因为,我们没有使用WITH CHECK OPTION,所以我们可以在“Info”中插入/更新新行,即使它与其定义不匹配。下面的查询及其结果说明了这一点 -
mysql> INSERT INTO Info(Id, Name, Address, Subject) values(132, 'Shyam','Chandigarh', 'Economics');
Query OK, 1 row affected (0.37 sec)
mysql> Select * from student_info;
+------+---------+------------+------------+
| id | Name | Address | Subject |
+------+---------+------------+------------+
| 101 | YashPal | Amritsar | History |
| 105 | Gaurav | Chandigarh | Literature |
| 125 | Raman | Shimla | Computers |
| 130 | Ram | Jhansi | Computers |
| 132 | Shyam | Chandigarh | Economics |
+------+---------+------------+------------+
5 rows in set (0.00 sec)
mysql> Select * from info;
+------+-------+---------+-----------+
| Id | Name | Address | Subject |
+------+-------+---------+-----------+
| 125 | Raman | Shimla | Computers |
| 130 | Ram | Jhansi | Computers |
+------+-------+---------+-----------+
2 rows in set (0.00 sec)
登录后复制
上面的结果集显示新行与“Info”的定义不匹配,因此它在视图中不可见。现在,在以下查询中,我们将创建相同的视图“Info”
通过使用“WITH CHECK OPTION” -
mysql> Create OR Replace VIEW Info AS Select Id, Name, Address, Subject from student_info WHERE Subject = 'Computers' WITH CHECK OPTION;
Query OK, 0 rows affected (0.06 sec)
登录后复制
现在,如果我们尝试插入与视图“Info”的定义匹配的行,MySQL 允许我们这样做。可以从下面的查询及其结果中清除它。
mysql> INSERT INTO Info(Id, Name, Address, Subject) values(133, 'Mohan','Delhi','Computers');
Query OK, 1 row affected (0.07 sec)
mysql> Select * from info;
+------+-------+---------+-----------+
| Id | Name | Address | Subject |
+------+-------+---------+-----------+
| 125 | Raman | Shimla | Computers |
| 130 | Ram | Jhansi | Computers |
| 133 | Mohan | Delhi | Computers |
+------+-------+---------+-----------+
3 rows in set (0.00 sec)
登录后复制
但是假设如果我们尝试插入与视图“Info”的定义不匹配的行,MySQL 将不允许我们这样做并抛出错误 -
mysql> INSERT INTO Info(Id, Name, Address, Subject) values(134, 'Charanjeet','Amritsar','Geophysics');
ERROR 1369 (HY000): CHECK OPTION failed
登录后复制
以上就是MySQL视图在哪些地方可能会不一致,如何保证它们的一致性?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!