[MySQL Must Know and Know (19)] [Use View]

Previous: [MySQL Must Know and Know (18)] [Create and manipulate tables]

+++++++++++++Start line++++++++++++++++

One, the view

The view is a virtual table. Unlike tables that contain data, views only contain queries that dynamically retrieve data when used.

1.1 Why use views

1. Reuse SQL statements
2. Simplify complex SQL statements
3. Use the components of the table instead of the entire table
4. Protect data, you can grant users access to specific parts of the table instead of 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

A view is only a facility for viewing data stored elsewhere. The views themselves do not contain data, so the data they return is retrieved from other tables. When adding or changing data in these tables, the view will return the changed data.

1.2 View rules and restrictions

1. Like the table, the view must be uniquely named
2. There is no limit
to the number of views that can be created 3. In order to create a view, you must have sufficient access rights
4. Views can be nested
5. ORDER BY can be used in the view, but if Retrieving data from the view SELECT also contains ORDER BY, then the ORDER BY in the view will be overwritten
6. The view cannot be indexed, nor can it have associated triggers or default values
7. The view can be used with the table

Second, use the view

1. Use the CREATE VIEW statement to create the view
. 2. Use SHOW CREATE VIEW viewname; to view the statement that created the view.
3. Use DROP to delete the view. The syntax is DROP VIEW viewname;
4. When updating the view, you can use DROP first and then CREATE , You can also use CREATE ORREPLACE VIEW directly. If the view to be updated does not exist, the second update statement will create a view; if the view to be updated exists, the second update statement will replace the original view.

2.1 Use views to simplify complex connections

mysql> CREATE VIEW productcustomers AS
    -> SELECT cust_name, cust_contact, prod_id
    -> FROM customers, orders, orderitems
    -> WHERE customers.cust_id = orders.cust_id
    -> AND orderitems.order_num = orders.order_num;

analysis

This statement joins three tables and returns a list of all customers who have ordered any product

Retrieve customers who ordered product TNT2

mysql> SELECT cust_name, cust_contact
    -> FROM productcustomers
    -> WHERE prod_id = 'TNT2';

Insert picture description here

2.2 Reformatting the retrieved data with the view

mysql> CREATE VIEW vendorlocations AS
    -> SELECT Concat(Rtrim(vend_name),'(',Rtrim(vend_country),')')
    -> AS vend_title
    -> FROM vendors
    -> ORDER BY vend_name;

analysis

Data retrieved to create all mailing labels

Insert picture description here

2.3 Use the view to filter unwanted data

Define the customeremaillist view to filter customers who do not have email addresses

mysql> CREATE VIEW customeremaillist AS
    -> SELECT cust_id, cust_name, cust_email
    -> FROM customers
    -> WHERE cust_email IS NOT NULL;

Insert picture description here

2.4 Using views and calculated fields

Retrieve items in a particular order and calculate the total price of each item

mysql> CREATE VIEW orderitemsexpanded AS
    -> SELECT order_num,
    -> prod_id,
    -> quantity,
    -> item_price,
    -> quantity*item_price AS expanded_price
    -> FROM orderitems;

Search the details of order 20005

Insert picture description here

2.5 Update view

The view is updatable, updating a view will update its base table. Adding or deleting rows to the view is actually adding or deleting rows to its base table.

If there are the following operations in the view definition, the view cannot be updated:
1. Grouping (GROUP BY and HAVING)
2. Joining
3. Subquery
4. Parallel
5. Aggregate function
6. DISTINCT
7. Export (calculation) column

Use view for retrieval

Generally, the view should be used for retrieval and not for update

+++++++++++++End line++++++++++++++++

Next: [MySQL Must Know and Know (20)] [Use Stored Procedures]

Guess you like

Origin blog.csdn.net/qq_42893334/article/details/108909470