[MySQL Series] View Features

The content of the "Preface" article is roughly MySQL transaction management.

"Attribution Column" MySQL

"Homepage link" personal homepage

"Author" Mr. Maple Leaf (fy)

MySQL

view

1.1 View concept

  • 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
  • Data changes in the view will affect the base table (original table), and data changes in the base table will also affect the view.

Notice: The view here Read Viewhas nothing to do with the previous one

1.2 Create a view

Prepare the test table and employee information table.
Insert image description here
Assuming that you want to query each employee and its corresponding department name, you 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

select ename, dname from emp,dept where emp.deptno=dept.deptno;

Insert image description here
If the query results will be used frequently, then we can create a view for the above query results.

The syntax for creating a view is as follows:

create view 视图名 as select语句;

Note: When creating a view, the select statement will be executed first, and then the view will be created using the results obtained from the query.

For example, to create a view for the above table

create view myview as select ename, dname from emp,dept where emp.deptno=dept.deptno;

The table is also viewed through the show command
Insert image description here
. When the view is created, a corresponding xxx.frmfile will be added in the directory corresponding to the database, but there is no corresponding xxx.ibdfile. This also proves that the view and the base table use the same data.
Insert image description here
Query view
Insert image description here

1.3 Modifications affect each other

Modifying the base table affects the view

Modify the name "CLARK" in the emp table to lowercase "clark".
Insert image description here
After modifying it, query the view and find that the content in the view has also been modified.
Insert image description here

Modifying the view affects the base table

Modify the name "SIMTH" in the view, change it to lowercase "smith"
Insert image description here
and then query the emp table, and find that the content has also been modified.
Insert image description here
The above experiment shows that the view and the base table share the same data

1.4 Delete a view

The syntax for deleting a view is as follows:

drop view 视图名;

For example, after deleting the view you just created, you will no longer be able to see this view in the database.
Insert image description here

1.5 View Rules and Restrictions

  • Like ordinary tables, the naming of views must also be unique. Views or table names with the same name cannot appear.
  • There is no limit to the number of views created, but the performance impact of creating complex queries as views must be considered.
  • Views cannot have indexes added, nor can they have associated triggers or default values.
  • Views can improve security, and you must have sufficient access rights when accessing the view
  • clauses can be used when creating a view order by, but if data is retrieved from the view that also contains order bythe clauses, the ones in the view order bywill be overwritten
  • Views can be used together with ordinary tables, such as multi-table queries, internal and external joins, etc.

The above is the content of the view, you can simply understand it
--------------------- END ------------------ ----

「 作者 」 枫叶先生
「 更新 」 2023.9.11
「 声明 」 余之才疏学浅,故所撰文疏漏难免,
          或有谬误或不准确之处,敬请读者批评指正。

Guess you like

Origin blog.csdn.net/m0_64280701/article/details/132773679