《数据库实验四答案(共7页).docx》由会员分享,可在线阅读,更多相关《数据库实验四答案(共7页).docx(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上数据库实验四答案 -实验四-1定义“IS”系学生基本情况视图V_IS 并查询结果;create view V_ISasselect *from Swhere Sdept='IS'with check option;select *from V_IS;-2将S,C,SC表中学生的学号,姓名,课程号,课程名,成绩定义为视图V_S_C_G并查询结果;create view V_S_C_Gasselect S.Sno,Sname,C.Cno,Cnamefrom S,C,SCwhere S.Sno=SC.Sno and C.Cno=SC.Cno;select *
2、from V_S_C_G;-3将各系学生人数,平均年龄定义为视图V_NUM_AVG并查询结果;create view V_NUM_AVGasselect count(Sno) DeptNum ,avg(Sage) DeptAvgAge,Sdeptfrom Sgroup by Sdept;select * from V_NUM_AVG;-4定义一个反映学生出生年份的视图V_YEAR并查询结果;create view V_YEARasselect Sno,2012-Sage birthdayfrom S;select *from V_YEAR;-5将各位学生选修课程的门数及平均成绩定义为视图V_A
3、VG_S_G并查询结果;create view V_AVG_S_Gasselect SC.Sno,count(SC.Cno) CountCno,avg(Grade) AvgGradefrom S,C,SCwhere S.Sno=SC.Sno and C.Cno=SC.Cno group by SC.Sno;select *from V_AVG_S_G;-6将各门课程的选修人数及平均成绩定义为视图V_AVG_C_G并查询结果;create view V_AVG_C_Gasselect count(SC.Sno) Cnum,avg(Grade) AvgGradefrom S,C,SCwhere S
4、.Sno=SC.Sno and C.Cno=SC.Cno group by SC.Cno;select *from V_AVG_C_G;-7查询平均成绩为90分以上的学生学号、姓名和成绩;select SC.Sno,Sname,avg(Grade) AvgGradefrom S,SCwhere S.Sno=SC.Sno group by SC.Sno,Snamehaving avg(Grade) >90;-8查询各课成绩均大于平均成绩的学生学号、姓名、课程和成绩;create view V_AVGasselect SC.Cno,avg(Grade) AvgGradefrom C,SCwh
5、ere C.Cno=SC.Cnogroup by SC.Cno;select distinct S.Sno,Sname,C.Cno,Gradefrom S,SC,C,V_AVGwhere S.Sno=SC.Sno and C.Cno=V_AVG.Cno and Grade >V_AVG.AvgGrade ;-9按系统计各系平均成绩在80分以上的人数,结果按降序排列;select Sdept,count(AvgGrade) Num from S,V_AVG_S_G where S.Sno=V_AVG_S_G.Sno and AvgGrade > 80 group by Sdept o
6、rder by Num desc;-10通过视图V_IS,分别将学号为“S1”和“S4”的学生姓名更改为“S1_MMM”,”S4_MMM” 并查询结果;insert into S(Sno,Sname,Ssex,Sage,Sdept) values('','三毛','男',21,'IS');insert into S(Sno,Sname,Ssex,Sage,Sdept) values('','紫薇','女',19,'IS');update V_IS set Sno=
7、39;S1_MMM' where Sno='' update V_IS set Sno='S4_MMM' where Sno='' select *from V_IS;-11通过视图V_IS,新增加一个学生记录 ('S12','YAN XI',19,'IS'),并查询结果;insert into V_IS(Sno,Sname,Sage,Sdept) values('S12','YAN XI',19,'IS'); select *from V_I
8、Swhere Sno='S12'-11通过视图V_IS,新增加一个学生记录 ('S13','YAN XI',19,'MA'),并查询结果;insert into V_IS(Sno,Sname,Sage,Sdept) values('S13','Y AN XI',19,'MA'); - Tip:由于该学生是MA系的,当有check约束时将会产生更新错误。-13通过视图V_IS,删除学号为“S12”和“S3”的学生信息,并查询结果;insert into V_IS(Sno,Sname,S
9、age,Sdept) values('S3','李杨',21,'IS'); select * from V_IS;deletefrom V_ISwhere Sno='S12'deletefrom V_ISwhere Sno='S3'select * from V_IS;-14要通过视图V_S_C_G,将学号为“S12”的姓名改为“S12_MMM”,是否可以实现?update V_S_C_G set Sname='S12_MMM' where Sno='S12' -15要通过视图V_AVG_S_G,将学号为“S1”的平均成绩改为90分,是否可以实现?- 不能实现,因为视图不是真正存在的,对视图的修改实际上是对基本表的修改, - 而平均分在基本表中不存在,涉及到多个成绩,所以不能修改。 专心-专注-专业