本文實(shí)例講述了mysql視圖之確保視圖的一致性(with check option)操作。分享給大家供大家參考,具體如下:
我們有的時(shí)候,會(huì)創(chuàng)建一個(gè)視圖來顯示表的部分?jǐn)?shù)據(jù)。我們知道,簡單視圖是的,因此可以更新通過視圖不可見的數(shù)據(jù),但是此更新會(huì)使的視圖不一致。為了確保視圖的一致性,在創(chuàng)建或修改視圖時(shí)使用WITH CHECK OPTION可更新子句。我們來看下WITH CHECK OPTION可更新子句的語法結(jié)構(gòu):
CREATE OR REPLACE VIEW view_name
AS
select_statement
WITH CHECK OPTION;
我們要注意,將分號(hào)(;)放在WITH CHECK OPTION子句的末尾,而不是在select語句的末尾來定義視圖。完事我們來嘗試根據(jù)employees表創(chuàng)建一個(gè)名為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視圖中查詢數(shù):
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
因?yàn)関ps是一個(gè)簡單的視圖,因此它是可更新的,所以,我們通過vps視圖將一行員工數(shù)據(jù)信息插入:
INSERT INTO vps(employeeNumber,firstname,lastname,jobtitle,extension,email,officeCode,reportsTo)
values(1703,'Lily','Bush','IT Manager','x9111','lilybush@yiiibai.com',1,1002);
我們要注意,新創(chuàng)建的員工通過vps視圖不可見,因?yàn)樗穆毼皇荌T經(jīng)理,而不是VP。使用以下SELECT語句來驗(yàn)證它:
SELECT * FROM employees WHERE employeeNumber=1703;
執(zhí)行上面語句,得到以下結(jié)果:
+----------------+-----------+-----------+-----------+-----------------------+------------+-----------+----------------------+
| 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 |
但這可能不是我們想要的,因?yàn)橥ㄟ^vps視圖暴露VP員工,而不是其他員工,所以,為了確保視圖的一致性,用戶只能顯示或更新通過視圖可見的數(shù)據(jù),則在創(chuàng)建或修改視圖時(shí)使用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語句的結(jié)尾處加上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);
這時(shí)mysql會(huì)拒絕插入并發(fā)出以下錯(cuò)誤消息:
Error Code: 1369 - CHECK OPTION failed 'luyaran.vps'
我們可以通過vps視圖將一個(gè)職位為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發(fā)出1行受影響(Query OK, 1 row affected),我們可以通過根據(jù)vps視圖查詢數(shù)據(jù)來再次驗(yàn)證插入操作:
如上查詢結(jié)果所示,它的確按預(yù)期工作了:
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
好啦,本次記錄就到這里了。
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過程技巧大全》、《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》及《MySQL常用函數(shù)大匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計(jì)有所幫助。
您可能感興趣的文章:- MySql8 WITH RECURSIVE遞歸查詢父子集的方法
- 安裝mysql出錯(cuò)”A Windows service with the name MySQL already exists.“如何解決
- MySQL 5.6 中TIMESTAMP with implicit DEFAULT value is deprecated錯(cuò)誤
- MySQL錯(cuò)誤TIMESTAMP column with CURRENT_TIMESTAMP的解決方法
- MySQL提示:The server quit without updating PID file問題的解決辦法
- php SQL Injection with MySQL
- MySQL8.0的WITH查詢詳情