首页 > 数据库技术 > 详细

20141110--SQL视图

时间:2014-11-10 17:09:31      阅读:269      评论:0      收藏:0      [点我收藏+]
--------------------------视图---------------------
--视图是一个虚拟表,数据来自于原表,原表的数据改变,视图也会改变
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

bubuko.com,布布扣

bubuko.com,布布扣

查询视图结果:

bubuko.com,布布扣

20141110--SQL视图

原文:http://www.cnblogs.com/Tirisfal/p/4087286.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!