山东大学软件学院2017-18学年数据库实验五、六、七、八

注:所有实验均为自己思考所答,非转载抄袭,并全部通过实验系统检测。希望学弟学妹自主思考,答案不唯一可能有些效率不高,仅供参考。

  • 实验五

实验步骤:(不要求罗列完整源代码)

create table test5_00 as select * from pub.TEACHER

grant all on test5_00 to user201600301315

update test5_00 set age = 88                         

select * from test5_00                                 

Commit

Rollback

update test5_00 set age = age + 1

Rollback

Commit

update test5_00 set age = age + 2

Commit

select * from test5_00                                 

Rollback

select * from userb201600301315.test5_00              

update test5_00 set age = age - 2

update test5_00 set age = age - 2

select * from test5_00                                 

select * from userb201600301315.test5_00             

Commit

select * from userb201600301315.test5_00              

Rollback

update userb201600301315.test5_00 set age = age – 10

Select * from test5_00                                 

create table test5_01 as select * from test5_00

Rollback

select * from userb201600301315.test5_00            

select * from userb201600301315.test5_00             

Rollback

select * from userb201600301315.test5_00            

 

结果为:88 90 90 86 90 90 86 86 76 86

  • 实验六

实验步骤:(不要求罗列完整源代码)

  1. 找出年龄小于20岁且是“物理学院”的学生的学号、姓名、院系名称,按学号排序

 

create view test6_01

as select sid,name,dname

from pub.STUDENT

where age<20 and dname='物理学院'

order by sid

 

  1. 查询统计2009级、软件学院的每个学生的学号、姓名、总成绩(sum_score)

 

create view test6_02 as

select pub.STUDENT.SID sid,pub.STUDENT.NAME name,sum(score) sum_score

from pub.STUDENT,pub.STUDENT_COURSE

where pub.STUDENT.SID=pub.STUDENT_COURSE.SID

and pub.STUDENT.CLASS=2009

and pub.STUDENT.DNAME='软件学院'

group by pub.STUDENT.SID,pub.STUDENT.NAME

 

  1. 查询统计2010级、计算机科学与技术学院、操作系统的学生成绩表,内容有学号姓名成绩。

 

create view test6_03 as

select pub.STUDENT.SID sid,pub.STUDENT.NAME name,pub.STUDENT_COURSE.SCORE score

from pub.STUDENT,pub.STUDENT_COURSE,pub.COURSE

where pub.STUDENT_COURSE.CID=pub.COURSE.CID

and pub.STUDENT.SID=pub.STUDENT_COURSE.SID

and pub.STUDENT.CLASS=2010

and pub.STUDENT.DNAME='计算机科学与技术学院'

and pub.COURSE.NAME = '操作系统'

 

  1. 找出选修“数据库系统”课程且成绩大于90学生的学号、姓名

 

create view test6_04 as select pub.STUDENT.SID sid,pub.STUDENT.NAME name

from pub.STUDENT,pub.STUDENT_COURSE,pub.COURSE

where pub.STUDENT_COURSE.CID=pub.COURSE.CID

and pub.STUDENT.SID=pub.STUDENT_COURSE.SID

and pub.COURSE.NAME = '数据库系统'

and pub.STUDENT_COURSE.SCORE>90

 

  1. 找出姓名叫“李龙”的学生的学号及其选修全部课程的课程号、课程名和成绩

 

create view test6_05 as select pub.STUDENT.SID sid,pub.STUDENT_COURSE.CID cid,pub.COURSE.NAME,pub.STUDENT_COURSE.SCORE

from pub.STUDENT,pub.STUDENT_COURSE,pub.COURSE

where pub.STUDENT_COURSE.CID=pub.COURSE.CID

and pub.STUDENT.SID=pub.STUDENT_COURSE.SID

and pub.STUDENT.NAME='李龙'

 

  1. 找出选修了所有课程的学生的学号、姓名

 

create or replace view test6_06 as

select a.SID,a.NAME

from pub.STUDENT a

where not exists

((select cid from pub.COURSE)

minus

(select cid

from  pub.STUDENT natural join pub.STUDENT_COURSE

where a.sid = sid))

 

  1. 找出选修了所有课程并且所有课程全部通过的学生的姓名、学号

 

create or replace view test6_07 as

select a.SID,a.NAME

from pub.STUDENT a

