本文實例講述了mysql視圖之管理視圖操作。分享給大家供大家參考,具體如下:
mysql提供了用于顯示視圖定義的SHOW CREATE VIEW語句,我們來看下語法結(jié)構(gòu):
SHOW CREATE VIEW [database_name].[view_ name];
要顯示視圖的定義,需要在SHOW CREATE VIEW子句之后指定視圖的名稱,我們先來根據(jù)employees表創(chuàng)建一個簡單的視圖用來顯示公司組織結(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;
從以上視圖中查詢數(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
要顯示視圖的定義,請使用SHOW CREATE VIEW語句如下:
SHOW CREATE VIEW organization;
我們還可以使用任何純文本編輯器(如記事本)顯示視圖的定義,以打開數(shù)據(jù)庫文件夾中的視圖定義文件。例如,要打開organization視圖定義,可以在數(shù)據(jù)庫文件夾下的data文件夾中找到你數(shù)據(jù)庫文件夾,完事進(jìn)入其中按著你視圖名稱找.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視圖中查詢數(shù)據(jù),咱就不贅述了,完事來看下另一個語法結(jié)構(gòu):
CREATE OR REPLACE VIEW v_contacts AS
SELECT
firstName, lastName, extension, email
FROM
employees;
-- 查詢視圖數(shù)據(jù)
SELECT * FROM v_contacts;
我們要注意,在我們修改的時候,如果一個視圖已經(jīng)存在,mysql只會修改視圖。如果視圖不存在,mysql將創(chuàng)建一個新的視圖。好啦,我們來看下上述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視圖中,只需使用以下語句:
CREATE OR REPLACE VIEW v_contacts AS
SELECT
firstName, lastName, extension, email, jobtitle
FROM
employees;
-- 查詢視圖數(shù)據(jù)
SELECT * FROM v_contacts;
執(zhí)行上面查詢語句后,可以看到添加一列數(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
完事我們來看使用DROP VIEW語句將視圖刪除,先來看下語法結(jié)構(gòu):
DROP VIEW [IF EXISTS] [database_name].[view_name]
上述sql中,IF EXISTS是語句的可選子句,它允許我們檢查視圖是否存在,用來避免刪除不存在的視圖的錯誤。完事我們來刪除organization視圖:
DROP VIEW IF EXISTS organization;
我們得注意下,每次修改或刪除視圖時,mysql會將視圖定義文件備份到/database_name/arc/目錄中。 如果我們意外修改或刪除視圖,可以從/database_name/arc/文件夾獲取其備份。
好啦,本次記錄就到這里了。
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲過程技巧大全》、《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》及《MySQL常用函數(shù)大匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。
您可能感興趣的文章:- mysql視圖之創(chuàng)建視圖(CREATE VIEW)和使用限制實例詳解
- MySQL如何創(chuàng)建視圖
- 詳細(xì)分析mysql視圖的原理及使用方法
- MySQL的視圖和索引用法與區(qū)別詳解
- 淺談MySql 視圖、觸發(fā)器以及存儲過程
- MySql視圖觸發(fā)器存儲過程詳解
- mysql視圖原理與用法實例詳解
- mysql視圖之創(chuàng)建可更新視圖的方法詳解
- MySQL中Update、select聯(lián)用操作單表、多表,及視圖與臨時表的區(qū)別
- mysql三張表連接建立視圖
- MySQL 視圖(View)原理解析