SQL CREATE VIEW 語句
什麼是視圖?
在 SQL 中
視圖包含行和列
注釋
SQL CREATE VIEW 語法
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
注釋
SQL CREATE VIEW 實例
可以從某個查詢內部
樣本數據庫 Northwind 擁有一些被默認安裝的視圖
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 數據庫的視圖實例會計算在
CREATE VIEW [Category Sales For] AS
SELECT DISTINCT CategoryNameSum(ProductSales) AS CategorySales
FROM [Product Sales for]
GROUP BY CategoryName
我們可以像這樣查詢上面這個視圖
SELECT * FROM [Category Sales For
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
現在
CREATE VIEW [Current Product List] AS
SELECT ProductIDProductName 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 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 || || a LASTNAME) AS "STUDENT"
FROM STUDENTS a
JOIN CLASSREGISTRATION b
ON aSTUDENT_ID = b STUDENT_ID_FK
JOIN CLASSES c
ON c
/
Above is a simple view that provides us with a listing of occupied/unoccupied seats for our classes
Using our View
View a single classSELECT COURSE
SEAT_NUM
STUDENT
FROM VW_OCCUPIED_SEATS_BY_CLASS
WHERE COURSE =Perl and STUDENT <>
/
COURSE SEAT_NUM STUDENT
PerlMadge Lowdown
PerlRobert Frapples
PerlMary Lamacker
PerlHelga Joens
PerlMaggie Jomomma
PerlMary Meigh
PerlJONES
PerlBob JONES
PerlTed Applebee
PerlJon Nesbitt
PerlMary Lamacker
PerlMark Jackson
Count open seats by classSELECT
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
CREATE OR REPLACE View vw_NAME_CONFLICT
AS
SELECT
aCLASSES_NUM
bCLASSES_NUM FROM CLASSES a
JOIN CLASSESREGISTRATION b
ON aCLASSES_NUM = b CLASSES_NUM
/
Duplicate column name
Here is how to resolve this issue
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 = b CLASSES_NUM /
Drop a View
DROP VIEW COURSEREGISTRATIONVW_NAME_CONFLICT
/
From:http://tw.wingwit.com/Article/program/Oracle/201311/19084.html