码迷,mamicode.com
首页 > 数据库 > 详细

20141110--SQL视图

时间:2014-11-10 17:10:02      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   sp   数据   div   on   

--------------------------视图---------------------
--视图是一个虚拟表,数据来自于原表,原表的数据改变,视图也会改变
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视图

标签:style   blog   http   color   使用   sp   数据   div   on   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!