MySQL View My Opinion

What is a database view

Creating a database view is based SQL SELECT queryand JOINthe. Tables and views are very similar, it also contains rows and columns, it can be directly query operation. In addition most of the database allows the same UPADTEoperations, but must meet certain conditions. View of the data structure shown in FIG:

image

We need to understand the database and no data associated storage view, but the view of the definition of storage is appropriate SQL SELECT and JOIN.

Then use the database view in the end what advantage does:

  • Your view can simplify complex query : defining the view is based on a query statement, the query statement may associate a lot of the underlying table. We can use the table view hide the complexities of the underlying relationship to an external program or user database.
  • View can restrict access to a particular user's data : Sometimes we want to hide some of the data tables for some specific user, then the view can be very good to help us achieve this function.
  • You can be calculated using the column view : We know the table columns generally do not support dynamic calculations, but the view is supported by columns. Assuming there is a order_detailstable, which contains product_numsand price_eachtwo, when we need, orderto calculate the total amount in code when we need, the results, if we use the words may be added to the view in a view total_price(product_nums*price_each). This allows queries directly out orderof the total price.
  • View can help us compatibility with older systems : Suppose we have a data center, the data center is a lot of programs in use. If one day we decided to redesign the data center to accommodate new business requirements, you may need to delete some old tables, and create a new table, but we do not want these changes affect those old programs. Then we can create a view to adapting those old programs.

MySQL View

MySQL version 5.x support from view, and in line with SQL: 2003 standard.
View MySQL query execution mode there are about two ways:

  • MySQL will merge input query statement queries and views and then execute the statement after the merger and returns the result.
  • MySQL will create a query based on the declaration view of the Temporary the Table , when executing queries this querytemporary table

If not specified when creating the view query, MySQL will default to use the first priority, but if the query statement in view of SELECTthe use of aggregate functions ( MIN, MAX, SUM, COUNT, AVG, etc., or DISTINCT, GROUP BY, HAVING, LIMIT, UNION, UNION ALL, subquery.), then the view of the query will use the second way.

Create View

MySQL can be used to create the view CREATE VIEWstatement:

CREATE 
   [ALGORITHM = {MERGE  | TEMPTABLE | UNDEFINED}]
VIEW [database_name].[view_name] 
AS
[SELECT  statement]

ALGORITHM:

MySQL There are three views of the execution policy, namely MERGE, TEMPTABLE, UNDEFINED.

  • Use MERGEstrategy declaration queries and views of MySQL will first enter the merge, and then execute the statement after the merger and return. However, if the query input is not allowed in a number of aggregate functions such as: MIN, MAX, SUM, COUNT, AVG, etc., or DISTINCT, GROUP BY, HAVING, LIMIT, UNION, UNION ALL, subquery. Similarly, if the view statement did not point to any data table, it is not allowed. If any of the above happens, MySQL will use the default UNDEFINEDstrategy.
  • Use TEMPTABLEstrategy, MySQL to create a claims-based view temporary table, when the input query will just look at this temporary table. Due to the need to create temporary tableto store the view result set TEMPTABLEof efficient than MERGElow strategy, in addition to the use of temporary tablepolicy views are not updated.
  • Use UNDEFINEDstrategy, if you create a view when not specified policy, MySQL using this policy by default. UNDEFINEDStrategies will automatically choose to use one of the above two strategies in the priority selection MERGEstrategy, can not be used would be transformed into TEMPTABLEpolicy.

View Name

Name of the view, the view and table names use the same namespace in MySQL, which means that table names and view names can not be repeated and to comply with the naming convention of the table name.

Select Statement

In view of the query statement you can query the database for all the data that already exists in the table, have focused on the following rules:

  • Query statement can WHEREuse sub-query but does not allow the conditions FROMof use of sub-query source.
  • Not allowed to reference any variables, including the query statement local variables, user variables, and session variables.
  • Query statement is not allowed in reference prepare statementparameters.

Note: query statement Fromcan reference other views

Example

CREATE VIEW customerOrders AS
    SELECT 
        d.orderNumber,
        customerName,
        SUM(quantityOrdered * priceEach) total
    FROM
        orderDetails d
            INNER JOIN
        orders o ON o.orderNumber = d.orderNumber
            INNER JOIN
        customers c ON c.customerNumber = c.customerNumber
    GROUP BY d.orderNumber
    ORDER BY total DESC;
  • Using Subqueries
CREATE VIEW aboveAvgProducts AS
    SELECT 
        productCode, productName, buyPrice
    FROM
        products
    WHERE
        buyPrice > 
 (SELECT 
                AVG(buyPrice)
            FROM
                products)
    ORDER BY buyPrice DESC;

Query View

SELECT * FROM customerOrders;

Source: https://www.jianshu.com/p/b11430bc4fba

Published 132 original articles · won praise 175 · views 900 000 +

Guess you like

Origin blog.csdn.net/wwd0501/article/details/104056546
Recommended