MySQL Must Know 14 Views: Simplified Query

The reading is compiled from "MySQL Must Know and Know" - Zhu Xiaofeng . For details, please log in to the purchase column on the official website of Geek Time .


A view is a virtual table , which can store a query statement in the database as a view. When needed, the view can be regarded as a table to query the data in it.

Views do not actually store data, and avoid possible redundancy in the process of data storage, improving storage efficiency.

view creation

create [or replace]
view 视图名称 [(字段列表)]
as 查询语句

data preparation:

mysql> select * from test.trans;
+---------+------------+---------------+------------+---------------------+
| transno | itemnumber | salesquantity | salesvalue | transdate           |
+---------+------------+---------------+------------+---------------------+
|    3451 |          1 |             1 |         89 | 2020-12-01 00:00:00 |
|    3451 |          2 |             1 |          5 | 2020-12-01 00:00:00 |
|    3452 |          3 |             2 |         20 | 2020-12-02 00:00:00 |
+---------+------------+---------------+------------+---------------------+
3 rows in set (0.00 sec)

mysql> select * from test.goodsmaster;
+------------+---------+-----------+------------+
| itemnumber | barcode | goodsname | salesprice |
+------------+---------+-----------+------------+
|          1 | 001     ||         89 |
|          2 | 002     ||          5 |
|          3 | 003     | 胶水      |         10 |
+------------+---------+-----------+------------+
3 rows in set (0.00 sec)

mysql> select * from test.inventoryhist;
+------------+-------------+---------------------+
| itemnumber | invquantity | invdate             |
+------------+-------------+---------------------+
|          1 |         100 | 2020-12-01 00:00:00 |
|          2 |          99 | 2020-12-01 00:00:00 |
|          3 |          88 | 2020-12-01 00:00:00 |
|          1 |         149 | 2020-12-02 00:00:00 |
|          2 |         105 | 2020-12-02 00:00:00 |
|          3 |         200 | 2020-12-02 00:00:00 |
+------------+-------------+---------------------+

In the case of not using the view, through the associated query of the sales flow table and the product information table, the results of daily product sales statistics are obtained, including the sales date, product name, the total of the daily sales quantity and the total of the daily sales amount, as follows Show:

mysql> select a.transdate, a.itemnumber, b.goodsname, sum(a.salesquantity) as quantity, sum(a.salesvalue) as salesvalue
    -> from test.trans as a
    -> left join test.goodsmaster as b on (b.itemnumber = a.itemnumber)
    -> group by a.transdate, b.goodsname;
+---------------------+------------+-----------+----------+------------+
| transdate           | itemnumber | goodsname | quantity | salesvalue |
+---------------------+------------+-----------+----------+------------+
| 2020-12-01 00:00:00 |          1 ||        1 |         89 |
| 2020-12-01 00:00:00 |          2 ||        1 |          5 |
| 2020-12-02 00:00:00 |          3 | 胶水      |        2 |         20 |
+---------------------+------------+-----------+----------+------------+

In the actual project, it is found that the frequency of daily product sales query is very high, and it is often necessary to conduct further statistics based on the results of this query.

For example, a supermarket operator wants to check "the comparison between the sales quantity of commodities every day and the inventory quantity of the day". If a SQL statement is used to query, it will be more complicated.

  • Subquery: A query nested within another query
  • Derived table: use the result of the subquery as a table in the query, this table is the derived table
select a.transdate, a.itemnumber, a.goodsname, a.quantity as quantity, b.invquantity as invquantity
from 
(select a.transdate, a.itemnumber, b.goodsname, 
sum(a.salesquantity) as quantity, 
sum(a.salesvalue) as salesvalue
from test.trans as a
left join test.goodsmaster as b on (b.itemnumber = a.itemnumber)
group by a.transdate, b.goodsname) as a   -- 派生表,与库存表进行连接
left join test.inventoryhist as b on (b.itemnumber = a.itemnumber and b.invdate = a.transdate);
+---------------------+------------+-----------+----------+-------------+
| transdate           | itemnumber | goodsname | quantity | invquantity |
+---------------------+------------+-----------+----------+-------------+
| 2020-12-01 00:00:00 |          1 ||        1 |         100 |
| 2020-12-01 00:00:00 |          2 ||        1 |          99 |
| 2020-12-02 00:00:00 |          3 | 胶水      |        2 |         200 |
+---------------------+------------+-----------+----------+-------------+

Use view query

  1. create view

    create view test.trans_goodsmaster as   -- 创建视图
    select a.transdate, a.itemnumber, b.goodsname, sum(a.salesquantity) as quantity, sum(a.salesvalue) as salesvalue
    from test.trans as a
    left join test.goodsmaster as b on (b.itemnumber = a.itemnumber)
    group by a.transdate, a.itemnumber;
    
    mysql> select * from test.trans_goodsmaster;
    +---------------------+------------+-----------+----------+------------+
    | transdate           | itemnumber | goodsname | quantity | salesvalue |
    +---------------------+------------+-----------+----------+------------+
    | 2020-12-01 00:00:00 |          1 ||        1 |         89 |
    | 2020-12-01 00:00:00 |          2 ||        1 |          5 |
    | 2020-12-02 00:00:00 |          3 | 胶水      |        2 |         20 |
    +---------------------+------------+-----------+----------+------------+
    
  2. Use view query

    select a.transdate, a.itemnumber, a.goodsname, a.quantity as quantity, b.invquantity as invquantity
    from test.trans_goodsmaster as a
    left join test.inventoryhist as b on (b.itemnumber = a.itemnumber and b.invdate = a.transdate)
    group by a.transdate, a.itemnumber;
    
    +---------------------+------------+-----------+----------+-------------+
    | transdate           | itemnumber | goodsname | quantity | invquantity |
    +---------------------+------------+-----------+----------+-------------+
    | 2020-12-01 00:00:00 |          1 ||        1 |         100 |
    | 2020-12-01 00:00:00 |          2 ||        1 |          99 |
    | 2020-12-02 00:00:00 |          3 | 胶水      |        2 |         200 |
    +---------------------+------------+-----------+----------+-------------+
    

