視圖的創(chuàng)建
- 視圖用
create view
語句來創(chuàng)建 - 使用
show create view viewname
來查看創(chuàng)建視圖的語句 - 用
drop view viewname
刪除視圖 - 更新視圖是,可以先用drop再用create,也可以直接用
create or replace view
利用視圖簡(jiǎn)化復(fù)雜的聯(lián)結(jié)
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';
使用視圖重新格式化檢索出來的數(shù)據(jù)
\1.PNG
\2.PNG
使用視圖過濾不想要的數(shù)據(jù)
create view customeremaillist as
select cust_id, cust_name, cust_email
from cutomers
where cust_email is not null;
使用視圖與計(jì)算字段
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必知必會(huì)