MySQL数据库之创建mysql视图
小标 2019-01-04 来源 : 阅读 1372 评论 0

摘要:本文主要向大家介绍了MySQL数据库之创建mysql视图 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

本文主要向大家介绍了MySQL数据库之创建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 MERGE, TEMPTABLE 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 number, customer 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.


本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程