在可更新視圖的情況下,我們很可能會更新通過視圖不可見的數據,因為我們建立的視圖僅顯示表的部分數據。這種更新使得視圖不一致。我們在建立或修改視圖時可以透過使用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視圖在哪些地方可能會不一致,如何確保它們的一致性?的詳細內容。更多資訊請關注PHP中文網其他相關文章!