CREATE VIEW productcustomers AS
SELECT cust_name, cust_contact,prod_id
FROM customers, orders, orderitems
WHERE customers.cust_id = orders.cust_id
AND orderitems.order_num = orders.order_num;SELECT cust_name,cust_contact FROM productcustomers
WHERE prod_id= 'TNT2';
2) 用視圖可以重新格式化檢索出的數據
SELECT Concat(RTrim(vend_name),'(',RTrim(vend_country),')')
AS vend_title
FROM vendors
ORDER BY vend_name;
CREATE VIEW vendorlocations AS
SELECT Concat(RTrim(vend_name),'(',RTrim(vend_country),')')
As vend_title
FROM vendors
ORDER BY vend_name ;
SELECT *
FROM vendorlocations;
3) 用視圖過濾不必要的數據
CREATE VIEW customeremaillist AS
SELECT cust_id,cust_name,cust_email FROM customers
WHERE cust_email IS NOT NULL;
SELECT *
FROM customeremaillist;
4) 使用視圖結合計算字段進行統計分析
SELECT prod_id,quantity,item_price,quantity*item_price
AS expanded_price
FROM orderitems
WHERE order_num=20005;
CREATE VIEW orderitemsexpanded AS
SELECT order_num,prod_id,quantity,item_price,quantity *
item_price
as expanded_price
FROM orderitems;
SELECT * FROM orderitemsexpanded
WHERE order_num=20005;
第二部分:所選課題數據庫的應用 1)
CREATE VIEW product AS
SELECT cust_name, cust_contact,prod_id
FROM customers, orders, orderitems
WHERE customers.cust_id = orders.cust_id
AND orderitems.order_num = orders.order_num;
SELECT cust_name,cust_contact FROM product
WHERE prod_id= 'FB';
2)
CREATE VIEW vendorsss AS
SELECT Concat('(',RTrim(vend_zip),')')
As vend_title
FROM vendors
ORDER BY vend_name ;
SELECT *
FROM vendorsss;
3)
CREATE VIEW customerssss AS
SELECT cust_id,cust_name,cust_email FROM customers
WHERE cust_email IS NULL;
SELECT *
FROM customerssss;
4)
CREATE VIEW orderitemssss AS
SELECT order_num,prod_id,quantity,item_price,quantity * item_price
as expanded_price
FROM orderitems;
SELECT *
FROM orderitemssss
WHERE order_num=20009;