MySQL 视图管理
简介:在本教程中,您将学习如何在MySQL中管理视图,包括显示,修改和删除视图。
显示视图定义
MySQL提供了SHOW CREATE VIEW
显示视图定义的语句。
以下是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;
+------------------+------------------+ | Employee | Manager | +------------------+------------------+ | BottLarry | BondurGerard | | BondurLoui | BondurGerard | | CastilloPamela | BondurGerard | | JonesBarry | BondurGerard | | HernandezGerard | BondurGerard | | GerardMartin | BondurGerard | | FirrelliJulie | BowAnthony | | TsengFoon Yue | BowAnthony | | ThompsonLeslie | BowAnthony | | PattersonSteve | BowAnthony | ...要显示视图的定义,请使用以下
SHOW CREATE VIEW
语句:
SHOW CREATE VIEW organization;您还可以使用任何纯文本编辑器(如记事本)显示视图的定义,以在数据库文件夹中打开视图定义文件。 例如,要打开
organization
视图定义,可以使用以下路径查找视图定义文件:\data\mysqldemo\organization.frm
但是,您不应直接在* .frm文件中修改视图。
修改视图
MySQL提供了两个语句,允许您修改现有视图:ALTER VIEW
和 CREATE OR REPLACE VIEW
使用ALTER VIEW语句修改视图
创建视图后,您可以使用ALTER VIEW
语句对其进行修改。
ALTER VIEW
语句的语法类似于CREATE VIEW
语句,除了CREATE
关键字被替换ALTER
关键字。
ALTER [ALGORITHM = {MERGE | TEMPTABLE | UNDEFINED}] VIEW [database_name]. [view_name] AS [SELECT statement]以下语句
organization
通过添加email
列来修改视图 。
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
视图中查询数据:
SELECT * FROM Organization;
+------------------+--------------------------------+------------------+ | Employee | employeeEmail | Manager | +------------------+--------------------------------+------------------+ | HernandezGerard | ghernande@gmail.com | BondurGerard | | BondurLoui | lbondur@yiibai.com | BondurGerard | | JonesBarry | bjones@gmail.com | BondurGerard | | BottLarry | lbott@yiibai.com | BondurGerard | | CastilloPamela | pcastillo@gmail.com | BondurGerard | | GerardMartin | mgerard@gmail.com | BondurGerard | | PattersonSteve | spatterson@yiibai.com | BowAnthony | ...
使用CREATE OR REPLACE VIEW语句修改视图
除了ALTER VIEW
语句之外,您还可以使用CREATE OR REPLACE VIEW
语句创建或替换现有视图。如果视图已经存在,MySQL只是修改视图。如果视图不存在,MySQL将创建一个新视图。
以下语句使用CREATE OR REPLACE VIEW
语法创建contacts
基于employees
表创建视图 :
CREATE OR REPLACE VIEW contacts AS SELECT firstName, lastName, extension, email FROM employees;
+-----------+-----------+-----------+--------------------------------+ | firstName | lastName | extension | email | +-----------+-----------+-----------+--------------------------------+ | Diane | Murphy | x5800 | dmurphy@yiibai.com | | Mary | Patterson | x4611 | mpatterso@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 | | Leslie | Thompson | x4065 | lthompson@yiibai.com | ...假设您要将作业标题列添加到联系人视图,只需使用以下语句即可。
CREATE OR REPLACE VIEW contacts AS SELECT firstName, lastName, extension, email, jobtitle FROM employees;
+-----------+-----------+-----------+--------------------------------+----------------------+ | firstName | lastName | extension | email | jobtitle | +-----------+-----------+-----------+--------------------------------+----------------------+ | Diane | Murphy | x5800 | dmurphy@yiibai.com | President | | Mary | Patterson | x4611 | mpatterso@yiibai.com | VP Sales | | Jeff | Firrelli | x9273 | jfirrelli@yiibai.com | VP Marketing | | William | Patterson | x4871 | wpatterson@yiibai.com | Sales Manager (APAC) | | Gerard | Bondur | x5408 | gbondur@gmail.com | Sale Manager (EMEA) | | Anthony | Bow | x5428 | abow@gmail.com | Sales Manager (NA) | | Leslie | Jennings | x3291 | ljennings@yiibai.com | Sales Rep | ...
删除视图
创建视图后,可以使用DROP VIEW
语句将其删除。以下说明了DROP VIEW
语句的语法:
DROP VIEW [IF EXISTS] [database_name].[view_name]
IF EXISTS
是语句的可选子句,允许您检查视图是否存在。它可以帮助您避免删除不存在的视图时出错。
例如,如果要删除organization
视图,可以使用以下DROP VIEW
语句:
DROP VIEW IF EXISTS organization;每次修改或删除视图时,MySQL都会将视图定义文件备份到
/database_name/arc/
文件夹。如果您意外修改或删除视图,可以从arc文件夹中获取备份。
在本教程中,您学习了如何在MySQL中管理视图,包括显示,修改和删除视图。