MySQL - Section 12 - MySQL View Features

Table of contents

1. MySQL View Features

2. Basic use

2.1. Prepare the test form

2.2. Create view

2.3. Modify the view to affect the base table

2.4. Modify the base table to affect the view

2.5. Delete view

3. View Rules and Restrictions


1. MySQL View Features

The concept of view:

• A view is a virtual table whose contents are defined by a query. Like a real table, a view contains a series of named columns and rows of data.
• The data in the view will not be stored in the database alone. The data comes from the table (base table) referenced by the query when the view is defined, and is dynamically generated each time the view is referenced.
• Since the view and the base table use the same data, the modification of the view will affect the base table, and the modification of the base table will also affect the view.


2. Basic use

2.1. Prepare the test form

The employee table and department table are used as test tables below. ename in the employee table represents the name of the employee, and deptno represents the department number of the department where the employee belongs. as follows:

In the department table, dname represents the department name, and deptno represents the department number of the department. as follows:

2.2. Create view

The SQL to create a view is as follows:

CREATE VIEW view_name AS SELECT ...;

Explain:

• When creating a view, the select statement will be executed first, and then the query result will be used to create the view.

When we want to query each employee and its corresponding department name, we need to use the employee table and department table to perform multi-table query, and filter out the records where the employee's department number is equal to the department's department number. as follows:

If the query result will be frequently used, then we can create a view for the above query result, and the view can be seen through the show command after creation. as follows:

And in the directory corresponding to the database, a corresponding xxx.frm file will be added, but there is no corresponding xxx.ibd file, which also proves that the view and the base table use the same data. as follows:

After creating the view, you can directly view each employee and the corresponding department name by querying the view. as follows:

2.3. Modify the view to affect the base table

By querying the employee table, you can see that the department number of employee CLARK is 10. as follows:

Further query the department table, you can see that the department name of department 10 is ACCOUNTING. as follows:

After changing the department name of employee CLARK's department to HR in the view, you will see that the department names of some other employees' departments have also changed to HR. as follows:

The fundamental reason is that the view and the base table use the same data. After changing the department name of Clark's department in the view to HR, the department name of department 10 in the department table also becomes HR. as follows:

The employees in department 10 also have KING and MILLER, so when the view is queried again after modification, the department names corresponding to these two employees will also change to HR. as follows:

2.4. Modify the base table to affect the view

By querying the employee table, you can see that the department number of employee JAMES is 30. as follows:

The department name of department 30 is SALES, so when you query the view, you can see that the department name of JAMES is SALES. as follows:

Now change the department number corresponding to employee JAMES to 20 in the employee table. as follows:

After modification, query the view, and you will find that the department name of the department where JAMES is located has changed to RESEARCH, the department name of department 20. as follows:

2.5. Delete view

The SQL for deleting a view is as follows:

DROP VIEW view_name;

For example, after deleting the view just created, the view will no longer be visible in the database. as follows:

And the xxx.frm file corresponding to the view in the database directory will also be deleted. as follows:


3. View Rules and Restrictions

Viewing rules and restrictions:

• Like ordinary tables, the names of views must be unique, and the names of views or tables with the same name cannot appear.
• There is no limit to the number of views you can create, but consider the performance impact of complex queries created as views.
• Views cannot be indexed, nor can they have associated triggers or default values.
• Views improve security and must have sufficient access rights to access a view.
• An order by clause can be used when creating a view, but if an order by clause is also included when retrieving data from the view, the order by clause in the view will be overwritten.
• Views can be used with ordinary tables, such as multi-table query (cartesian product), inner and outer joins, etc.

Guess you like

Origin blog.csdn.net/qq_45113223/article/details/131516016