您好,登錄后才能下訂單哦!
本文實(shí)例講述了mysql視圖之管理視圖操作。分享給大家供大家參考,具體如下:
mysql提供了用于顯示視圖定義的SHOW CREATE VIEW語(yǔ)句,我們來(lái)看下語(yǔ)法結(jié)構(gòu):
SHOW CREATE VIEW [database_name].[view_ name];
要顯示視圖的定義,需要在SHOW CREATE VIEW子句之后指定視圖的名稱(chēng),我們先來(lái)根據(jù)employees表創(chuàng)建一個(gè)簡(jiǎn)單的視圖用來(lái)顯示公司組織結(jié)構(gòu),完事在進(jìn)行演示:
CREATE VIEW organization AS SELECT CONCAT(E.lastname, E.firstname) AS Employee, CONCAT(M.lastname, M.firstname) AS Manager FROM employees AS E INNER JOIN employees AS M ON M.employeeNumber = E.ReportsTo ORDER BY Manager;
從以上視圖中查詢(xún)數(shù)據(jù),得到以下結(jié)果:
mysql> SELECT * FROM organization; +------------------+------------------+ | Employee | Manager | +------------------+------------------+ | BondurLoui | BondurGerard | | CastilloPamela | BondurGerard | | JonesBarry | BondurGerard | | HernandezGerard | BondurGerard | .......此處省略了many many數(shù)據(jù)....... | KatoYoshimi | NishiMami | | KingTom | PattersonWilliam | | MarshPeter | PattersonWilliam | | FixterAndy | PattersonWilliam | +------------------+------------------+ 24 rows in set
要顯示視圖的定義,請(qǐng)使用SHOW CREATE VIEW語(yǔ)句如下:
SHOW CREATE VIEW organization;
我們還可以使用任何純文本編輯器(如記事本)顯示視圖的定義,以打開(kāi)數(shù)據(jù)庫(kù)文件夾中的視圖定義文件。例如,要打開(kāi)organization視圖定義,可以在數(shù)據(jù)庫(kù)文件夾下的data文件夾中找到你數(shù)據(jù)庫(kù)文件夾,完事進(jìn)入其中按著你視圖名稱(chēng)找.frm文件。
我們?cè)賮?lái)通過(guò)ALTER VIEW和CREATE OR REPLACE VIEW來(lái)嘗試修改視圖,先來(lái)看下alert view語(yǔ)法:
ALTER [ALGORITHM = {MERGE | TEMPTABLE | UNDEFINED}] VIEW [database_name]. [view_name] AS [SELECT statement]
以下語(yǔ)句通過(guò)添加email列來(lái)演示如何修改organization視圖:
ALTER VIEW organization AS SELECT CONCAT(E.lastname,E.firstname) AS Employee, E.email AS employeeEmail, CONCAT(M.lastname,M.firstname) AS Manager FROM employees AS E INNER JOIN employees AS M ON M.employeeNumber = E.ReportsTo ORDER BY Manager;
要驗(yàn)證更改,可以從organization視圖中查詢(xún)數(shù)據(jù),咱就不贅述了,完事來(lái)看下另一個(gè)語(yǔ)法結(jié)構(gòu):
CREATE OR REPLACE VIEW v_contacts AS SELECT firstName, lastName, extension, email FROM employees; -- 查詢(xún)視圖數(shù)據(jù) SELECT * FROM v_contacts;
我們要注意,在我們修改的時(shí)候,如果一個(gè)視圖已經(jīng)存在,mysql只會(huì)修改視圖。如果視圖不存在,mysql將創(chuàng)建一個(gè)新的視圖。好啦,我們來(lái)看下上述sql執(zhí)行的結(jié)果:
+-----------+-----------+-----------+--------------------------------+ | firstName | lastName | extension | email | +-----------+-----------+-----------+--------------------------------+ | Diane | Murphy | x5800 | dmurphy@yiibai.com | | Mary | Hill | x4611 | mary.hill@yiibai.com | | Jeff | Firrelli | x9273 | jfirrelli@yiibai.com | | William | Patterson | x4871 | wpatterson@yiibai.com | | Gerard | Bondur | x5408 | gbondur@gmail.com | | Anthony | Bow | x5428 | abow@gmail.com | | Leslie | Jennings | x3291 | ljennings@yiibai.com | .............. 此處省略了many many數(shù)據(jù) .................................. | Martin | Gerard | x2312 | mgerard@gmail.com | | Lily | Bush | x9111 | lilybush@yiiibai.com | | John | Minsu | x9112 | johnminsu@classicmodelcars.com | +-----------+-----------+-----------+--------------------------------+ 25 rows in set
假設(shè)我們要將職位(jobtitle)列添加到v_contacts視圖中,只需使用以下語(yǔ)句:
CREATE OR REPLACE VIEW v_contacts AS SELECT firstName, lastName, extension, email, jobtitle FROM employees; -- 查詢(xún)視圖數(shù)據(jù) SELECT * FROM v_contacts;
執(zhí)行上面查詢(xún)語(yǔ)句后,可以看到添加一列數(shù)據(jù):
+-----------+-----------+-----------+--------------------------------+----------------------+ | firstName | lastName | extension | email | jobtitle | +-----------+-----------+-----------+--------------------------------+----------------------+ | Diane | Murphy | x5800 | dmurphy@yiibai.com | President | | Mary | Hill | x4611 | mary.hill@yiibai.com | VP Sales | | Jeff | Firrelli | x9273 | jfirrelli@yiibai.com | VP Marketing | ................... 此處省略了一大波數(shù)據(jù) .................................................... | Yoshimi | Kato | x102 | ykato@gmail.com | Sales Rep | | Martin | Gerard | x2312 | mgerard@gmail.com | Sales Rep | | Lily | Bush | x9111 | lilybush@yiiibai.com | IT Manager | | John | Minsu | x9112 | johnminsu@classicmodelcars.com | SVP Marketing | +-----------+-----------+-----------+--------------------------------+----------------------+ 25 rows in set
完事我們來(lái)看使用DROP VIEW語(yǔ)句將視圖刪除,先來(lái)看下語(yǔ)法結(jié)構(gòu):
DROP VIEW [IF EXISTS] [database_name].[view_name]
上述sql中,IF EXISTS是語(yǔ)句的可選子句,它允許我們檢查視圖是否存在,用來(lái)避免刪除不存在的視圖的錯(cuò)誤。完事我們來(lái)刪除organization視圖:
DROP VIEW IF EXISTS organization;
我們得注意下,每次修改或刪除視圖時(shí),mysql會(huì)將視圖定義文件備份到/database_name/arc/目錄中。 如果我們意外修改或刪除視圖,可以從/database_name/arc/文件夾獲取其備份。
好啦,本次記錄就到這里了。
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《MySQL查詢(xún)技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過(guò)程技巧大全》、《MySQL數(shù)據(jù)庫(kù)鎖相關(guān)技巧匯總》及《MySQL常用函數(shù)大匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫(kù)計(jì)有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。