为什么要使用视图:
简化复杂的SQL操作,在编写查询后,可以方便地重用它而不必知道其基本查询细节。
使用表的一部分而不是整个表。
保护数据,可以授予用户访问表的特定部分的权限,而不是整个表的访问权限。
更改数据格式和表示。视图可返回与底层表的表示和格式不同的数据。

创建视图
create view newspaper as
select n.n_id,n.n_title,t.t_id
from nrc_news as n,nrc_type as t
where n.t_id=t.t_id;
检索数据
select * from newspaper
where t_id=10;

使用视图格式化检索出的数据
select rtrim(n_title)+'('+RTRIM(t_id)+')' as news from newspaper
where t_id=10;

create view topID10 as
select rtrim(n_title)+'('+RTRIM(t_id)+')' as news from newspaper
where t_id=10;
使用视图进行计算
select n_id,t_id,n_id*t_id as value from newspaper

posted on 2015-10-12 14:40  baraka  阅读(260)  评论(0编辑  收藏  举报