MySQL-- create a view (3)

To create a new view in MySQL, you can use CREATE VIEWstatement. Create a view in MySQL syntax is as follows:

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

1. Check processing algorithms

Algorithm attribute allows you to control the use of MySQL mechanisms when creating the view, MySQL provides three algorithms: , and  .MERGETEMPTABLE UNDEFINED

  • Using the MERGEalgorithm, MySQL first input query SELECT statement defining the view into a single query. Then execute MySQL query returns a result set combination. If the SELECT statement contains an aggregate function (such as MIN , MAX , SUM , COUNT , AVG , etc.) or DISTINCT , the GROUP BY , the HAVING , LIMIT , UNION , UNION ALL , the subquery is not allowed to use MERGEthe algorithm. If the SELECT statement no reference table, it is also not allowed to use the MERGEalgorithm. If not MERGEthe algorithm, MySQL will change the algorithm UNDEFINED. Note that, in the view definition of the input queries and query are combined into a query called view resolution .

  • The use of TEMPTABLEalgorithms, MySQL First, according to the definition of the view SELECTto create a temporary table statement, and then execute the query entered for the temporary table. Because MySQL must create a temporary table to store the result set from the data base table is moved to the temporary table, the TEMPTABLEefficiency of the algorithm than the MERGElower efficiency of the algorithm. In addition, the use of TEMPTABLEalgorithms view is not updated.
  • When you create a view without specifying an explicit algorithm UNDEFINEDis the default algorithm. UNDEFINEDMySQL can choose to use the algorithm or algorithms. MySQL precedence algorithm algorithm, because the higher efficiency of the algorithm.MERGETEMPTABLEMERGETEMPTABLEMERGE

2, view the name

In the database, tables and views share the same namespace, views and tables and therefore can not have the same name. In addition, the name of the view must follow the naming table.

SELECT statement

In the SELECTstatement, the presence of any table or view can query data from the database. SELECTStatement must follow the following rules:

  • SELECTIn the statement WHERE contain sub-query clause, but the FROMclause does not contain sub-queries.
  • SELECTStatement can not refer to any variable, including local variables, user variables, and session variables.
  • SELECTStatement can not refer parameters prepared statement.

Please note that the SELECTstatement does not require any reference table.

3. Create a MySQL view example

Creating a simple view

Let's look at orderDetailsthe table. Based on orderDetailsthe table to create a view of total sales representation for each order.

CREATE VIEW SalePerOrder AS
    SELECT 
        orderNumber, SUM(quantityOrdered * priceEach) total
    FROM
        orderDetails
    GROUP by orderNumber
    ORDER BY total DESC;

To know which object is a table or view, use the SHOW FULL TABLEScommand as follows:

mysql> SHOW FULL TABLES;
+--------------------+------------+
| Tables_in_yiibaidb | Table_type |
+--------------------+------------+
| article_tags       | BASE TABLE |
| contacts           | BASE TABLE |
| customers          | BASE TABLE |
| departments        | BASE TABLE |
| employees          | BASE TABLE |
| offices            | BASE TABLE |
| offices_bk         | BASE TABLE |
| offices_usa        | BASE TABLE |
| orderdetails       | BASE TABLE |
| orders             | BASE TABLE |
| payments           | BASE TABLE |
| productlines       | BASE TABLE |
| products           | BASE TABLE |
| saleperorder       | VIEW       |
+--------------------+------------+
The results set table_type column specifies which object is a view, which object is a table (base table). As indicated above, saleperorder the corresponding table_type column is: VIEW

Create a view using the connection table

The following is an INNER JOIN example creates a view. This view contains the customer number and the total amount paid by the customer.

CREATE VIEW customerOrders AS
    SELECT 
        c.customerNumber,
        p.amount
    FROM
        customers c
            INNER JOIN
        payments p ON p.customerNumber = c.customerNumber
    GROUP BY c.customerNumber
    ORDER BY p.amount DESC;

Create a view using subqueries

Here's how to use a subquery to create a view that contains the price is higher than the average price of all products of the product.
CREATE VIEW aboveAvgProducts AS
    SELECT 
        productCode, productName, buyPrice
    FROM
        products
    WHERE
        buyPrice > 
 (SELECT 
                AVG(buyPrice)
            FROM
                products)
    ORDER BY buyPrice DESC;

 

Guess you like

Origin www.cnblogs.com/yuezc/p/12661469.html