SQL CREATE VIEW 語(yǔ)句
什么是視圖?
在 SQL 中,視圖是基于 SQL 語(yǔ)句的結(jié)果集的可視化的表。
視圖包含行和列,就像一個(gè)真實(shí)的表。視圖中的字段就是來(lái)自一個(gè)或多個(gè)數(shù)據(jù)庫(kù)教程中的真實(shí)的表中的字段。我們可以向視圖添加 SQL 函數(shù)、WHERE 以及 JOIN 語(yǔ)句,我們也可以提交數(shù)據(jù),就像這些來(lái)自于某個(gè)單一的表。
注釋:數(shù)據(jù)庫(kù)的設(shè)計(jì)和結(jié)構(gòu)不會(huì)受到視圖中的函數(shù)、where 或 join 語(yǔ)句的影響。
SQL CREATE VIEW 語(yǔ)法
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
注釋:視圖總是顯示最近的數(shù)據(jù)。每當(dāng)用戶查詢視圖時(shí),數(shù)據(jù)庫(kù)引擎通過(guò)使用 SQL 語(yǔ)句來(lái)重建數(shù)據(jù)。
SQL CREATE VIEW 實(shí)例
可以從某個(gè)查詢內(nèi)部、某個(gè)存儲(chǔ)過(guò)程內(nèi)部,或者從另一個(gè)視圖內(nèi)部來(lái)使用視圖。通過(guò)向視圖添加函數(shù)、join 等等,我們可以向用戶精確地提交我們希望提交的數(shù)據(jù)。
樣本數(shù)據(jù)庫(kù) Northwind 擁有一些被默認(rèn)安裝的視圖。視圖 "Current Product List" 會(huì)從 Products 表列出所有正在使用的產(chǎn)品。這個(gè)視圖使用下列 SQL 創(chuàng)建:
CREATE VIEW [Current Product List] AS
SELECT ProductID,ProductName
FROM Products
WHERE Discontinued=No我們可以查詢上面這個(gè)視圖:
SELECT * FROM [Current Product List]Northwind 樣本數(shù)據(jù)庫(kù)的另一個(gè)視圖會(huì)選取 Products 表中所有單位價(jià)格高于平均單位價(jià)格的產(chǎn)品:
CREATE VIEW [Products Above Average Price] AS
SELECT ProductName,UnitPrice
FROM Products
WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)
我們可以像這樣查詢上面這個(gè)視圖:
SELECT * FROM [Products Above Average Price]另一個(gè)來(lái)自 Northwind 數(shù)據(jù)庫(kù)的視圖實(shí)例會(huì)計(jì)算在 1997 年每個(gè)種類的銷售總數(shù)。請(qǐng)注意,這個(gè)視圖會(huì)從另一個(gè)名為 "Product Sales for 1997" 的視圖那里選取數(shù)據(jù):
CREATE VIEW [Category Sales For 1997] AS
SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales
FROM [Product Sales for 1997]
GROUP BY CategoryName
我們可以像這樣查詢上面這個(gè)視圖:
SELECT * FROM [Category Sales For 1997]我們也可以向查詢添加條件?,F(xiàn)在,我們僅僅需要查看 "Beverages" 類的全部銷量:
SELECT * FROM [Category Sales For 1997]
WHERE CategoryName='Beverages'
SQL 更新視圖
您可以使用下面的語(yǔ)法來(lái)更新視圖:
SQL CREATE OR REPLACE VIEW Syntax
CREATE OR REPLACE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
現(xiàn)在,我們希望向 "Current Product List" 視圖添加 "Category" 列。我們將通過(guò)下列 SQL 更新視圖:
CREATE VIEW [Current Product List] AS
SELECT ProductID,ProductName,Category
FROM Products
WHERE Discontinued=No
SQL 撤銷視圖
您可以通過(guò) DROP VIEW 命令來(lái)刪除視圖。
SQL DROP VIEW Syntax
DROP VIEW view_name
OR REPLACE VIEW `<your_view_name>`
下面為老外網(wǎng)站上的教程
your_view_name>`
AS
...其次是正常的SQL的SELECT。這個(gè)SELECT可以包含一個(gè)WHERE子句或其他需要,可以對(duì)SELECT語(yǔ)句放在別的事情。該方案是無(wú)止境的。這實(shí)際上取決于視圖的目的。
正如你可以看到在我們看來(lái),我們正在格式化的姓氏和名字。這是一個(gè)很常見(jiàn)的事做有一個(gè)觀點(diǎn),即我們已經(jīng)這樣做了保存有寫在每一個(gè)查詢的WHERE這是一個(gè)要求的功能。你也可以看到,我們已經(jīng)采取的出生日期列和計(jì)算年齡。
執(zhí)行視圖
執(zhí)行一個(gè)SQL視圖
下面的例子顯示所有從視圖代碼。你也可以做一個(gè)SELECT*,或進(jìn)一步限制列你想看到的。您還可以添加額外的行限制的看法,因?yàn)槲覀兊淖龇ā?/P>
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
/
更多信息請(qǐng)查看IT技術(shù)專欄