Detailed explanation of MySQL view management view example [add, delete, modify and query operations]

Detailed explanation of MySQL view management view example [add, delete, modify and query operations]

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 | [email protected] |
| Mary | Hill | x4611 | [email protected] |
| Jeff | Firrelli | x9273 | [email protected] |
| William | Patterson | x4871 | [email protected] |
| Gerard | Bondur | x5408 | [email protected] |
| Anthony | Bow | x5428 | [email protected] |
| Leslie | Jennings | x3291 | [email protected] |
.............. Many many data is omitted here..................................
| Martin | Gerard | x2312 | [email protected] |
| Lily | Bush | x9111 | [email protected] |
| John | Minsu | x9112 | [email protected] |
+-----------+-----------+-----------+--------------------------------+
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 | [email protected] | President |
| Mary | Hill | x4611 | [email protected] | VP Sales |
| Jeff | Firrelli | x9273 | [email protected] | VP Marketing |
................... A large amount of data is omitted here...................................................
| Yoshimi | Kato | x102 | [email protected] | Sales Rep |
| Martin | Gerard | x2312 | [email protected] | Sales Rep |
| Lily | Bush | x9111 | [email protected] | IT Manager |
| John | Minsu | x9112 | [email protected] | 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:
  • Using PHP to access the MySql database logical operations and examples of adding, deleting, modifying and checking
  • PHP implements the object-oriented mysqli extension library to add, delete, modify and query operation tool class
  • A simple example of adding, deleting, modifying and checking PHP MySql
  • PHP+MySQL implements simple add, delete, modify and query functions
  • PHP+MYSQL implements user addition, deletion, modification and query
  • MySQL detailed single table add, delete, modify and query CRUD statements
  • MySQL trigger trigger add, delete, modify and query operation example
  • MySQL and PHP basics and applications: add, delete, modify and query

<<:  Detailed explanation of Docker daemon security configuration items

>>:  Detailed explanation of how to use Vue self-nested tree components

Recommend

How to allow external network access to mysql and modify mysql account password

The root account of mysql, I usually use localhos...

Vue realizes the whole process of slider drag verification function

Rendering Define the skeleton, write HTML and CSS...

Debian virtual machine created by VirtualBox shares files with Windows host

the term: 1. VM: Virtual Machine step: 1. Downloa...

Detailed explanation of three methods of JS interception string

JS provides three methods for intercepting string...

Implementation of building Kubernetes cluster with VirtualBox+Ubuntu16

Table of contents About Kubernetes Basic environm...

Detailed explanation of type protection in TypeScript

Table of contents Overview Type Assertions in syn...

JS implements city list effect based on VUE component

This article example shares the specific code for...

Detailed explanation of PHP+nginx service 500 502 error troubleshooting ideas

Overview When a 500 or 502 error occurs during ac...

Analysis of MySQL's method of exporting to Excel

This article describes how to use MySQL to export...

Vue implements the method of displaying percentage of echart pie chart legend

This article mainly introduces the pie chart data...

Advanced and summary of commonly used sql statements in MySQL database

This article uses examples to describe the common...

Axios project with 77.9K GitHub repository: What are the things worth learning?

Table of contents Preface 1. Introduction to Axio...