MySQL视图和存储过程
一. 视图
视图是一种特殊的表,但不包含表中应有的任何列和数据,只包含使用时动态检索数据的查询(即:sql查询语句)。
使用视图的目的,在本质上就是用来查看存储在别处的数据。
【引例】
/*使用sql查询*/
select cust_name,cust_contact from customers,orders,orderitems
where customers.cust_id=orders.cust_id
and orderitems.order_num=orders.order_num
and prod_id='TNT2';
查询结果
cust_name
cust_contact
1
Yosemite Place
Y Sam
2
Coyote Inc.
Y Lee
/*创建视图*/
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';
查询结果
cust_name<