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'; 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
广告