亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

創建mysql視圖

標簽:
MySQL

Summary: in this tutorial, you will learn how to create views in MySQL by using the CREATE VIEW statement.

Introducing to CREATE VIEW statement

The syntax of creating a view in MySQL is as follows:

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

 

Algorithms

The algorithm attribute allows you to control which mechanism is used when creating a view. MySQL provides the MERGETEMPTABLE and UNDEFINED algorithms.

  • MERGE means the input query will be combined with the SELECT statement of the view definition. MySQL will execute the combined query to return the result set. This mechanism is more efficient than TEMPTABLE (temporary table) but MERGE only allowed when the rows in the view represent a one-to-one relationship with the rows in the underlying table. In case the MERGE is not allowed, MySQL will switch the algorithm to UNDEFINED. The combination of input query and query in view definition into one query sometimes refers as view resolution.

  • TEMPTABLE means that MySQL first creates a temporary table based on the SELECT statement of the view definition, and then it executes the input query against this temporary table. Because MySQL has to create temporary table to store the result set and move the data from the physical tables to the temporary table, the TEMPTABLE algorithm is less efficient than the MERGE algorithm. In addition, a view that uses TEMPTABLE algorithm is not updateable.

  • UNDEFINED is the default algorithm when you create a view without specifying an explicit algorithm. The UNDEFINED algorithm allows MySQL to make a decision whether to use MERGE or TEMPTABLE. MySQL prefers MERGE to TEMPTABLE, which is more efficient.

View name

Each view is associated with a specific database therefore you can have database name prefix with the view name. Names of views share the same domain with tables therefore they cannot be the same names as tables in a database.

SELECT statement

In the SELECT statement, you can query data from any table or view that exists in the database. There are several rules that the SELECT statement must follow:

  • The SELECT statement can contain a subquery in WHERE clause but not in the FROM clause.

  • The SELECT statement cannot refer to any variable including local variable, user variable or session variable.

  • The SELECT statement cannot refer to the parameters of prepared statements.

MySQL create view examples

Create a simple view

Let’s take a look at the orderDetails table. We can create a view that represents total sales per order.

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

If you want to query total sales for each sales order, you just need to execute a simple SELECT statement against the SalePerOrder view as follows:

SELECT total  FROM salePerOrder WHERE orderNumber = 10102

 

Create view with JOIN

The following is an example of creating a view with an INNER JOIN statement. The view contains order numbercustomer name and total sales per order.

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 O.customerNumber =  C.customerNumber    GROUP BY D.orderNumber   ORDER BY total DESC

 

Create view with subquery

The following illustrates how to create a view with subquery. The view contains products whose buy prices are higher than average price of all products.

CREATE VIEW vwProducts  AS   SELECT productCode,         productName,         buyPrice   FROM products  WHERE buyPrice > (       SELECT AVG  (buyPrice)       FROM  products  )  ORDER BY buyPrice DESC

In this tutorial, we have shown you how to create views by using the CREATE VIEW statement.

Related Tutorials

原文链接:http://outofmemory.cn/mysql/view/create-sql-views-mysql

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消