在可更新視圖的情況下,我們很可能更新通過視圖不可見的數據,因為我們創建的視圖僅顯示表的部分數據。這種更新使得視圖不一致。我們在創建或修改視圖時可以通過使用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.92cms.cn其它相關文章!