MySQL must know must learn notes Chapter 22 using views

MySQL 5 adds support for views.

The view is a virtual table. What it contains is the result of a query. It does not contain data itself, but is just a facility for viewing data stored elsewhere. When adding or changing data in these tables, the view will return the changed data.

View applications:
1. Reuse SQL statements.
2. Simplify complex SQL operations, after writing a query, you can easily reuse it without knowing its basic details.
3. Use part of the table instead of the entire table.
4. To protect data, you can grant users access to specific parts of the table instead of access to the entire table.
5. Change the data format and representation, the view can return data that is different from the representation and format of the underlying table.

After a view is created, it can be used like a table. You can perform SELECT operations on the view, filter and sort data, connect the view to other views or tables, and even add and update data.

Because the view does not contain data, every time you use the view, you must handle any retrieval during the query execution. When multiple joins and filters are used to create complex views or nested views, performance may drop a lot. Before deploying an application that uses a lot of views, perform a performance test.

Rules for using views:
1. The view name must be unique and cannot be the same as the existing table name.
2. The number of views that can be created is unlimited.
3. To create a view, you must have sufficient access rights.
4. Views can be nested, you can use queries that retrieve data from other views to construct a view.
5. ORDER BY can be used in a view. If the SELECT that retrieves data from the view also contains ORDER BY, the ORDER BY in the view is overwritten.
6. The view cannot be indexed, nor can it have triggers or default values.
7. Views can be used with tables, such as connecting together.

Use view:
1. View creation:

CREATE VIEW viewName AS
SELECT语句

2. Use the SHOW CREATE VIEW viewName;view statement to create the view.
3. Delete the view DROP VIEW viewName.
4. When updating the view, you can do it DROPagain CREATEor use it CREATE OR REPLACE VIEW.

If a WHERE clause is used when retrieving data from a view, this WHERE clause will be automatically combined with the WHERE clause in the view (WHERE when creating the view).

Part of the view can be updated, and the base table will be updated when INSERT, UPDATE, or DELETE are used on the view.

If MySQL cannot correctly determine the updated base data, it is not allowed to update. If the view definition has the following operations, the view cannot be updated:
1. Grouping.
2. Connect.
3. Subquery.
4. And.
5. Aggregate function.
6. DISTINCT.
7. Calculate (export) columns.

Maybe MySQL will remove some of the above restrictions after 5.

Generally, the view should be used for retrieval rather than modification.

Guess you like

Origin blog.csdn.net/tus00000/article/details/111563111