MySQL视图可能不一致的地方,我们如何确保它们的一致性?
在可更新视图的情况下,很有可能我们会更新视图中不可见的数据,因为我们创建的视图仅显示表的部分数据。这种更新会使视图不一致。我们可以在创建或修改视图时使用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';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');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;
现在,如果我们尝试插入与视图“ Info”的定义匹配的行,MySQL允许我们这样做。可以从以下查询及其结果中清除它。
mysql> INSERT INTO Info(Id, Name, Address, Subject) values(133, 'Mohan','Delhi','Computers');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视图可能不一致的地方,我们如何确保它们的一致性? 的全部内容, 来源链接: utcz.com/z/316906.html