您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“mysql如何確保視圖的一致性”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“mysql如何確保視圖的一致性”這篇文章吧。
具體如下:
我們有的時候,會創建一個視圖來顯示表的部分數據。我們知道,簡單視圖是的,因此可以更新通過視圖不可見的數據,但是此更新會使的視圖不一致。為了確保視圖的一致性,在創建或修改視圖時使用WITH CHECK OPTION可更新子句。我們來看下WITH CHECK OPTION可更新子句的語法結構:
CREATE OR REPLACE VIEW view_name AS select_statement WITH CHECK OPTION;
我們要注意,將分號(;)放在WITH CHECK OPTION子句的末尾,而不是在select語句的末尾來定義視圖。完事我們來嘗試根據employees表創建一個名為vps的視圖,以顯示其職位為VP的員工,例如VP Marketing和 VP Sales:
CREATE OR REPLACE VIEW vps AS SELECT employeeNumber, lastname, firstname, jobtitle, extension, email, officeCode, reportsTo FROM employees WHERE jobTitle LIKE '%VP%';
接下來,我們使用以下語句從vps視圖中查詢數:
mysql> SELECT * FROM vps; +----------------+----------+-----------+--------------+-----------+----------------------+------------+-----------+ | employeeNumber | lastname | firstname | jobtitle | extension | email | officeCode | reportsTo | +----------------+----------+-----------+--------------+-----------+----------------------+------------+-----------+ | 1056 | Hill | Mary | VP Sales | x4611 | mary.hill@yiibai.com | 1 | 1002 | | 1076 | Firrelli | Jeff | VP Marketing | x9273 | jfirrelli@yiibai.com | 1 | 1002 | +----------------+----------+-----------+--------------+-----------+----------------------+------------+-----------+ 2 rows in set
因為vps是一個簡單的視圖,因此它是可更新的,所以,我們通過vps視圖將一行員工數據信息插入:
INSERT INTO vps(employeeNumber,firstname,lastname,jobtitle,extension,email,officeCode,reportsTo) values(1703,'Lily','Bush','IT Manager','x9111','lilybush@yiiibai.com',1,1002);
我們要注意,新創建的員工通過vps視圖不可見,因為她的職位是IT經理,而不是VP。使用以下SELECT語句來驗證它:
SELECT * FROM employees WHERE employeeNumber=1703;
執行上面語句,得到以下結果:
+----------------+-----------+-----------+-----------+-----------------------+------------+-----------+----------------------+ | employeeNumber | lastName | firstName | extension | email | officeCode | reportsTo | jobTitle | +----------------+-----------+-----------+-----------+-----------------------+------------+-----------+----------------------+ | 1703 | Bush | Lily | x9111 | lilybush@yiiibai.com | 1 | 1002 | IT Manager | | 1702 | Gerard | Martin | x2312 | mgerard@gmail.com | 4 | 1102 | Sales Rep | | 1625 | Kato | Yoshimi | x102 | ykato@gmail.com | 5 | 1621 | Sales Rep | | 1621 | Nishi | Mami | x101 | mnishi@gmail.com | 5 | 1056 | Sales Rep |
但這可能不是我們想要的,因為通過vps視圖暴露VP員工,而不是其他員工,所以,為了確保視圖的一致性,用戶只能顯示或更新通過視圖可見的數據,則在創建或修改視圖時使用WITH CHECK OPTION:
CREATE OR REPLACE VIEW vps AS SELECT employeeNumber, lastname, firstname, jobtitle, extension, email, officeCode, reportsTo FROM employees WHERE jobTitle LIKE '%VP%' WITH CHECK OPTION;
我們要注意在CREATE OR REPLACE語句的結尾處加上WITH CHECK OPTION子句,完事再次通過vps視圖將一行插入employees表中,如下所示:
INSERT INTO vps(employeeNumber,firstname,lastname,jobtitle,extension,email,officeCode,reportsTo) VALUES(1704,'John','Minsu','IT Staff','x9112','johnminsu@yiibai.com',1,1703);
這時mysql會拒絕插入并發出以下錯誤消息:
Error Code: 1369 - CHECK OPTION failed 'luyaran.vps'
我們可以通過vps視圖將一個職位為SVP Marketing的員工插入employees表,看看mysql是否允許這樣做:
INSERT INTO vps(employeeNumber,firstname,lastname,jobtitle,extension,email,officeCode,reportsTo) VALUES(1704,'John','Minsu','SVP Marketing','x9112','johnminsu@classicmodelcars.com',1,1076);
mysql發出1行受影響(Query OK, 1 row affected),我們可以通過根據vps視圖查詢數據來再次驗證插入操作:
SELECT * FROM vps;
如上查詢結果所示,它的確按預期工作了:
mysql> SELECT * FROM vps; +----------------+----------+-----------+---------------+-----------+--------------------------------+------------+-----------+ | employeeNumber | lastname | firstname | jobtitle | extension | email | officeCode | reportsTo | +----------------+----------+-----------+---------------+-----------+--------------------------------+------------+-----------+ | 1056 | Hill | Mary | VP Sales | x4611 | mary.hill@yiibai.com | 1 | 1002 | | 1076 | Firrelli | Jeff | VP Marketing | x9273 | jfirrelli@yiibai.com | 1 | 1002 | | 1704 | Minsu | John | SVP Marketing | x9112 | johnminsu@classicmodelcars.com | 1 | 1076 | +----------------+----------+-----------+---------------+-----------+--------------------------------+------------+-----------+ 3 rows in set
以上是“mysql如何確保視圖的一致性”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。