2020-4-3 三-(七) 视图

/**
 *虚表:从一个表或几个表(或视图)导出的表
 *只存放视图的定义,不会出现数据冗余
 *基表中的数据发生变化,从视图中查询出的数据也随之改变

 *最多的是 查询
 */
 /**
 *with check option
 *透过视图进行增删改查操作的时候,不得破坏视图定义中的谓词条件
 */
 /**作用
  1.能够简化用户操作
  2.可以使用户以多种角度看待同一数据
  3.对重构数据库提供了一定程度的逻辑独立性
  4.能够对机密数据提供安全性的保护
  5.可以更加清晰的表达查询
  */
 --行列子集视图:去除了某些行和列,保留了码


--建立信息系学生的视图,包括学号,姓名,年龄
--create view is_student(sno,sname,sage)
--as/*来自哪张表*/
--select sno,sname,sage
--from student
--where sdept='IS';

--select *
--from is_student

--insert into is_student/*实际插入的是student表*/
--values('201215131','杨康',19)


--create view is_student1(sno,sname,sage)
--as/*来自哪张表*/
--select sno,sname,sage
--from student
--where sdept='IS'
--with check option/*加上约束,这样插入的数据就只能是 sno,sname,sage 插入,不可以插入别的属性*/

--insert into is_student1/*实际插入的是student表*/
--values('201215132','洪七公',38)


--create view is_student2(sno,sname,sage)
--as/*来自哪张表*/
--select sno,sname,Sdept
--from student
--where sdept='IS'

--insert into is_student2
--values('201215133','黄药师','MA')/**/
--with check option 将对视图正确与否很重要
 

 --2)建立信息系选修了1号课程的学生视图,包括学号,姓名 成绩
 --create view is_s1(sno,sname,grade)
 --as
 --select student.sno,sname,grade
 --from student,sc
 --where student.sno=sc.sno and
	--   sdept='IS' and
	--   cno='1'

--select *
--from is_s1

--3)查询信息系选修了1号课程并且成绩大于90的信息
--create view is_s2  /*省略了字段*/
--as
--select * 
--from is_s1
--where grade>90

--select *
--from is_s2

--4)建立学生基本情况的视图,包括 学号 姓名 出生年份
--create view bt_s(sno,sname,sbirth)/*由于2020-sage没有名字,所以在这里必须注明字段*/
--select sno,sname,2020-sage
--from student

/**三种情况下必须加字段
 *计算
 *聚集函数
 *连接的表
 */



--5)建立每个学生以及他平均成绩的视图
--create view s_g(sno,Gavg)
--as
--select sno,avg(grade)
--from sc
--group by sno

--select *
--from s_g

--6)建立所有女同学的视图,包括所有属性
--create view f_student(no,name,sex,sage,sdept)
--as
--select sno,sname,ssex,sage,sdept
--from student
--where ssex='女'

--select*
--from f_student


--删除视图
--drop view is_student
--drop view is_s1/*按理说不能删,因为is_s2是由is_s1推导出来的,但是在这个里面可以删*/
--drop view is_s2/*由于已经删除了is_s1,所以is_s2已经没有用了*/

--在信息系学生的视图中找到
--select *
--from is_student
--where sage<20
--视图消解
--select sno,sname,sage
--from student
--where sage<20 and sdept='IS'

--查找平均成绩大于90分
--select sno,gavg
--from s_g
--where gavg>90
--视图消解 有聚集函数的消解为  having
--select sno,avg(grade)
--from sc
--group by sno
--having avg(Grade)>90 /*where子句中不可以出现聚集函数*/



发布了33 篇原创文章 · 获赞 5 · 访问量 685

猜你喜欢

转载自blog.csdn.net/u013140841/article/details/105380170
今日推荐