視圖的創建
- 視圖用
create view
語句來創建 - 使用
show create view viewname
來查看創建視圖的語句 - 用
drop view viewname
刪除視圖 - 更新視圖是,可以先用drop再用create,也可以直接用
create or replace view
利用視圖簡化復雜的聯結
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';
使用視圖重新格式化檢索出來的數據
\1.PNG
\2.PNG
使用視圖過濾不想要的數據
create view customeremaillist as
select cust_id, cust_name, cust_email
from cutomers
where cust_email is not null;
使用視圖與計算字段
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;
參考書籍:
- MySQL必知必會