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 ProductID,ProductName
FROM Products
WHERE Discontinued=No我们可以查询上面这个视图:
SELECT * FROM [Current Product List]Northwind 样本数据库的另一个视图会选取 Products 表中所有单位价格高于平均单位价格的产品:
CREATE VIEW [Products Above Average Price] AS
SELECT ProductName,UnitPrice
FROM Products
WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)
我们可以像这样查询上面这个视图:
SELECT * FROM [Products Above Average Price]另一个来自 Northwind 数据库的视图实例会计算在 1997 年每个种类的销售总数。请注意,这个视图会从另一个名为 "Product Sales for 1997" 的视图那里选取数据:
CREATE VIEW [Category Sales For 1997] AS
SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales
FROM [Product Sales for 1997]
GROUP BY CategoryName
我们可以像这样查询上面这个视图:
SELECT * FROM [Category Sales For 1997]我们也可以向查询添加条件。现在,我们仅仅需要查看 "Beverages" 类的全部销量:
SELECT * FROM [Category Sales For 1997]
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 ProductID,ProductName,Category
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_STUDENTS1
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
c.COURSE_DESIGNATER_FK AS "COURSE",
b.SEAT_NUM ,
(a.FIRSTNAME || ' ' || a.LASTNAME) AS "STUDENT"
FROM STUDENTS a
JOIN CLASSREGISTRATION b
ON a.STUDENT_ID = b.STUDENT_ID_FK
JOIN CLASSES c
ON c.CLASSES_NUM = b.CLASSES_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 = 'Perl100' and STUDENT <> '1'
/
COURSE SEAT_NUM STUDENT
----------------------------------
Perl100 1 Madge Lowdown
Perl100 2 Robert Frapples
Perl100 3 Mary Lamacker
Perl100 4 Helga Joens
Perl100 5 Maggie Jomomma
Perl100 6 Mary Meigh
Perl100 7 JONES
Perl100 8 Bob JONES
Perl100 9 Ted Applebee
Perl100 10 Jon Nesbitt
Perl100 11 Mary Lamacker
Perl100 12 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
----------------------------
dbOrchestra100 16
Perl100 12
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
a.CLASSES_NUM ,
b.CLASSES_NUM
FROM CLASSES a
JOIN CLASSESREGISTRATION b
ON a.CLASSES_NUM = b.CLASSES_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 a.CLASSES_NUM "CLASSES_CLASSES_NUM" ,
b.CLASSES_NUM "CLASSREGISTRATION_CLASSES_NUM"
FROM CLASSES a
JOIN CLASSREGISTRATION b
ON a.CLASSES_NUM = b.CLASSES_NUM
/
Drop a View
DROP VIEW COURSEREGISTRATION.VW_NAME_CONFLICT
/
更多信息请查看IT技术专栏