To create a new view in MySQL, you can use CREATE VIEW
statement. 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 .MERGE
TEMPTABLE
UNDEFINED
-
Using the
MERGE
algorithm, 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 useMERGE
the algorithm. If the SELECT statement no reference table, it is also not allowed to use theMERGE
algorithm. If notMERGE
the algorithm, MySQL will change the algorithmUNDEFINED
. Note that, in the view definition of the input queries and query are combined into a query called view resolution .
- The use of
TEMPTABLE
algorithms, MySQL First, according to the definition of the viewSELECT
to 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, theTEMPTABLE
efficiency of the algorithm than theMERGE
lower efficiency of the algorithm. In addition, the use ofTEMPTABLE
algorithms view is not updated.
-
When you create a view without specifying an explicit algorithm
UNDEFINED
is the default algorithm.UNDEFINED
MySQL can choose to use the algorithm or algorithms. MySQL precedence algorithm algorithm, because the higher efficiency of the algorithm.MERGE
TEMPTABLE
MERGE
TEMPTABLE
MERGE
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 SELECT
statement, the presence of any table or view can query data from the database. SELECT
Statement must follow the following rules:
SELECT
In the statement WHERE contain sub-query clause, but theFROM
clause does not contain sub-queries.SELECT
Statement can not refer to any variable, including local variables, user variables, and session variables.SELECT
Statement can not refer parameters prepared statement.
Please note that the
SELECT
statement does not require any reference table.
3. Create a MySQL view example
Creating a simple view
Let's look at orderDetails
the table. Based on orderDetails
the 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 TABLES
command 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 | +--------------------+------------+
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
CREATE VIEW aboveAvgProducts AS SELECT productCode, productName, buyPrice FROM products WHERE buyPrice > (SELECT AVG(buyPrice) FROM products) ORDER BY buyPrice DESC;