In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This paper describes the management view operation of mysql view with an example. Share with you for your reference, the details are as follows:
Mysql provides SHOW CREATE VIEW statements for displaying view definitions. Let's take a look at the syntax structure:
SHOW CREATE VIEW [database_name]. [view_ name]
To display the definition of the view, 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 show the organizational structure of the company, and then demonstrate:
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
Query the data from the above view and 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 the view, use the SHOW CREATE VIEW statement as follows:
SHOW CREATE VIEW organization
We can also use any plain text editor, such as notepad, to display the view definition 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, and then go into it and find the .frm file by your view name.
Let's try to modify the view through ALTER VIEW and CREATE OR REPLACE VIEW. Let's take a 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.lastame.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. Let's not dwell on it, but let's 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 change, if a view already exists, mysql will only change the view. If the view does not exist, mysql creates a new view. All right, let's take a look at the results of the above sql implementation:
+-+ | 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.. | 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 the position (jobtitle) column to the v_contacts view, simply 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, you can see that a column of data has been 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 wave 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
When we're done, let's use the DROP VIEW statement to delete the view. Let's first take a 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 to avoid the error of deleting a view that does not exist. When we're done, let's delete the organization view:
DROP VIEW IF EXISTS organization
We should note that every time a view is modified or deleted, mysql backs up the view definition file to the / database_name/arc/ directory. If we accidentally modify or delete the view, we can get its backup from the / database_name/arc/ folder.
All right, that's all for this record.
More readers who are interested in MySQL-related content can check out this site topic: "MySQL query skills Collection", "MySQL transaction Operation skills Summary", "MySQL stored procedure skills Collection", "MySQL Database Lock related skills Summary" and "MySQL Common function Summary".
It is hoped that what is described in this article will be helpful to everyone's MySQL database design.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.