where not exists(

(select cid from pub.COURSE)

minus

(select cid

from  pub.STUDENT natural join pub.STUDENT_COURSE

where a.sid = sid and score>=60))

 

  1. 检索先行课的学分为2的课程号、课程名

 

create or replace view test6_08 as select A.cid, A.NAME

from pub.COURSE A, pub.COURSE B

where A.FCID = B.CID and B.CREDIT = 2

 

  1. 查询统计2010级、化学与化工学院的学生总学分表,内容有学号、姓名、总学分sum_credit

 

注:学分为成绩大于等于60才算

 

create or replace view test6_09 as

select pub.STUDENT.SID sid,pub.STUDENT.NAME name,sum(credit) sum_credit

from pub.STUDENT,pub.STUDENT_COURSE,pub.COURSE

where pub.STUDENT.SID=pub.STUDENT_COURSE.SID

and pub.STUDENT_COURSE.CID=pub.COURSE.CID

and pub.STUDENT.CLASS=2010

and pub.STUDENT.DNAME='化学与化工学院'

and pub.STUDENT_COURSE.SCORE>=60                     

group by pub.STUDENT.SID,pub.STUDENT.NAME

 

  1. 找出有间接先行课的所有课程的课程号、课程名称

 

create or replace view test6_10 as select A.CID,A.NAME

from pub.COURSE A,pub.COURSE B,pub.COURSE C

where A.FCID = B.CID and B.FCID = C.CID

 

  • 实验七

实验步骤:(不要求罗列完整源代码)

  1. 统计名字的使用频率

 

create table test7_01 as

select First_name, (count(*)) frequency

from (select (substr(name,2)) First_name from pub.STUDENT)

group by First_name

 

2.统计名字每个字的使用频率

 

create table test7_02 as

select letter,(count(*)) frequency

from

((select (substr(name,2,1)) letter from pub.STUDENT)

union all

(select (substr(name,3,1)) letter from pub.STUDENT where substr(name,3,1) is not NULL))

group by letter

 

3.统计学院班级学分达标情况统计表1

 

create table test7_03 as

select * from

(select dname, class, (count(*)) P_count1

 from (select S.sid, S.DNAME, S.CLASS, sum_credit

    from pub.STUDENT S,

            (select sid, (sum(credit)) sum_credit

         from pub.STUDENT_COURSE SC, pub.COURSE C

         where SC.CID = C.CID

         and SC.SCORE >= 60

         and dname is not NULL

         group by sid) B

    where S.SID = B.SID)

 where sum_credit > = 10

 and dname is not NULL

 group by dname, class)

 

natural full outer join

 

(select dname, class, (count(*)) P_count2

 from (select S.sid, S.DNAME, S.CLASS, sum_credit

       from pub.STUDENT S,

            (select sid, (sum(credit)) sum_credit

         from pub.STUDENT_COURSE SC, pub.COURSE C

         where SC.CID = C.CID

         and SC.SCORE >= 60

             and dname is not NULL

         group by sid) B

       where S.SID = B.SID)

 where sum_credit < 10

 and dname is not NULL

 group by dname, class)

 

natural full outer join

 

(select *

 from (select dname, class, (count(*)) P_count

       from pub.STUDENT

       where dname is not NULL

       group by dname, class) sum_count)

 

4.统计学院班级学分达标情况统计表2

 

create table test7_04 as

select * from

(select dname, class, (count(*)) P_count1

    from (select A.sid, A.DNAME, A.CLASS, sum_credit

       from pub.STUDENT A, (select sid, (sum(credit)) sum_credit

       from pub.STUDENT_COURSE S, pub.COURSE K

       where S.CID = K.CID

       and S.SCORE >= 60

       group by sid) B

       where A.SID = B.SID and A.DNAME is not NULL)

       where (sum_credit > = 10)

    or (sum_credit > = 8 and class <= 2008)   group by dname, class)

natural full outer join

(select dname, class, (count(*)) P_count2

    from (select A.sid, A.DNAME, A.CLASS, sum_credit

       from pub.STUDENT A, (select sid, (sum(credit)) sum_credit

       from pub.STUDENT_COURSE S, pub.COURSE K

       where S.CID = K.CID

       and S.SCORE >= 60

       group by sid) B

       where A.SID = B.SID)

    where sum_credit < 10

    and dname is not NULL

    group by dname, class)

natural full outer join

(select *

from (select dname, class, (count(*)) P_count

    from pub.STUDENT

    where dname is not NULL

    group by dname, class) sum_count) 

  • 实验八

