This article uses an example to describe the management view operation of MySQL view. Share with you for your reference, the details are as follows: MySQL provides the SHOW CREATE VIEW statement for displaying view definitions. Let's look at the syntax structure: SHOW CREATE VIEW [database_name].[view_ name]; To display the view definition, you need to specify the name of the view after the SHOW CREATE VIEW clause. Let's first create a simple view based on the employees table to display the company's organizational structure, and then demonstrate it: 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; Querying data from the above view, we get the following results: mysql> SELECT * FROM organization; +------------------+------------------+ | Employee | Manager | +------------------+------------------+ | BondurLoui | BondurGerard | | CastilloPamela | BondurGerard | | JonesBarry | BondurGerard | | HernandezGerard | BondurGerard | .......many many data is omitted here....... | KatoYoshimi | NishiMami | | KingTom | PattersonWilliam | | MarshPeter | PattersonWilliam | | FixterAndy | PattersonWilliam | +------------------+------------------+ 24 rows in set To display the definition of a view, use the SHOW CREATE VIEW statement as follows: SHOW CREATE VIEW organization; We can also display the definition of a view 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 your database folder in the data folder under the database folder, enter it and find the .frm file according to your view name. Let's try to modify the view through ALTER VIEW and CREATE OR REPLACE VIEW. First, let's look at the alert view syntax: ALTER [ALGORITHM = {MERGE | TEMPTABLE | UNDEFINED}] VIEW [database_name]. [view_name] AS [SELECT statement] The following statement demonstrates how to modify the organization view by adding an email column: 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 changes, you can query the data from the organization view. I won’t go into details. Let’s take a look at another syntax structure: CREATE OR REPLACE VIEW v_contacts AS SELECT firstName, lastName, extension, email FROM employees; -- Query view data SELECT * FROM v_contacts; We should note that when we modify, if a view already exists, MySQL will only modify the view. If the view does not exist, MySQL creates a new view. Well, let's take a look at the results of the above SQL execution: +-----------+-----------+-----------+--------------------------------+ | 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 data is omitted here.................................. | Martin | Gerard | x2312 | mgerard@gmail.com | | Lily | Bush | x9111 | lilybush@yiiibai.com | | John | Minsu | x9112 | johnminsu@classicmodelcars.com | +-----------+-----------+-----------+--------------------------------+ 25 rows in set Suppose we want to add a jobtitle column to the v_contacts view. We can use the following statement: CREATE OR REPLACE VIEW v_contacts AS SELECT firstName, lastName, extension, email, jobtitle FROM employees; -- Query view data SELECT * FROM v_contacts; After executing the above query statement, you can see that a column of data is added: +-----------+-----------+-----------+--------------------------------+----------------------+ | 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 | ................... A large amount of data is omitted here................................................... | 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 Now let's use the DROP VIEW statement to delete the view. Let's first look at the syntax structure: DROP VIEW [IF EXISTS] [database_name].[view_name] In the above SQL, IF EXISTS is an optional clause of the statement, which allows us to check whether the view exists and avoid errors when deleting a non-existent view. When we are done, let's delete the organization view: DROP VIEW IF EXISTS organization; We have to note that every time a view is modified or deleted, MySQL will back up the view definition file to the /database_name/arc/ directory. If we accidentally modify or delete a view, we can get its backup from the /database_name/arc/ folder. Okay, that’s all for this record. Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary" I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
<<: Detailed explanation of Docker daemon security configuration items
>>: Detailed explanation of how to use Vue self-nested tree components
Ubuntu 15.04 opens MySQL remote port 3306. All th...
Detailed explanation of the implementation method...
When using vue to develop projects, the front end...
Try installing via pip in a virtual environment: ...
Part.0 Background The company's intranet serv...
Table label composition The table in HTML is comp...
Chapter 1 Source Code Installation The installati...
Related articles: Beginners learn some HTML tags ...
Table of contents 1. How to find duplicate rows 2...
This article describes how to add or expand a dis...
C++ connects to MySQL for your reference. The spe...
This article shares the installation tutorial of ...
1. Download nginx [root@localhost my.Shells]# doc...
This article example shares the specific code of ...
We know that in general, a function must be calle...