亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

管理mysql的視圖

標簽:
MySQL

Summary: in this tutorial, you will learn how to manage views in MySQL including displaying, modifying and removing views.

Show view definition in MySQL

MySQL provides the SHOW CREATE VIEW statement that helps you show view definition. The following is the syntax of the SHOW CREATE VIEW statement:

SHOW CREATE VIEW [database_name].[view_ name];

To display the definition of a view, you just need to specify its name after the SHOW CREATE VIEW keywords.

Let’s create a view for the demonstration.

First, we create a simple view against the employees table that displays the company’s organization structure:

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

To display the view’s definition, you use the SHOW CREATE VIEW statement as follows:

SHOW CREATE VIEW organization

You can also display the definition of the view by using any plain text editor such as notepad to open the view definition file in the database folder.

For example, to open the organization view definition, you can find the view definition file with the following path: \data\classicmodels\organization.frm

Modifying views

Once a view is defined, you can modify it by using the ALTER VIEW statement. The syntax of the ALTER VIEWstatement is similar to the CREATE VIEW statement except the CREATE keyword is replaced by the ALTER keyword.

ALTER  [ALGORITHM = {MERGE | TEMPTABLE | UNDEFINED}]   VIEW [database_name]. [view_name]    AS   [SELECT  statement]

The following query modifies the organization view by adding an addition email field.

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

To verify the change, you can query data from the organization view:

SELECT * FROM Organization

 

MySQL drop views

Once a view created, you can remove it by using the DROP VIEW statement. The following illustrates the syntax of the DROP VIEW statement:

DROP VIEW [IF EXISTS] [database_name].[view_name]

The IF EXISTS is the optional element of the statement, which allows you to check whether the view exists or not. It helps you avoid an error of removing a non-existent view.

For example, if you want to remove the organization view, you can use the DROP VIEW statement as follows:

DROP VIEW IF EXISTS organization

Each time you modify or remove a view, MySQL makes a back up of the view definition file to the /database_name/arc/ folder. In case you modify or remove a view by accident, you can get a back up from there.

In this tutorial, you have learned how to manage views in MySQL including displaying, modifying and removing views.

Related Tutorials

原文链接:http://outofmemory.cn/mysql/view/managing-sql-views

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消