您好,登錄后才能下訂單哦!
本文實例講述了mysql視圖之管理視圖操作。分享給大家供大家參考,具體如下:
mysql提供了用于顯示視圖定義的SHOW CREATE VIEW語句,我們來看下語法結構:
SHOW CREATE VIEW [database_name].[view_ name];
要顯示視圖的定義,需要在SHOW CREATE VIEW子句之后指定視圖的名稱,我們先來根據employees表創建一個簡單的視圖用來顯示公司組織結構,完事在進行演示:
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;
從以上視圖中查詢數據,得到以下結果:
mysql> SELECT * FROM organization; +------------------+------------------+ | Employee | Manager | +------------------+------------------+ | BondurLoui | BondurGerard | | CastilloPamela | BondurGerard | | JonesBarry | BondurGerard | | HernandezGerard | BondurGerard | .......此處省略了many many數據....... | KatoYoshimi | NishiMami | | KingTom | PattersonWilliam | | MarshPeter | PattersonWilliam | | FixterAndy | PattersonWilliam | +------------------+------------------+ 24 rows in set
要顯示視圖的定義,請使用SHOW CREATE VIEW語句如下:
SHOW CREATE VIEW organization;
我們還可以使用任何純文本編輯器(如記事本)顯示視圖的定義,以打開數據庫文件夾中的視圖定義文件。例如,要打開organization視圖定義,可以在數據庫文件夾下的data文件夾中找到你數據庫文件夾,完事進入其中按著你視圖名稱找.frm文件。
我們再來通過ALTER VIEW和CREATE OR REPLACE VIEW來嘗試修改視圖,先來看下alert view語法:
ALTER [ALGORITHM = {MERGE | TEMPTABLE | UNDEFINED}] VIEW [database_name]. [view_name] AS [SELECT statement]
以下語句通過添加email列來演示如何修改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;
要驗證更改,可以從organization視圖中查詢數據,咱就不贅述了,完事來看下另一個語法結構:
CREATE OR REPLACE VIEW v_contacts AS SELECT firstName, lastName, extension, email FROM employees; -- 查詢視圖數據 SELECT * FROM v_contacts;
我們要注意,在我們修改的時候,如果一個視圖已經存在,mysql只會修改視圖。如果視圖不存在,mysql將創建一個新的視圖。好啦,我們來看下上述sql執行的結果:
+-----------+-----------+-----------+--------------------------------+ | 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數據 .................................. | Martin | Gerard | x2312 | mgerard@gmail.com | | Lily | Bush | x9111 | lilybush@yiiibai.com | | John | Minsu | x9112 | johnminsu@classicmodelcars.com | +-----------+-----------+-----------+--------------------------------+ 25 rows in set
假設我們要將職位(jobtitle)列添加到v_contacts視圖中,只需使用以下語句:
CREATE OR REPLACE VIEW v_contacts AS SELECT firstName, lastName, extension, email, jobtitle FROM employees; -- 查詢視圖數據 SELECT * FROM v_contacts;
執行上面查詢語句后,可以看到添加一列數據:
+-----------+-----------+-----------+--------------------------------+----------------------+ | 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 | ................... 此處省略了一大波數據 .................................................... | 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
完事我們來看使用DROP VIEW語句將視圖刪除,先來看下語法結構:
DROP VIEW [IF EXISTS] [database_name].[view_name]
上述sql中,IF EXISTS是語句的可選子句,它允許我們檢查視圖是否存在,用來避免刪除不存在的視圖的錯誤。完事我們來刪除organization視圖:
DROP VIEW IF EXISTS organization;
我們得注意下,每次修改或刪除視圖時,mysql會將視圖定義文件備份到/database_name/arc/目錄中。 如果我們意外修改或刪除視圖,可以從/database_name/arc/文件夾獲取其備份。
好啦,本次記錄就到這里了。
更多關于MySQL相關內容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL事務操作技巧匯總》、《MySQL存儲過程技巧大全》、《MySQL數據庫鎖相關技巧匯總》及《MySQL常用函數大匯總》
希望本文所述對大家MySQL數據庫計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。