20141110--SQL视图

--------------------------视图---------------------
--视图是一个虚拟表,数据来自于原表,原表的数据改变,视图也会改变
select Student.Sno,sname,ssex,class,cno,degree from Student join Score on Student.Sno=Score.Sno 
--以下代码由系统自己生成,在左边的对象管理器中,右键视图-新建视图
SELECT     dbo.Student.Sno AS Expr2, dbo.Student.Sname AS Expr3, dbo.Student.Ssex AS Expr4, dbo.Student.Sbirthday AS Expr5, dbo.Student.Class AS Expr6, dbo.Score.Cno AS Expr7, 
                      dbo.Score.Degree AS Expr8
FROM         dbo.Student INNER JOIN
                      dbo.Score ON dbo.Student.Sno = dbo.Score.Sno
----------------------------------------
--用代码生成视图
--只能有表连接 join on, 不能出现聚合函数,等等
create view St_Sc
as
select Student.Sno,sname,ssex,class,cno,degree from Student join Score on Student.Sno=Score.Sno 
----------查询视图
select *from St_Sc
-----------------------不用 join on
create view ssc
as
select Student.Sno,Sname,Ssex,Sbirthday,Class,Score.Cno,Cname,Tno,Degree  
from Student,Course,Score where Student.Sno=Score.Sno and Score.Cno=Course.Cno
------------------使用 join on
create view sss
as
select Student.Sno,Sname,Ssex,Sbirthday,Class,Score.Cno,Cname,Tno,Degree from Score 
join Course on Score.Cno=Course.Cno
join Student on Student.Sno=Score.Sno

select *from ssc
select *from sss
------------修改视图
----需要重写,相当于删掉重建
alter view sss
as
select Student.Sno,Sname,Ssex,Class,Score.Cno,Cname,Tno,Degree from Score 
join Course on Score.Cno=Course.Cno
join Student on Student.Sno=Score.Sno

查询视图结果:

posted @ 2014-11-10 15:32  Tirisfal  阅读(156)  评论(0编辑  收藏  举报