zoukankan      html  css  js  c++  java
  • 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

    查询视图结果:

  • 相关阅读:
    [框架] DAO的作用以及和其他层的关联
    [框架] SSH所需要的jar包列表
    DLBBS工作总结
    只不过是R.java文件的特性出错信息:R.java was modified manually! Reverting to generated version!
    [jQuery] jQuery函数
    WebTeam多层系统框架(请高手提意见)
    对象基础知识
    Android 4.0 的软件测试
    顺序存储数据结构java实现
    xp下清除多余的鼠标右键菜单
  • 原文地址:https://www.cnblogs.com/Tirisfal/p/4087286.html
Copyright © 2011-2022 走看看