The query results are the same as those using derived tables. However, query statements using views are significantly simpler, more readable, and easier to maintain.


Manipulating Views and Data

Modify view:

alter view 视图名
as 查询语句;

View view:

describe 视图名;

Delete view:

drop view 视图名;

insert data

mysql> create view test.view_goodsmaster as
    -> select itemnumber,barcode,goodsname,specification,saleprice
    -> from demo.goodsmaster;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test.view_goodsmaster (itemnumber, barcode, goodsname, saleprice)
    -> values (5, '0005', 'test', 100);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test.view_goodsmaster;
+------------+---------+-----------+---------------+-----------+
| itemnumber | barcode | goodsname | specification | saleprice |
+------------+---------+-----------+---------------+-----------+
|          1 | 0001    || 16|     89.00 |
|          2 | 0002    || NULL          |      5.00 |
|          5 | 0005    | test      | NULL          |    100.00 |
+------------+---------+-----------+---------------+-----------+

mysql> select * from demo.goodsmaster;  -- 查看实际数据表
+------------+---------+-----------+---------------+------+-----------+
| itemnumber | barcode | goodsname | specification | unit | saleprice |
+------------+---------+-----------+---------------+------+-----------+
|          1 | 0001    || 16||     89.00 |
|          2 | 0002    || NULL          ||      5.00 |
|          5 | 0005    | test      | NULL          | NULL |    100.00 |
+------------+---------+-----------+---------------+------+-----------+

It should be noted that only if the fields in the view are exactly the same as the fields in the actual data table, MySQL allows data to be inserted through the view.

delete data

mysql> delete from test.view_goodsmaster
    -> where itemnumber = 5;
Query OK, 1 row affected (0.01 sec)

mysql> select * from test.view_goodsmaster;
+------------+---------+-----------+---------------+-----------+
| itemnumber | barcode | goodsname | specification | saleprice |
+------------+---------+-----------+---------------+-----------+
|          1 | 0001    || 16|     89.00 |
|          2 | 0002    || NULL          |      5.00 |
+------------+---------+-----------+---------------+-----------+
3 rows in set (0.00 sec)

mysql> select * from demo.goodsmaster;
+------------+---------+-----------+---------------+------+-----------+
| itemnumber | barcode | goodsname | specification | unit | saleprice |
+------------+---------+-----------+---------------+------+-----------+
|          1 | 0001    || 16||     89.00 |
|          2 | 0002    || NULL          ||      5.00 |
+------------+---------+-----------+---------------+------+-----------+
2 rows in set (0.00 sec)

Both the view and the original data table are deleted.

change the data

mysql> update test.view_goodsmaster
    -> set saleprice = 90 where itemnumber = 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test.view_goodsmaster;
+------------+---------+-----------+---------------+-----------+
| itemnumber | barcode | goodsname | specification | saleprice |
+------------+---------+-----------+---------------+-----------+
|          1 | 0001    || 16|     90.00 |
|          2 | 0002    || NULL          |      5.00 |
|          6 | 0006    | test      | NULL          |    100.00 |
+------------+---------+-----------+---------------+-----------+
3 rows in set (0.00 sec)

mysql> select * from demo.goodsmaster;
+------------+---------+-----------+---------------+------+-----------+
| itemnumber | barcode | goodsname | specification | unit | saleprice |
+------------+---------+-----------+---------------+------+-----------+
|          1 | 0001    || 16||     90.00 |
|          2 | 0002    || NULL          ||      5.00 |
|          6 | 0006    | test      | NULL          | NULL |    100.00 |
+------------+---------+-----------+---------------+------+-----------+

Both the view and the original data table have been changed.

It is not recommended to update the data of the view , because MySQL allows more complex SQL query statements to create views (such as the use of grouping and aggregation functions in SQL query statements, or UION and DISTINCT keywords), so, through the The update of this result set to update the actual data table may not be allowed, because MySQL has no way to accurately locate the records in the actual data table.


summary

View advantages:

  • Treat the view as a table for query, modularize and simplify the query, and improve the efficiency of development and maintenance.
  • The view is different from the actual data table, it stores query statements. The view itself does not store data and does not occupy data storage resources.
  • Views are isolated. A view is equivalent to adding a layer of virtual tables between the user and the actual data table. Users do not need to query the data table, and can directly obtain the information in the data table through the view.
  • The data structure of the view is relatively independent. Even if the structure of the actual data table changes, the fields in the query result set can remain unchanged by modifying the query statement that defines the view.

shortcoming:

  • While creating views and simplifying queries, the database maintenance cost caused by too many views should also be considered.
  • The more views, the better, especially nested views (that is, creating views based on views). It is not recommended to use them, because the logic is complex, the readability is not good, and it is easy to become a potential hidden danger of the system.

Guess you like

Origin blog.csdn.net/qq_31362767/article/details/124511088