实验步骤:(不要求罗列完整源代码)

  1. 查询各院系的数据结构平均成绩和操作系统平均成绩。

create table test8_01 as

select * from

(select dname, (round(avg(s1),0)) Avg_ds_score

from (select pub.STUDENT.SID, max(score) s1

from pub.STUDENT, pub.STUDENT_COURSE, pub.COURSE

where pub.STUDENT.SID = pub.STUDENT_COURSE.SID

and pub.STUDENT_COURSE.CID = pub.COURSE.CID

and pub.COURSE.NAME = '数据结构'

group by pub.STUDENT.SID) A,

pub.STUDENT B

where A.SID = B.SID

and dname is not NULL

group by dname)

 

natural full outer join

 

(select dname, (round(avg(s2),0)) Avg_os_score

  from (select pub.STUDENT.SID, max(score) s2

         from pub.STUDENT, pub.STUDENT_COURSE, pub.COURSE

         where pub.STUDENT.SID = pub.STUDENT_COURSE.SID

         and pub.STUDENT_COURSE.CID = pub.COURSE.CID

         and pub.COURSE.NAME = '操作系统'

         group by pub.STUDENT.SID) A,

         pub.STUDENT B

  where A.SID = B.SID

  and dname is not NULL

  group by dname)

  1. 查询计算机科学与技术学院同时选修了数据结构、操作系统两门课的学生详细信息

create table test8_02 as

select A.SID, name, dname, ds_score, os_score

from

(select pub.STUDENT.SID, max(score) ds_score

from pub.STUDENT, pub.STUDENT_COURSE, pub.COURSE

where pub.STUDENT.SID = pub.STUDENT_COURSE.SID

and pub.STUDENT_COURSE.CID = pub.COURSE.CID

and pub.COURSE.NAME = '数据结构'

group by pub.STUDENT.SID) A,

 

(select pub.STUDENT.SID, max(score) os_score

from pub.STUDENT, pub.STUDENT_COURSE, pub.COURSE

where pub.STUDENT.SID = pub.STUDENT_COURSE.SID

and pub.STUDENT_COURSE.CID = pub.COURSE.CID

and pub.COURSE.NAME = '操作系统'

group by pub.STUDENT.SID) B,

 

pub.STUDENT C

 

where A.SID = B.SID and B.SID = C.SID and dname = '计算机科学与技术学院'

  1. 查询计算机科学与技术学院选修了数据结构或操作系统的学生详细信息

create table test8_03 as

select sid, name, dname, ds_score, os_score

from (select * from

(select pub.STUDENT.SID, max(score) ds_score

from pub.STUDENT, pub.STUDENT_COURSE, pub.COURSE

where pub.STUDENT.SID = pub.STUDENT_COURSE.SID

and pub.STUDENT_COURSE.CID = pub.COURSE.CID

and pub.COURSE.NAME = '数据结构'

group by pub.STUDENT.SID)

 

natural full outer join

 

(select pub.STUDENT.SID, max(score) os_score

from pub.STUDENT, pub.STUDENT_COURSE, pub.COURSE

where pub.STUDENT.SID = pub.STUDENT_COURSE.SID

and pub.STUDENT_COURSE.CID = pub.COURSE.CID

and pub.COURSE.NAME = '操作系统'

group by pub.STUDENT.SID)

 

natural join pub.STUDENT

where dname = '计算机科学与技术学院'

 

  1. 查询计算机科学与技术学院所有学生的信息

 

create table test8_04 as

select sid, name, dname, ds_score, os_score

from (select * from

(select pub.STUDENT.SID, max(score) ds_score

from pub.STUDENT, pub.STUDENT_COURSE, pub.COURSE

where pub.STUDENT.SID = pub.STUDENT_COURSE.SID

and pub.STUDENT_COURSE.CID = pub.COURSE.CID

and pub.COURSE.NAME = '数据结构'

group by pub.STUDENT.SID)

 

natural full outer join

 

(select pub.STUDENT.SID, max(score) os_score

from pub.STUDENT, pub.STUDENT_COURSE, pub.COURSE

where pub.STUDENT.SID = pub.STUDENT_COURSE.SID

and pub.STUDENT_COURSE.CID = pub.COURSE.CID

and pub.COURSE.NAME = '操作系统'

group by pub.STUDENT.SID)

natural right outer join pub.STUDENT

where dname = '计算机科学与技术学院'

 

你可能感兴趣的:(学习资料)