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 classSELECT 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 classSELECT
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_NUMFROM 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
/