SQL CREATE VIEW 语句
什么是视图?
在 SQL 中视图是基于 SQL 语句的结果集的可视化的表
视图包含行和列就像一个真实的表视图中的字段就是来自一个或多个数据库教程中的真实的表中的字段我们可以向视图添加 SQL 函数WHERE 以及 JOIN 语句我们也可以提交数据就像这些来自于某个单一的表
注释数据库的设计和结构不会受到视图中的函数where 或 join 语句的影响
SQL CREATE VIEW 语法
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
注释视图总是显示最近的数据每当用户查询视图时数据库引擎通过使用 SQL 语句来重建数据
SQL CREATE VIEW 实例
可以从某个查询内部某个存储过程内部或者从另一个视图内部来使用视图通过向视图添加函数join 等等我们可以向用户精确地提交我们希望提交的数据
样本数据库 Northwind 拥有一些被默认安装的视图视图 "Current Product List" 会从 Products 表列出所有正在使用的产品这个视图使用下列 SQL 创建
CREATE VIEW [Current Product List] AS
SELECT ProductIDProductName
FROM Products
WHERE Discontinued=No我们可以查询上面这个视图
SELECT * FROM [Current Product List]Northwind 样本数据库的另一个视图会选取 Products 表中所有单位价格高于平均单位价格的产品
CREATE VIEW [Products Above Average Price] AS
SELECT ProductNameUnitPrice
FROM Products
WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)
我们可以像这样查询上面这个视图
SELECT * FROM [Products Above Average Price]另一个来自 Northwind 数据库的视图实例会计算在 年每个种类的销售总数请注意这个视图会从另一个名为 "Product Sales for " 的视图那里选取数据
CREATE VIEW [Category Sales For ] AS
SELECT DISTINCT CategoryNameSum(ProductSales) AS CategorySales
FROM [Product Sales for ]
GROUP BY CategoryName
我们可以像这样查询上面这个视图
SELECT * FROM [Category Sales For ]我们也可以向查询添加条件现在我们仅仅需要查看 "Beverages" 类的全部销量
SELECT * FROM [Category Sales For ]
WHERE CategoryName=Beverages
SQL 更新视图
您可以使用下面的语法来更新视图
SQL CREATE OR REPLACE VIEW Syntax
CREATE OR REPLACE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
现在我们希望向 "Current Product List" 视图添加 "Category" 列我们将通过下列 SQL 更新视图
CREATE VIEW [Current Product List] AS
SELECT ProductIDProductNameCategory
FROM Products
WHERE Discontinued=No
SQL 撤销视图
您可以通过 DROP VIEW 命令来删除视图
SQL DROP VIEW Syntax
DROP VIEW view_name
OR REPLACE VIEW `<your_view_name>`
下面为老外网站上的教程
your_view_name>`
AS
其次是正常的SQL的SELECT这个SELECT可以包含一个WHERE子句或其他需要可以对SELECT语句放在别的事情该方案是无止境的这实际上取决于视图的目的
正如你可以看到在我们看来我们正在格式化的姓氏和名字这是一个很常见的事做有一个观点即我们已经这样做了保存有写在每一个查询的WHERE这是一个要求的功能你也可以看到我们已经采取的出生日期列和计算年龄
执行视图
执行一个SQL视图
下面的例子显示所有从视图代码你也可以做一个SELECT*或进一步限制列你想看到的您还可以添加额外的行限制的看法因为我们的做法
SELECT FIRSTNAME
LASTNAME
BIRTH_DTTM
FULLNAME_FL
AGE
FROM VW_STUDENTS
WHERE AGE IS NOT NULL
/
Creating a View containing one or more SQL Tables
Another key advantage of a view is that it allows us to join multiple tables together
CREATE OR REPLACE VIEW vw_OCCUPIED_SEATS_BY_CLASS
AS
SELECT
cCOURSE_DESIGNATER_FK AS "COURSE"
bSEAT_NUM
(aFIRSTNAME || || aLASTNAME) AS "STUDENT"
FROM STUDENTS a
JOIN CLASSREGISTRATION b
ON aSTUDENT_ID = bSTUDENT_ID_FK
JOIN CLASSES c
ON cCLASSES_NUM = bCLASSES_NUM
/
Above is a simple view that provides us with a listing of occupied/unoccupied seats for our classes As you can see from the examples below we can use this view in a variety of different ways Note that for each scenario that we did not need to join any tables The grunt work is already done
Using our View
View a single class
SELECT COURSE
SEAT_NUM
STUDENT
FROM VW_OCCUPIED_SEATS_BY_CLASS
WHERE COURSE = Perl and STUDENT <>
/
COURSE SEAT_NUM STUDENT
Perl Madge Lowdown
Perl Robert Frapples
Perl Mary Lamacker
Perl Helga Joens
Perl Maggie Jomomma
Perl Mary Meigh
Perl JONES
Perl Bob JONES
Perl Ted Applebee
Perl Jon Nesbitt
Perl Mary Lamacker
Perl Mark Jackson
Count open seats by class
SELECT
COURSE
COUNT(SEAT_NUM) "# Open Seats"
FROM VW_OCCUPIED_SEATS_BY_CLASS
GROUP BY COURSE
/
COURSE # Open Seats
dbOrchestra
Perl
Column name considerations
The column name MUST be unique in a view Note the following example
CREATE OR REPLACE View vw_NAME_CONFLICT
AS
SELECT
aCLASSES_NUM
bCLASSES_NUM
FROM CLASSES a
JOIN CLASSESREGISTRATION b
ON aCLASSES_NUM = bCLASSES_NUM
/
Duplicate column name classes_num
Here is how to resolve this issue Create a unique name using "as"
CREATE OR REPLACE VIEW vw_NAME_CONFLICT
AS
SELECT aCLASSES_NUM "CLASSES_CLASSES_NUM"
bCLASSES_NUM "CLASSREGISTRATION_CLASSES_NUM"
FROM CLASSES a
JOIN CLASSREGISTRATION b
ON aCLASSES_NUM = bCLASSES_NUM
/
Drop a View
DROP VIEW COURSEREGISTRATIONVW_NAME_CONFLICT
/