数据库实验—视图定义与操作

(1)定义“IS”系学生基本情况视图V_IS 并查询结果

create view V_IS 
as 
select Sno, Sname, Sage from S where Sdept = 'IS';

(2)将S,C,SC表中学生的学号,姓名,课程号,课程名,成绩定义为视图V_S_C_G并查询结果

create view V_S_C_G 
ss select S.Sno, Sname, C.Cno, Cname, Grade
from S, C,SC where S.Sno = SC.Sno and C.Cno = SC.Cno;

  (3)将各系学生人数,平均年龄定义为视图V_NUM_AVG并查询结果

create view V_NUM_AVG
as select count(Sno) as NUM, avg(Sage) as AVG
from S group by Sdept;

 (4)定义一个反映学生出生年份的视图V_YEAR并查询结果

create view V_YEAR 
as select (2020 – Sage) as YEAR from S;

 (5)将各位学生选修课程的门数及平均成绩定义为视图V_AVG_S_G并查询结果

create view V_AVG_S_G
as 
select count(Cno) as NUM, avg(Grade) as AVG
from SC group by Sno;

 (6)将各门课程的选修人数及平均成绩定义为视图V_AVG_C_G并查询结果

create view V_AVG_C_G
as 
select Cno,count(Sno) as NUM,avg(Grade) as AVG
from SC group by Cno;

(7)查询平均成绩为90分以上的学生学号、姓名和成绩

select Sno,Sname, Grade from V_S_C_G 
where Sno in 
select Sno from V_S_C_G 
group by Sno 
having avg(Grade) >=90;

 (8)查询各课成绩均大于平均成绩的学生学号、姓名、课程和成绩

select * from V_S_C_G
where Sno in 
(select X.Sno from V_S_C_G X
where not exists
(select Y.Grade from V_S_C_G Y
where Grade <=
(select avg(Grade) from V_S_C_G Z 
where (Y.Cno = Z.Cno) and (X.Sno = Y.Sno)))); 

(9)按系统计各系平均成绩在80分以上的人数,结果按降序排列

select Sdept,count(Sno) as NUM. from S
where Sno in 
(select V_S_C_G.Sno from V_S_C_G 
group by V_S_C_G.Sno 
having avg(Grade)>=80) 
group by Sdept;

(10)通过视图V_IS,分别将学号为“S1”和“S4”的学生姓名更改为“S1_MMM”,”S4_MMM” 并查询结果

update V_IS set Sname = ‘周芷若’ where Sno =1’;

update V_Is set Sname = ‘乔峰’ where Sno =4’;

select * from S;

(11)通过视图V_IS,新增加一个学生记录 ('S12','YAN XI',19,'IS'),并查询结果

insert into V_IS(Sno ,Sname, Sage,Sdept) values
('S12', 'YAN XI', 19. 'IS')

(12)通过视图V_IS,新增加一个学生记录 ('10','木婉清',19,'IS'),并查询结果

insert into V_IS(Sno, Sname, Sage, Sdept)values
('10','木婉清',19,'IS');

select * from S;

(13)通过视图V_IS,删除学号为“S12”和“S3”的学生信息,并查询结果

delete from V_IS 
where Sno = ‘s12’ or Sno = ‘S3’

(14)要通过视图V_S_C_G,将学号为“S12”的姓名改为“S12_MMM”,是否能实现?

可以;
update V_SC_G set Sname = ‘S12_MMM’ 
where Sno = ‘S12’;

(15)要通过视图V_AVG_S_G,将学号为“S1”的平均成绩改为90分,是否可以实现?

不能实现。因为V_AVG_S_G视图的字段来自聚集函数,里面有group by;

你可能感兴趣的:(数据库实验—视图定义与操作)