实验题目:创建视图、删除视图
实验目的:
oracle 管理平台,针对公共用户 pub 下的表,完成创建视图、查询验证视图、删除视图。视图名为 test6_(题号,题号长度两位,前面补零),例如 test6_01。
实验原理和方法:
例如:找出年龄小于 20 岁的所有学生的学号、姓名、年龄 正确执行:
create view test6_00 as select sid,name,age from pub.student where age>20
Oracle扩展后方便写法:create or replace view test6_00 as select sid,name,age from pub.student where age>20
同实验五:sql很简单,懒得改锁进了:)
实验步骤:
找出年龄小于 20 岁且是“物理学院”的学生的学号、姓名、院系名称,按学号排序。
create or replace view test6_01 as
select sid,name,dname
from pub.student
where age<20 and dname='物理学院'
order by sid
查询统计 2009 级、软件学院每个学生的学号、姓名、总成绩(sum_score)。
create or replace view test6_02 as
select a.sid sid,a.name name,sum(b.score) sum_score
from pub.student a left join pub.student_course b
on a.sid=b.sid
where a.dname='软件学院' and a.class=2009
group by a.sid,a.name
查询 2010 级、计算机科学与技术学院、操作系统的学生成绩表,内容有学号、姓名、成绩。
create or replace view test6_03 as
select a.sid sid,a.name name,score
from pub.student a,pub.student_course b,pub.course c
where a.sid=b.sid and b.cid=c.cid
and dname='计算机科学与技术学院' and class=2010
and c.name='操作系统'
找出选修“数据库系统”课程,且成绩大于 90 的学生的学号、姓名
create or replace view test6_04 as
select a.sid sid,a.name name
from pub.student a,pub.student_course b,pub.course c
where a.sid=b.sid and b.cid=c.cid
and score>90 and c.name='数据库系统'
找出姓名叫“李龙”的学生的学号及其选修全部课程的课程号、课程名和成绩。
create or replace view test6_05 as
select a.sid sid,b.cid cid,c.name name,score
from pub.student a,pub.student_course b,pub.course c
where a.sid=b.sid and b.cid=c.cid and a.name='李龙'
找出选修了所有课程的学生的学号、姓名
create or replace view test6_06 as
select sid,name
from pub.student a
where not exists
((select cid from pub.course)
Minus
(select cid from pub.student_course
where sid=a.sid))
找出选修了所有课程并且所有课程全部通过的学生的学号、姓名
create or replace view test6_07 as
select sid,name
from pub.student a
where not exists
((select cid from pub.course)
minus
(select cid from pub.student_course
where sid=a.sid and score>=60))
检索先行课的学分为 2 的课程号、课程名。
create or replace view test6_08 as
select a.cid cid,a.name name
from pub.course a,pub.course b
where a.fcid=b.cid and b.credit=2
查询统计 2010 级、化学与化工学院的学生总学分表,内容有学号、姓名、总学分 sum_credit。
create or replace view test6_09 as
select a.sid sid,a.name name,sum(credit) sum_credit
from pub.student a,pub.student_course b,pub.course c
where a.sid=b.sid and b.cid=c.cid and score>=60
and class=2010 and dname='化学与化工学院'
group by a.sid,a.name
找出有间接先行课的所有课程的课程号、课程名称。
create or replace view test6_10 as
select a.cid cid,a.name name
from pub.course a,pub.course b
where a.fcid=b.cid and b.fcid is not null
结论分析与体会:
熟练掌握了视图的创建、查询验证和删除,oracle中create or replace解决了需要对错误视图首先进行删除再重复创建的问题。
就实验过程中遇到和出现的问题,你是如何解决和处理的,自拟1-3道问答题:
Q1:查询6:报错“ORA-00907: 缺失右括号”
Q2:exists拼错,一定要多加注意粗心错误,解决措施:不要对原sql语句进行复制粘贴,重新输入。