2020-05-07

虚表,是从一个或几个基本表(或视图)到出的表 (查看数据的一种方式)只存放视图的定义,不存放视图对应的数据基表的数据发生变化,从视图中查询出的数据也随之改变基于视图视图操作查询删除受限更新定义基于该视图的新视图建立视图 CREATE VIEW <视图名>[(<列名>[,<列名>]…)]as <子查询>[with check option];所有视图操作必须加上子查询的where条件省略视图各个属性列名,则隐含该视图有子查询中select子句目标列中诸子段组成必须明确指定组成视图所有列名的情形(1)某个目标不单纯的属性名,而是集函数或列表达式(2).多表链接时选出几个同名列作为视图的字段(3)需要在视图中为某个列启用新的更是适合的名字1.创建视图(来自基本表的数据)建立学生视图create view view1as select sno,sname,sdept,sagefrom studentwhere sdept='is’更新操作insert into view1 values(‘08000’,‘shj’,‘cs’,‘19’)create or replace view view1 – 创建或更新as select sno,sname,sdept,sagefrom studentwhere sdept='is’with check OPTIONinsert into view1 values(‘08011’,‘jhs’,‘soft’,‘19’)-- 不能执行insert into view1 values(‘08011’,‘jhs’,‘is’,‘19’)带有 with check option 选项时对IS_Student视图的更新操作修改操作:自动加上sdept=‘is’ 的条件(子查询的where条件)删除 操作:自动加上sdept='is’条件插入操作: 自动检查sdept属性是否为’is’的条件 如果不是,则拒绝该插入操作**基于多个基表的视图建立选修1号课程的学生视图create view view2 – view2视图名as select student.,sc.grade,cname-- 查询学生所有信息from student ,sc,coursewhere student.sno=sc.sno and sc.cno=course.cno and sc.cno=‘001’;create or replace view view2 – (指定列名)as select student.,sc.*,cname – 里面没有指定 sno from student ,sc,coursewhere student.sno=sc.sno and sc.cno=course.cno and sc.cno=‘001’;建立选修1号课程且成绩在90分以上的学生的视图create or replace view view3 as select * from view2where grade>90;-- 来源于已经存在的视图带表达式的视图定义一个学生出生年份的视图create view BT_S(sno,sname,sbirth)AS – 在视图中起了列别名select sno,sname,2020-sagefrom student;将学生平均成绩CREATE view view4(sno,Gavg)as SELECT sno,avg(grade)-- 求平均成绩from scgroup by sno;-- 按学号分组二 删除视图drop view <视图名>删除指定的视图定义删除视图 1drop view 1;执行此语句后,1视图的定义将从数据字典中删除。由1视图导出1的视图已无法使用,但其定义虽然仍在数据字典中查询视图由视图转向基本表在信息系学生的视图中找出年龄小于20岁的学生create or replace view view1 – 创建或更新as select sno,sname,sdept,sagefrom studentwhere sdept='is’with check OPTIONselect *from view1WHERE sage<20;-- 对视图查询转换成对基本表的查询select sno,sname,sdept,sagefrom student where sage<20 and sdept=‘is’;查询1号课程的信息系学生select bt_s.sno,snamefrom bt_s,scwhere bt_s.sno=sc.sno and sc.cno=‘001’;

你可能感兴趣的:(笔记)