🎜🎜 . Kemas kini ini menjadikan paparan tidak konsisten. Kami boleh memastikan ketekalan paparan dengan menggunakan
WITH CHECK OPTION semasa mencipta atau mengubah suai paparan. Walaupun klausa WITH CHECK OPTION ialah bahagian pilihan bagi pernyataan CREATE VIEW, ia sangat berguna untuk menjadikan pandangan itu konsisten.
Pada asasnya, klausa WITH CHECK OPTION menghalang kami daripada mengemas kini atau memasukkan baris yang tidak kelihatan melalui paparan. Secara ringkas, kita boleh mengatakan bahawa selepas menggunakan klausa WITH CHECK OPTION, MySQL memastikan bahawa operasi sisipan atau kemas kini disahkan oleh definisi paparan. Berikut ialah sintaks klausa WITH CHECK OPTION -
Tatabahasa
CREATE OR REPLACE VIEW view_name AS Select_statement WITH CHECK OPTION;
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)
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)
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)
dengan menggunakan "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)
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)
mysql> INSERT INTO Info(Id, Name, Address, Subject) values(134, 'Charanjeet','Amritsar','Geophysics'); ERROR 1369 (HY000): CHECK OPTION failed
Atas ialah kandungan terperinci Di manakah pandangan MySQL mungkin tidak konsisten, dan bagaimana untuk memastikan konsistensinya?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!