当前位置:   article > 正文

2023年山东大学软件学院数据库实验1-9(全部)_山东大学数据库实验

山东大学数据库实验

目录

写在前面

实验代码

实验1

1-1

1-2

1-3

1-4

1-5

1-6

实验2

2-1

2-2

2-3

2-4

2-5

2-6

2-7

2-8

2-9

2-10

实验3

3-1

3-2

3-3

3-4

3-5

3-6

3-7

3-8

3-9

3-10

实验4

4-1

4-2

4-3

4-4

4-5

4-6

4-7

4-8

4-9

4-10

实验5

5-1

5-2

5-3

5-4

5-5

5-6

5-7

5-8

5-9

5-10

实验6

6-1

6-2

6-3

6-4

6-5

6-6

6-7

6-8

6-9

6-10

实验7

7-1

7-2

7-3

7-4

7-5

7-6

实验8

8-1

实验9

9-1

9-2

关于要注意的一些问题


写在前面

这个是2023年软件学院数据库实验,使用的数据库管理系统为oracle

因为作者之前是写mysql更多一点嘛...有的地方可能写法会很奇怪(比如能用自然连接但是还是用了手动连接,还有基本不用集合运算等).

相比之下,mysql的语法自由度更高一些,所以写实验花了比往常写同等难度的sql题目花费的时间更长一些.

实验代码全部通过,并且已经拿到满分,但是还望同志们仅仅是作为参考,迅速找出自己代码或者思维上的漏洞,不要抄袭捏.

如果未来24年数据库的同学注意一下,23年相较于22年之前有了一些变化,谨慎参考题目

实验代码

实验1

  1. 实验一 建表、删除表、插入数据
  2. 一、 实验内容
  3. 创建3个表,为每个表输入2行数据,没有逻辑难度,只是熟悉环境,学会创建表。
  4. 表名、列名采用英文,oracle不区分大小写,有not null的列代表不允许为空。
  5. 二、 实验步骤
  6. 1. 在SQL编辑区输入建表SQL,创建学生信息表(学生编号、姓名、性别、年龄、出生日期、院系名称、班级):
  7. test1_student:sid char 12 not null、name varchar 10 not null、sex char 2、age int、
  8. birthday date、dname varchar 30class varchar 10
  9. 2. 创建课程信息表(仅考虑一门课程最多一个先行课的情况)(课程编号、课程名称、先行课编号、学分)
  10. test1_course:cid char 6 not null、name varchar 40 not null、fcid char 6
  11. credit numeric 41(其中4代表总长度,1代表小数点后面长度)。
  12. 3. 创建学生选课信息表(学号、课程号、成绩、教师编号、选课时间)
  13. test1_student_course:sid char 12 not null、cid char 6 not null
  14. score numeric 51(其中5代表总长度,1代表小数点后面长度)、tid char 6、sctime date
  15. 4. 给表test1_student插入2行数据。
  16. 5. 给表test1_course插入2行数据。
  17. 6. 给表test1_student_course插入2行数据。

1-1

  1. create table test1_student(
  2. sid char(12) not null,
  3. name varchar(10) not null,
  4. sex char(2),
  5. age int,
  6. birthday date,
  7. dname varchar(30),
  8. class varchar(10)
  9. );

1-2

  1. create table test1_course(
  2. cid char(6) not null,
  3. name varchar(40) not null,
  4. fcid char(6),
  5. credit numeric(41)
  6. )

1-3

  1. create table test1_student_course(
  2. sid char(12) not null,
  3. cid char(6) not null,
  4. score numeric(5,1),
  5. tid char(6),
  6. sctime date
  7. );

1-4

  1. insert into test1_student values('201800020101','王欣','女',21,to_date('19940202','yyyymmdd'),'计算机学院','2010');
  2. insert into test1_student values('201800020102','李华','女',20,to_date('19950303','yyyymmdd'),'软件学院','2009');

1-5

  1. insert into test1_course values('800001','数据结构',null,2);
  2. insert into test1_course values('800002','数据库',800001,2.5)

1-6

  1. insert into test1_student_course values
  2. ('201800020101','300001',91.5,'200101',
  3. to_date(' 2009-07-15 09:09:09','yyyy-mm-dd hh24-mi-ss'));
  4. insert into test1_student_course values
  5. ('201800020101','300002',92.6,'200102',
  6. to_date(' 2009-07-15 10:10:10','yyyy-mm-dd hh24-mi-ss'));

实验2

  1. 实验二 检索查询
  2. 1. 找出没有选修任何课程的学生的学号、姓名(即没有选课记录的学生)。
  3. 自己认为查询语句正确后,通过下面语句将查询语句创建成视图test2_01
  4. Create or replace view test2_01 as select ……
  5. 然后就可以点击实验二的题目1的【交卷验证】,验证正确性,正确后就有得分。
  6. 如果结果不对,会提示错误几行或者多几行或者少几行,这时你需要思考解题思路有没有遗漏的情况,所有情况是否都有相应条件进行约束,或者显示所得数据,一条一条查看是否符合条件。
  7. 特别提醒:
  8. 1、逻辑错误尽量不要去问老师错在哪里了,老师虽然知道答案但那不是唯一答案,在不了解你的解题思路情况下,根本不知道你的错误是思路错误,还是实现的语句有错误。
  9. 2、如果出现错误一两行情况,可以私信QQ群李老师,提醒你可能你忽略的特殊情况。
  10. 2. 找出至少选修了学号为“200900130417”的学生所选修的一门课的学生的学号、姓名(不包含这名同学)。
  11. 自己认为查询语句正确后,通过下面语句将查询语句创建成视图test2_02
  12. Create or replace view test2_02 as select ……
  13. 然后就可以点击实验二的题目2的【交卷验证】,验证正确性,正确后就有得分。
  14. 以下各题操作类似。
  15. 3. 找出至少选修了一门其先行课程号为“300002”号课程的学生的学号、姓名。
  16. 4. 找出选修了“操作系统”并且也选修了“数据结构”,但是没有选修“程序设计语言”的学生的学号、姓名。
  17. 5. 找出姓名叫“李龙”的学生的学号及其选修全部课程的课程号、课程名和成绩。
  18. 6. 查询2010级、计算机科学与技术学院、操作系统的学生成绩表,内容有学号、姓名、成绩。
  19. 7. 查询所有不姓张、不姓李、也不姓王的学生的学号sid、姓名name
  20. 8. 找出有间接先行课的所有课程的课程号、课程名称。
  21. 9. 查询选修了300003号课程的学生的sid、name、score
  22. 10. 找出选修了所有课程的学生的学号、姓名。

2-1

  1. create or replace view test2_01
  2. as
  3. select
  4. a.sid , a.name
  5. from
  6. pub.student a
  7. where
  8. a.sid not in (select sid from pub.student_course)

2-2

  1. create or replace view test2_02
  2. as
  3. select
  4. a.sid,a.name
  5. from
  6. pub.student a
  7. join
  8. (select distinct sid from pub.student_course
  9. where cid in (select cid from course where fcid='300002')
  10. ) b
  11. on
  12. a.sid=b.sid

2-3

  1. create or replace view test2_03
  2. as
  3. select
  4. a.sid,a.name
  5. from
  6. pub.student a
  7. join
  8. (select sid,z.name
  9. from
  10. (select sid,pub.course.name from pub.sutdent_course a join course b on a.cid=b.cid) x
  11. join
  12. (select sid,pub.course.name from pub.sutdent_course a join course b on a.cid=b.cid) y
  13. on
  14. x.sid=y.sid and x.name='操作系统' and y.name='数据库'
  15. left join
  16. (select sid,pub.course.name from pub.sutdent_course a join course b on a.cid=b.cid) z
  17. on
  18. x.sid=z.sid and z.name='程序设计语言'
  19. ) b
  20. on
  21. a.sid=b.sid
  22. where
  23. z.name is null

2-4

  1. create or replace view test2_04
  2. as
  3. select
  4. a.sid,b.cid,c.name,b.score
  5. from
  6. pub.student a
  7. join
  8. pub.student_course b
  9. on a.sid=b.sid
  10. join
  11. pub.course c
  12. on b.cid=c.cid
  13. where
  14. a.name='李龙'

2-5

  1. create or replace view test2_05
  2. as
  3. select
  4. a.sid,b.cid,c.name,b.score
  5. from
  6. pub.student a
  7. join
  8. pub.student_course b
  9. on a.sid=b.sid
  10. join
  11. pub.course c
  12. on b.cid=c.cid
  13. where
  14. a.name='李龙'

2-6

  1. create or replace view test2_06
  2. as
  3. select
  4. sid,name
  5. from
  6. pub.student a
  7. where
  8. name not like '张%'
  9. and
  10. name not like '李%'
  11. and
  12. name not like '王%'

2-7

  1. create or replace view test2_07
  2. as
  3. select
  4. sid,name
  5. from
  6. pub.student a
  7. where
  8. name not like '张%'
  9. and
  10. name not like '李%'
  11. and
  12. name not like '王%'

2-8

  1. create or replace view test2_08
  2. as
  3. select
  4. a.sid,a.name,c.score
  5. from
  6. pub.student a
  7. join
  8. pub.studen_course b
  9. on
  10. a.sid=b.sid
  11. join
  12. pub.course c
  13. on
  14. b.cid=c.cid
  15. where
  16. b.cid='300003'

2-9

  1. create or replace view test2_09
  2. as
  3. select
  4. a.sid,name
  5. from
  6. pub.student a
  7. join
  8. pub.student_course b
  9. on
  10. a.sid=b.sid
  11. group by
  12. a.sid,name
  13. having
  14. sum(b.cid) = (select sum(cid) from pub.course)

2-10

  1. Create or replace view test2_10
  2. as
  3. select
  4. a.sid,name
  5. from
  6. pub.student a
  7. join
  8. pub.student_course b
  9. on
  10. a.sid=b.sid
  11. group by
  12. a.sid , name
  13. having
  14. count(distinct b.cid) = (select count(b.cid) from pub.course)

实验3

  1. 二、 实验题目
  2. 1. 将pub用户下的Student_31及数据复制到主用户的表test3_01(注意:test3_xx要建成表不是视图,否则删除数据时会显示无此权限),删除表中的学号不全是数字的那些错误数据,学号应该是数字组成,不能够包含字母空格等非数字字符。
  3. 方法之一:用substr函数,例如Substr(sid,1,1)返回学号的第一位,判断是否是数字。
  4. 2. 将pub用户下的Student_31及数据复制到主用户的表test3_02,删除表中的出生日期和年龄(截止到2012年的年龄,即年龄=2012-出生年份)不一致的那些错误数据。
  5. 函数extract(year from birthday)返回birthday的年份
  6. 3. 将pub用户下的Student_31及数据复制到主用户的表test3_03,删除表中的性别有错误的那些错误数据(性别只能够是“男”、“女”或者空值)。
  7. 4. 将pub用户下的Student_31及数据复制到主用户的表test3_04,删除表中的院系名称有空格的、院系名称为空值的或者院系名称小于3个字的那些错误数据。
  8. 5. 将pub用户下的Student_31及数据复制到主用户的表test3_05,删除表中的班级不规范的那些错误数据,不规范是指和大多数不一致。
  9. 这个题知识点是学会用SQL找出不规范的数据,而不是用人工办法找不规范。
  10. 提示:寻找不规范有很多解决思路,可以去对比大纲最后的提示。
  11. 6. 将pub用户下的Student_31及数据复制到主用户的表test3_06,删除表中的错误数据,不规范的数据也被认为是那些错误数据。
  12.  学号不全是数字;
  13.  出生日期和年龄不一致的(年龄=2012-出生年份);
  14.  姓名有空格的或者长度小于2个字的;函数length()返回字符串长度。
  15.  性别有错误的(只能够是“男”、“女”、空值);
  16.  院系名称有空格的、院系名称为空值的;
  17.  院系名称小于3个字的;
  18.  班级数据有错误的(需要先找到班级里面的错误)。
  19. 保留最后全部正确的数据。
  20. 7. 将pub用户下的Student_course_32及数据复制到主用户的表test3_07,删除其中的错误数据,错误指如下情况:
  21. 学号在学生信息pub.student中不存在的;
  22. 8. 将pub用户下的Student_course_32及数据复制到主用户的表test3_08,删除其中的错误数据,错误指如下情况:
  23. 课程号和教师编号在教师授课表pub.teacher_course中不同时存在的,即没有该教师教该课程;
  24. 9. 将pub用户下的Student_course_32及数据复制到主用户的表test3_09,删除其中的错误数据,错误指如下情况:
  25. 成绩数据有错误(需要先找到成绩里面的错误)。
  26. 这个题知识点是学会用SQL找出错误数据,而不是用人工办法找错误数据。
  27. 提示:寻找不规范有很多解决思路,可以去对比大纲最后的提示。
  28. 10. 将pub用户下的Student_course_32及数据复制到主用户的表test3_10,删除其中的错误数据,错误指如下情况:
  29. 1) 学号在学生信息pub.student中不存在的;
  30. 2) 课程号在课程信息pub.course中不存在的;
  31. 3) 教师编号在教师信息pub.teacher中不存在的;
  32. 4) 课程号和教师编号在教师授课表pub.teacher_course中不存在的;
  33. 5) 成绩数据有错误(需要先找到成绩里面的错误)。
  34. 保留最后正确的数据。

3-1

  1. create table test3_01 as
  2. (select * from pub.Student_31);
  3. delete from test3_01
  4. where not regexp_like(sid,'^[0-9]+$');

3-2

  1. create table test3_02 as
  2. (select * from pub.Student_31);
  3. delete from test3_02
  4. where (2012-extract(year from birthday))!=age;

3-3

  1. create table test3_03 as
  2. (select * from pub.Student_31);
  3. delete from test3_03
  4. where not(sex='男' or sex='女' or sex is null);

3-4

  1. create table test3_04 as
  2. (select * from pub.Student_31);
  3. delete from test3_04
  4. where dname like '% %' or dname not like '%___%' or dname is null;

3-5

  1. create table test3_05 as
  2. (select * from pub.Student_31);
  3. delete from test3_05
  4. where not regexp_like(class,'^[0-9]{4}$');

3-6

  1. create table test3_06 as
  2. (select * from pub.Student_31);
  3. delete from test3_06
  4. where not regexp_like(sid,'^[0-9]+$')
  5. or
  6. name like '% %' or name not like '%__%'
  7. or
  8. not(sex='男' or sex='女' or sex is null)
  9. or
  10. dname like '% %' or dname not like '%___%' or dname is null
  11. or
  12. not regexp_like(class,'^[0-9]{4}$')
  13. or
  14. (2012-extract(year from birthday))!=age

3-7

  1. create table test3_07 as
  2. (select * from pub.student_course_32);
  3. delete from test3_07
  4. where sid not in (select sid from pub.student);

3-8

  1. create table test3_08 as
  2. (select * from pub.student_course_32);
  3. delete from test3_08
  4. where
  5. (cid,tid) not in (select cid,tid from pub.teacher_course)

3-9

  1. create table test3_09 as
  2. (select * from pub.student_course_32);
  3. delete from test3_09
  4. where
  5. sid not in (select sid from pub.student) or
  6. cid not in (select cid from pub.course) or
  7. tid not in (select tid from pub.teacher) or
  8. (cid,tid) not in (select cid,tid from pub.teacher_course) or
  9. score <0 or score >100;
  10. ;

3-10

  1. create table test3_10 as
  2. (select * from pub.student_course_32);
  3. delete from test3_10
  4. where
  5. sid not in (select sid from pub.student) or
  6. cid not in (select cid from pub.course) or
  7. tid not in (select tid from pub.teacher) or
  8. cid not in (select cid from pub.teacher_course) or
  9. tid not in (select tid from pub.teacher_course) or
  10. (cid,tid) not in (select cid,tid from pub.teacher_course) or
  11. score <0 or score >100

实验4

  1. 实验四 复制表、修改表结构、修改数据
  2. 三、 实验内容
  3. 利用oracle管理平台完成对表的结构、数据进行修改,每一个问题可以通过多个SQL语句完成。
  4. 四、 实验题目
  5. 1. 将pub用户下表student_41及数据复制到主用户的表test4_01中,使用alter table语句为表增加列:“总成绩:sum_score”。
  6. 使用update语句,利用pub.student_course,统计 “总成绩”;
  7. 2. 将pub用户下表student_41及数据复制到主用户的表test4_02中,使用alter table语句为表增加列“平均成绩:avg_score” (小数点后保留1位)。
  8. 利用pub.student_course,统计“平均成绩”,四舍五入到小数点后1
  9. 3. 将pub用户下表student_41及数据复制到主用户的表test4_03中,使用alter table语句为表增加列:“总学分:sum_credit”。
  10. 使用update语句,利用pub.student_course、pub.course,统计 “总学分”;
  11. 这是需要注意:成绩及格才能够计算所得学分,一门课多个成绩都及格只计一次学分。
  12. 4. 将pub用户下表student_41及数据复制到主用户的表test4_04中。
  13. 根据列院系名称dname到pub.department找到对应院系编号did,将对应的院系编号回填到院系名称列dname中,如果表中没有对应的院系名称,则列dname中内容不变仍然是原来的内容。
  14. 5. 将pub用户下表student_41及数据复制到主用户的表test4_05中,使用alter table语句为表增加4个列:“总成绩:sum_score”、 “平均成绩:avg_score”、“总学分:sum_credit”、“院系编号:did varchar(2) ”。
  15. 1) 利用pub.student_course、pub.course,统计 “总成绩”;
  16. 2) 利用pub.student_course、pub.course,统计“平均成绩”,四舍五入到小数点后1位;
  17. 3) 利用pub.student_course、pub.course,统计 “总学分”;
  18. 4) 根据院系名称到pub.department和pub.department_41中,找到对应编号,填写到院系编号中,如果都没有对应的院系,则填写为00
  19. 说明:执行update后,在查询表中数据,可能出现顺序变化,这是正常,因为数据在表中是无序。需要顺序的时候可以通过orderby实现。
  20. 6. 将pub用户下的Student_42及数据复制到主用户的表test4_06中,对表中的数据进行整理,修复那些不规范的数据:
  21. 剔除姓名列中的所有空格;
  22. 7. 将pub用户下的Student_42及数据复制到主用户的表test4_07中,对表中的数据进行整理,修复那些不规范的数据:
  23. 对性别列进行规范(需要先确定哪些性别数据不规范,也就是那些和大多数不一样的就是不规范的);
  24. 8. 将pub用户下的Student_42及数据复制到主用户的表test4_08中,对表中的数据进行整理,修复那些不规范的数据:
  25. 对班级列进行规范(需要先确定哪些班级不规范)。
  26. 9. 将pub用户下的Student_42及数据复制到主用户的表test4_09中,对表中的数据进行整理,修复那些不规范的数据:
  27. 年龄为空值的根据出生日期设置学生年龄(截止到2012年的年龄,即年龄=2012-出生年份),年龄不为空值的不要改变。
  28. 10. 将pub用户下的Student_42及数据复制到主用户的表test4_10中,对表中的数据进行整理,修复那些不规范的数据:
  29. 1) 剔除姓名列中的所有空格;
  30. 2) 剔除院系名称列中的所有空格;
  31. 3) 对性别列进行规范(需要先确定哪些性别数据不规范,也就是那些和大多数不一样的就是不规范的);
  32. 4) 对班级列进行规范(需要先确定哪些班级不规范)。
  33. 5) 年龄为空值的根据出生日期设置学生年龄(截止到2012年的年龄,即年龄=2012-出生年份),年龄不为空值的不要改变。

4-1

  1. create table test4_01 as
  2. (select * from pub.Student_41);
  3. alter table test4_01 add sum_score int;
  4. update test4_01 a
  5. set sum_score=(
  6. select sum(score)
  7. from pub.student_course P
  8. where a.sid=P.sid
  9. )

4-2

  1. create table test4_02 as
  2. (select * from pub.Student_41);
  3. alter table test4_02 add avg_score int;
  4. alter table test4_02 drop column avg_score;
  5. alter table test4_02 add avg_score numeric(3,1);
  6. update test4_02 a
  7. set avg_score=round((
  8. select avg(score)
  9. from pub.student_course P
  10. where a.sid=P.sid
  11. ),1)

4-3

  1. create table test4_03 as
  2. (select * from pub.Student_41);
  3. alter table test4_03 add sum_credit int;
  4. update test4_03 t
  5. set sum_credit=(
  6. select sum(temp.credit)
  7. from
  8. (select a.sid,b.cid,max(score) score,c.credit
  9. from test4_03 a join pub.Student_course b on a.sid=b.sid
  10. join pub.course c on b.cid=c.cid
  11. group by a.sid,b.cid,c.credit
  12. ) temp
  13. where
  14. t.sid=temp.sid and score>=60
  15. )

4-4

  1. drop table if exists test4_04;
  2. create table test4_04 as
  3. (select * from pub.Student_41);
  4. update test4_04 t
  5. set dname= (
  6. select did
  7. from pub.department a
  8. where t.dname=a.dname
  9. )
  10. where t.dname in (select dname from pub.department)

4-5

  1. create table test4_05 as
  2. (select * from pub.Student_41);
  3. alter table test4_05 add sum_score int;
  4. update test4_05 a
  5. set sum_score=(
  6. select sum(score)
  7. from pub.student_course P
  8. where a.sid=P.sid
  9. )
  10. alter table test4_05 add avg_score numeric(3,1);
  11. update test4_05 a
  12. set avg_score=round((
  13. select avg(score)
  14. from pub.student_course P
  15. where a.sid=P.sid
  16. ),1)
  17. alter table test4_05 add did varchar(2);
  18. update test4_05 set did ='00';
  19. update test4_05 t
  20. set did= (
  21. select did
  22. from pub.department a
  23. where t.dname=a.dname
  24. )
  25. where t.dname in (select dname from pub.department)
  26. ;
  27. update test4_05 t
  28. set did= (
  29. select did
  30. from pub.department_41 a
  31. where t.dname=a.dname
  32. )
  33. where t.dname in (select dname from pub.department_41)
  34. ;
  35. alter table test4_05 add sum_credit int;
  36. update test4_05 t
  37. set sum_credit=(
  38. select sum(temp.credit)
  39. from
  40. (select a.sid,b.cid,max(score) score,c.credit
  41. from test4_05 a join pub.Student_course b on a.sid=b.sid
  42. join pub.course c on b.cid=c.cid
  43. group by a.sid,b.cid,c.credit
  44. ) temp
  45. where
  46. t.sid=temp.sid and score>=60
  47. )

4-6

  1. create table test4_06 as
  2. (select * from pub.Student_42);
  3. update test4_06
  4. set name=replace(name,' ','' );

4-7

  1. create table test4_07 as
  2. (select * from pub.Student_42);
  3. update test4_07
  4. set sex=replace(sex,'性','');
  5. update test4_07
  6. set sex=replace(sex,' ','');

4-8

  1. create table test4_08 as
  2. (select * from pub.Student_42);
  3. update test4_08
  4. set class=replace(class,'级','');

4-9

  1. create table test4_09 as
  2. (select * from pub.Student_42);
  3. update test4_09
  4. set age=(2012-extract(year from birthday))
  5. where age is null

4-10

  1. create table test4_10 as
  2. (select * from pub.Student_42);
  3. update test4_10
  4. set name=replace(name,' ','');
  5. update test4_10
  6. set dname=replace(dname,' ','');
  7. update test4_10
  8. set sex=replace(sex,'性','');
  9. update test4_10
  10. set sex=replace(sex,' ','');
  11. update test4_10
  12. set class=replace(class,'级','');
  13. update test4_10
  14. set age=(2012-extract(year from birthday))
  15. where age is null

实验5

  1. 二、实验题目
  2. 1. 在学生表pub.student中统计名字(姓名的第一位是姓氏,其余为名字,不考虑复姓)的使用的频率,将统计结果放入表test5_01中
  3. 2. 在学生表pub.student中统计名字(姓名的第一位是姓氏,不作统计,名字指姓名的第二个之后的汉字)的每个字使用的频率,将统计结果放入表test5_02中,表结构如下。
  4. 3. 先创建“学院班级学分达标情况统计表1”test5_03,依据pub.student, pub.course,pub.student_course统计形成表中各项数据,成绩>=60为及格计入学分,总学分>=10算作达标,院系为空值的数据不统计在下表中,表结构:院系名称dname、班级class、学分达标人数p_count1、学分未达标人数p_count2、总人数p_count。
  5. 排错提示:
  6. 平台提示有多行数据错误,特别是行数比较多,可以从结果的第一行开始,一个列一个列进行单独统计,找到错误的是那个列数,然后对照你形成这个列的部分寻找原因。假设输出结果如上图,可以这样统计:
  7. select count(*) from pub.STUDENT where dname='计算机学院' and class=2006,检查统计计算机学院2006级是不是9
  8. Select count(*) …………检查统计计算机学院2006级达标人数是不是4
  9. Select count(*) …………检查统计计算机学院2006级不达标人数是不是5
  10. 通过这样办法找到错在那个列,然后再去对应的SQL部分找错误就容易了。
  11. 4. 创建“学院班级学分达标情况统计表2”test5_04,依据pub.student, pub.course,pub.student_course统计形成表中各项数据,成绩>=60为及格计入学分,2008级及之前的班级总学分>=8算作达标,2008级之后的班级学分>=10算作达标,院系为空值的数据不统计在下表中,表结构:院系名称dname、班级class、学分达标人数p_count1、学分未达标人数p_count2、总人数p_count。
  12. 5. 查询各院系(不包括院系名称为空的)的数据结构平均成绩avg_ds_score、操作系统平均成绩avg_os_score,平均成绩四舍五入到个位,创建表test5_05
  13. 6. 查询”计算机科学与技术学院”的同时选修了数据结构、操作系统两门课的学生的学号sid、姓名name、院系名称dname、数据结构成绩ds_score、操作系统成绩os_score,创建表test5_06
  14. 7. 查询计算机科学与技术学院的选修了数据结构或者操作系统的学生的学号sid、姓名name、院系名称dname、数据结构成绩ds_score、操作系统成绩os_score,创建表test5_07
  15. 8. 查询计算机科学与技术学院所有学生的学号sid、姓名name、院系名称dname、数据结构成绩ds_score、操作系统成绩os_score,创建表test5_08,表结构及格式如下
  16. 9. 创建“成绩及格学生每个成绩人数百分比统计表” 视图test5_09,依据pub.student_course统计形成视图中各项数据,成绩>=60为及格,视图结构:成绩score、对应成绩人数count1、及格总人数count0、站总人数的百分比percentage(即前面两列相除后乘以100)。其中百分比四舍五入到小数点后面2位。输出按成绩排序。上面说的“人数”理解成 “人次”更加准确,亦既一个同学同一门考试考两次,算两人次。
  17. 视图内容示例如下:
  18. 10. 创建“每个课程成绩在60-99分之间每段成绩的人数百分比统计表”视图test5_10,依据pub.course,pub.student_course统计形成视图中各项数据,仅仅统计成绩在60-99(包含6099)之间成绩,视图结构:课程编号cid、课程名称cname、成绩score(这个列内容不要包含空格)、对应成绩人数count1、及格总人数count0(仅统计60-99分)、站总人数的百分比percentage(即前面两列相除后乘以100)。其中百分比四舍五入到小数点后面2位。输出按课程编号、成绩排序。上面说的“人数”理解成 “人次”更加准确,亦既一个同学同一门考试考两次,算两人次。

5-1

  1. create table test5_01
  2. (First_name varchar(4),frequency numeric(4));
  3. insert into test5_01
  4. (
  5. select substr(name,2),count(*)
  6. from pub.student
  7. group by substr(name,2)
  8. )

5-2

  1. create table test5_02
  2. (letter varchar(2),frequency numeric(4));
  3. insert into test5_02
  4. (select
  5. last,count(*)
  6. from
  7. (
  8. (select substr(name,2,1) as last
  9. from pub.student
  10. where length(name)>=2)
  11. union all
  12. (select substr(name,3,1) as last
  13. from pub.student
  14. where length(name)>=3)
  15. )
  16. group by last
  17. );

5-3

  1. insert into test5_03
  2. (select Dname,class,p1,psum-p1,psum
  3. from
  4. (select
  5. Dname,class,sum(CASE WHEN sum_credit >= 10 THEN 1 ELSE 0 END) as p1
  6. from
  7. (select sid,sum(credit) sum_credit
  8. from
  9. (select sid,cid,max(score) score from pub.student_course where score>=60 group by sid,cid) a
  10. natural left join pub.course b
  11. group by sid
  12. ) temp1
  13. natural join pub.student temp2
  14. where Dname is not null
  15. group by Dname,class
  16. ) t1
  17. natural join
  18. (select dname,class,count(sid) as psum
  19. from pub.student
  20. where dname is not null
  21. group by dname,class
  22. ) t2
  23. );

5-4

  1. create table test5_04
  2. (Dname varchar(30),class varchar(10),P_count1 Int,P_count2 int,P_count int);
  3. insert into test5_04
  4. (select Dname,class,p1,psum-p1,psum
  5. from
  6. (select
  7. Dname,class,sum(case when class<=2008
  8. then
  9. (CASE WHEN sum_credit >= 8 THEN 1 ELSE 0 END)
  10. else
  11. (CASE WHEN sum_credit >= 10 THEN 1 ELSE 0 END)
  12. end ) as p1
  13. from
  14. (select sid,sum(credit) sum_credit
  15. from
  16. (select sid,cid,max(score) score from pub.student_course where score>=60 group by sid,cid) a
  17. natural left join pub.course b
  18. group by sid
  19. ) temp1
  20. natural join pub.student temp2
  21. where Dname is not null
  22. group by Dname,class
  23. ) t1
  24. natural join
  25. (select dname,class,count(sid) as psum
  26. from pub.student
  27. where dname is not null
  28. group by dname,class
  29. ) t2
  30. );

5-5

  1. create or replace view test5_05 as
  2. select
  3. dname,a as Avg_ds_score,b as Avg_os_score
  4. from
  5. (select dname ,round(avg(max_ds)) a
  6. from
  7. (select sid,dname,max(score) max_ds
  8. from pub.student a natural join pub.student_course b join pub.course c on b.cid=c.cid
  9. where dname is not null and c.name='数据结构'
  10. group by sid,dname ) temp
  11. group by dname)
  12. natural join
  13. (select dname ,round(avg(max_ds)) b
  14. from
  15. (select sid,dname,max(score) max_ds
  16. from pub.student a natural join pub.student_course b join pub.course c on b.cid=c.cid
  17. where dname is not null and c.name='操作系统'
  18. group by sid,dname ) temp
  19. group by dname)
  20. ;

5-6

  1. create or replace view test5_06 as
  2. (select sid,n.name,n.dname,max(a.score) ds_score,max(b.score) os_score
  3. from pub.student_course a
  4. join pub.student_course b on a.sid=b.sid
  5. join pub.course c1 on a.cid=c1.cid
  6. join pub.course c2 on b.cid=c2.cid
  7. join pub.student n on n.sid=a.sid
  8. where c1.name='数据结构' and c2.name='操作系统' and n.dname='计算机科学与技术学院'
  9. group by sid,n.name,n.dname
  10. );

5-7

  1. create or replace view test5_07 as
  2. select
  3. sid,name,dname,ds ds_score,os os_score
  4. from
  5. (select
  6. n.sid,name,dname,max(score) ds
  7. from
  8. pub.student n
  9. join
  10. (select sid,score from pub.student_course
  11. where cid=(select cid from pub.course where name='数据结构')) a
  12. on
  13. n.sid=a.sid
  14. where n.dname='计算机科学与技术学院'
  15. group by n.sid,name,dname)
  16. natural full join
  17. (select
  18. n.sid,name,dname,max(score) os
  19. from
  20. pub.student n
  21. join
  22. (select sid,score from pub.student_course
  23. where cid=(select cid from pub.course where name='操作系统')) a
  24. on
  25. n.sid=a.sid
  26. where n.dname='计算机科学与技术学院'
  27. group by n.sid,name,dname);

5-8

  1. create or replace view test5_08 as
  2. select n.sid,n.name,n.dname,max(a.score) ds_score,max(b.score) os_score
  3. from
  4. pub.student n
  5. left join
  6. (select * from pub.student_course where cid=(select cid from pub.course where name='数据结构')) a
  7. on n.sid=a.sid
  8. left join
  9. (select * from pub.student_course where cid=(select cid from pub.course where name='操作系统')) b
  10. on n.sid=b.sid
  11. where n.dname='计算机科学与技术学院'
  12. group by n.sid,n.name,n.dname;

5-9

  1. create or replace view test5_09 as
  2. with temp as
  3. (select score ,sid from pub.student_course where score>=60)
  4. select
  5. score,
  6. count(sid) count1,
  7. (select count(sid) from temp) count0,
  8. round(count(sid)/(select count(sid) from temp),4)*100 percentage
  9. from temp
  10. group by score
  11. ;

5-10

  1. create or replace view test5_10 as
  2. select
  3. cid,
  4. (select name from pub.course where cid=a.cid) cname,
  5. to_char(trunc(score,-1),'fm00')||'-'||to_char(trunc(score,-1)+9,'fm00') Score,
  6. count(*) count1,
  7. (select count(*) from pub.student_course t where a.cid=t.cid and score>=60 and score<100) count0,
  8. round(count(*)/(select count(*) from pub.student_course where cid=a.cid and score>=60 and score<100)*100,2) percentage
  9. from
  10. pub.student_course a
  11. where
  12. score>=60 and score<=99
  13. group by
  14. cid, to_char(trunc(score,-1),'fm00')||'-'||to_char(trunc(score,-1)+9,'fm00')
  15. ;

实验6

  1. 创建视图、删除视图
  2. 一、 实验内容
  3. oracle管理平台,针对公共用户pub下的表,完成创建视图、查询验证视图、删除视图。视图名为test6_(题号,题号长度两位,前面补零),例如test6_01。
  4. 二、 实验题目
  5. 1. 找出年龄小于20岁且是“物理学院”的学生的学号、姓名、院系名称,按学号排序。
  6. 2. 查询统计2009级、软件学院所有学生的学号、姓名、总成绩(列名sum_score)(如果有学生没有选一门课,则总成绩为空值)。
  7. 3. 查询所有课的最高成绩、最高成绩人数,test6_06有四个列:课程号cid、课程名称name、最高成绩max_score、最高成绩人数max_score_count(一个学生同一门课成绩都是第一,只计一次,需要考虑刷成绩情况,一个同学选了一个课程多次,两次都是最高分。如果结果错一行,可能就是没有考虑这种情况,这里是为了考核“去重复计数”知识点的)。如果没有学生选课,则最高成绩为空值,最高成绩人数为零。
  8. 提示:参考讲义关于标量子查询(只返回包含单个属性的单个元组)。
  9. 4. 找出选修了“操作系统”并取得学分或者选修“数据结构”并且取得学分,但是没有选修“程序设计语言”或者没有取得这门课的学分的男学生的学号、姓名。
  10. 5. 查询20岁的所有有选课的学生的学号、姓名、平均成绩(avg_score,此为列名,下同)(平均成绩四舍五入到个位)、总成绩(sum_score)
  11. Test6_05有四个列,并且列名必须是:sid、name、avg_score、sum_score。通过下面方式实现列名定义:
  12. create or replace view test6_05 as select sid,name,(表达式) avg_score,(表达式) sum_score from ……
  13. 6. 找出同一个同学同一门课程有两次或以上不及格的所有学生的学号、姓名(即一门课程需要补考两次或以上的学生的学号、姓名)。
  14. 7. 找出选修了所有课程并且每门课程每次考试成绩均及格的学生的学号、姓名。(题6的延伸和巩固)
  15. 8. 找出选修了所有课程并且得到所有课程的学分(即每门课程最少一次考试及格)的学生的学号、姓名。(题6的 延伸和巩固)。
  16. 9. 查询统计2010级、化学与化工学院的学生总学分表,内容有学号、姓名、总学分sum_credit。(不统计没有选课的学生)。
  17. 10. 查询学生表中每一个姓氏及其人数(不考虑复姓,用到取子串函数substr(string,postion,length))),test6_10有两个列:second_name、p_count

6-1

  1. create or replace view test6_01 as select sid,name,dname
  2. from pub.student
  3. where age<20 and dname='物理学院'
  4. order by sid;

6-2

  1. create or replace view test6_02 as
  2. select
  3. b.sid,b.name,sum(Sco) sum_score
  4. from
  5. (
  6. select b.sid,b.cid,max(score) Sco
  7. from pub.student_course b
  8. join pub.course c on b.cid=c.cid
  9. group by b.sid,b.cid
  10. ) a
  11. right join pub.student b on a.sid=b.sid
  12. where
  13. class=2009 and dname='软件学院'
  14. group by
  15. b.name,b.sid
  16. ;

6-3

  1. create or replace view test6_03 as
  2. select
  3. a.cid ,
  4. (select name from pub.course tt where tt.cid=a.cid) name,
  5. max_score,
  6. (case when max_score_count is null then 0 else max_score_count end) max_score_count
  7. from
  8. pub.course a
  9. left join
  10. (
  11. select
  12. cid,score max_score,count(distinct sid) max_score_count
  13. from
  14. pub.student_course a
  15. where
  16. a.score = (select max(score) from pub.student_course temp where temp.cid=a.cid)
  17. group by cid,score
  18. ) b
  19. on a.cid=b.cid;

6-4

  1. create or replace view test6_04 as
  2. select sid , name
  3. from
  4. pub.student
  5. natural join
  6. (select distinct sid
  7. from pub.student_course
  8. where score>=60 and (cid='300002' or cid='300005')
  9. )
  10. where
  11. sex='男'
  12. and
  13. sid not in
  14. (select distinct sid
  15. from pub.student_course
  16. where cid='300001' and score>=60 )

6-5

  1. create or replace view test6_05 as
  2. select
  3. u.sid,
  4. name,
  5. round(avg(score),0) avg_score,
  6. sum(score) sum_score
  7. from
  8. pub.student_course u
  9. join
  10. pub.student a
  11. on u.sid=a.sid
  12. where age=20
  13. group by u.sid,name
  14. ;

6-6

  1. create or replace view test6_06 as
  2. select
  3. sid,name
  4. from
  5. pub.student
  6. natural join
  7. pub.student_course
  8. where score<60
  9. group by
  10. sid,cid,name
  11. having
  12. count(*)>=2
  13. ;

6-7

  1. create or replace view test6_07 as
  2. select
  3. u.sid, name
  4. from
  5. (select
  6. sid,cid,min(score) sc
  7. from
  8. pub.student_course
  9. group by
  10. sid,cid) u
  11. join
  12. pub.student a
  13. on u.sid=a.sid
  14. where sc>=60
  15. group by
  16. u.sid,name
  17. having
  18. count(*)=140
  19. ;

6-8

  1. create or replace view test6_08 as
  2. select
  3. u.sid, name
  4. from
  5. (select
  6. sid,cid,max(score) sc
  7. from
  8. pub.student_course
  9. group by
  10. sid,cid) u
  11. join
  12. pub.student a
  13. on u.sid=a.sid
  14. where sc>=60
  15. group by
  16. u.sid,name
  17. having
  18. count(*)=140
  19. ;

6-9

  1. create or replace view test6_09 as
  2. select
  3. a.sid,a.name,sum(credit)
  4. from
  5. pub.student a
  6. join pub.student_course b on a.sid=b.sid
  7. join pub.course c on b.cid=c.cid
  8. where
  9. score >= 60 and class=2010 and dname='化学与化工学院'
  10. group by a.sid,a.name;

6-10

  1. create or replace view test6_10 as
  2. select
  3. substr(name,1,1) second_name , sum(*) p_count
  4. from
  5. pub.student
  6. group by
  7. substr(name,1,1);
  8. create or replace view test6_10 as
  9. select
  10. substr(name,1,1) second_name ,
  11. count(*) p_count
  12. from
  13. pub.student
  14. group by
  15. substr(name,1,1);

实验7

  1. 二、 题目1
  2. 1. 将pub用户下表student的3个列sid,name,birthday复制到表test7_01中。
  3. 2. 执行如下查询,观察运行速度(5秒以上)。
  4. 查询Samefirstname相同姓氏的人数。
  5. select * from
  6. (select sid,name,birthday,
  7. (select count(*) from test7_01 where substr(name,1,1)=substr(t1.name,1,1)) samefirstname
  8. from pub.student_testindex t1)
  9. where samefirstname=7
  10. 3. 为test7_01创建一个仅仅一个索引test7_01_index,保证上面SQL耗时在1秒内。
  11. 三、 题目2
  12. 1. 将pub用户下表student的3个列sid,name,birthday复制到表test7_02中。
  13. 2. 将出生日期部分修改成一天:
  14. Update test7_02 set birthday=to_date('19881018','yyyymmdd') where substr(sid,12,1)= '0';
  15. 3. 为test7_02创建一个仅仅一个索引test7_02_index,保证下面SQL耗时在1秒内。
  16. Samenamebirthday同名同生日的人数,Samebirthday相同出生日期的人数
  17. select * from
  18. (select sid,name,birthday,
  19. (select count(*) from test7_02 where name=t1.name and birthday=t1.birthday) samenamebirthday,
  20. (select count(*) from test7_02 where birthday=t1.birthday) samebirthday
  21. from pub.student_testindex t1)
  22. where samebirthday=403
  23. 4. 验证问题:用计划验证下面这个查询能使用test7_02_index的索引吗?
  24. select * from
  25. (select sid,name,birthday,
  26. (select count(*) from test7_02 where name=t1.name) samename
  27. from pub.student_testindex t1)
  28. where samename=7
  29. 四、 题目3
  30. 1. pub用户下表student已经用下面两句SQL创建了两索引。
  31. Create index student_birthday on student(birthday);
  32. Create index student_name on student(name);
  33. 2. 下面SQL在访问pub.student时不能用索引student_name因此耗时超过2秒,在逻辑不变情况下,修改SQL中标为记红色的子查询的where条件部分,不要修改其它地方,使其能使用索引。
  34. 说明:因为pub.student_testindex数据行数太少,不能通过修改主句where绕过问题。
  35. 查询samefirstname同姓氏的人数。
  36. select * from
  37. (select sid,name,birthday,
  38. (select count(*) from pub.student
  39. where substr(name,1,1)=substr(t1.name,1,1)
  40. ) samefirstname
  41. from pub.student_testindex t1) where samefirstname=7
  42. 3. 修改以后验证耗时在2秒之内,将修改以后语句创建成视图create view test7_03 as select ……。
  43. 五、 题目4
  44. 1. pub用户下表student已经用下面两句SQL创建了两索引。
  45. Create index student_birthday on student(birthday);
  46. Create index student_name on student(name);
  47. 2. 下面SQL在访问pub.student时不能用索引student_birthday因此耗时超过1秒,在逻辑不变情况下,修改SQL中标为记红色的子查询的where条件部分,不要修改其它地方,使其能使用索引。
  48. 说明:因为pub.student_testindex数据行数太少,不能通过修改主句where绕过问题。
  49. select * from
  50. (select sid,name,birthday,
  51. (select count(*) from pub.student
  52. where to_char(birthday,'yyyymm')=to_char(t1.birthday,'yyyymm')
  53. ) sameyearmonth,
  54. (select count(*) from pub.student
  55. where extract (year from birthday) =extract (year from t1.birthday)
  56. ) sameyear
  57. from pub.student_testindex t1) where sameyearmonth=35
  58. 3. 修改以后验证耗时在1秒之内,将修改以后语句创建成视图create view test7_04 as select ……。
  59. 六、 题目5
  60. 1. pub用户下表student已经用下面两句SQL创建了两索引。
  61. Create index student_birthday on student(birthday);
  62. Create index student_name on student(name);
  63. 2. 下面SQL在访问pub.student时不能用索引student_birthday因此耗时超过1秒,在逻辑不变情况下,修改SQL中标为记红色的子查询的where条件部分,不要修改其它地方,使其能使用索引。
  64. 说明:因为pub.student_testindex数据行数太少,不能通过修改主句where绕过问题。
  65. 查询nextbirthday晚一天出生的人数
  66. select * from
  67. (select sid,name,birthday,
  68. (select count(*) from pub.student
  69. where birthday-1=t1.birthday
  70. ) nextbirthday
  71. from pub.student_testindex t1) where nextbirthday=7
  72. 3. 修改以后验证耗时在1秒之内,将修改以后语句创建成视图create view test7_05 as select ……。

7-1

  1. create table test7_01 as
  2. select sid,name,birthday from pub.student;
  3. create index suoyin on test7_01 substr(name,1,1);

7-2

  1. create table test7_02 as
  2. select sid,name,birthday from pub.student;
  3. Update test7_02 set birthday=to_date('19881018','yyyymmdd') where substr(sid,12,1)='0';
  4. CREATE INDEX index02
  5. ON test7_02 (birthday,name);

7-3

  1. create view test7_03 as
  2. select * from
  3. (select sid,name,birthday,
  4. (select count(*) from pub.student
  5. where name like substr(t1.name,1,1)||'%'
  6. ) samefirstname
  7. from pub.student_testindex t1)
  8. where samefirstname=7

7-4

  1. create view test7_04 as
  2. select * from
  3. (select sid,name,birthday,
  4. (select count(*) from pub.student
  5. where birthday >= trunc(t1.birthday,'mm') and birthday <=last_day(t1.birthday)
  6. ) sameyearmonth,
  7. (select count(*) from pub.student
  8. where birthday >= trunc(t1.birthday,'YYYY') and birthday <= last_day(add_months(trunc(t1.birthday,'yyyy'),11))
  9. ) sameyear
  10. from pub.student_testindex t1
  11. ) where sameyearmonth=35

7-5

  1. create view test7_05 as
  2. select * from
  3. (select sid,name,birthday,
  4. (select count(*) from pub.student
  5. where birthday=t1.birthday+1
  6. ) nextbirthday
  7. from pub.student_testindex t1) where nextbirthday=7

7-6

(??怎么少了一个题??我是按着报告写的啊)

实验8

  1. 实验一 提交commit和回滚rollback、实体授权
  2. 一、 实验内容
  3. 测试提交commit和回滚rollback的作用,了解锁等待、授权等知识。
  4. 二、 实验步骤
  5. 1. 使用主用户userID登录数据库,简称主窗口。
  6. 2. 使用备用用户userbID登录数据库,简称备用窗口。
  7. 3. 关闭自动提交复选框。按【提交】执行commit,按【回滚】执行rollback
  8. 4. 主用户访问备用用户的表之前,需要在备用账号中将相应的表的相应的权限授权给主用户,这样主用户才可以查询操作备用用户的相应的表。
  9. 在主用户下可以执行select * from userbId.test8_00查询备用用户的表test8_00的数据,如果没有授权,则会提示表没有表找到。
  10. 如果备用用户执行grant select on test8_00 to userID,即授权表test8_00的select权限给用户userID,上面的查询语句就可以正确执行,并查询到相应的结果。
  11. 5. 常用的授权、命令:
  12. grant select on test8_00 to userID授权表test8_00的select权限给用户userID。
  13. grant update on test8_00 to userID授权表test8_00的update权限给用户userID。
  14. grant insert on test8_00 to userID授权表test8_00的insert权限给用户userID。
  15. grant delete on test8_00 to userID授权表test8_00的delete权限给用户userID。
  16. grant all on test8_00 to userID授权表test8_00的all权限给用户userID。
  17. revoke select on test8_00 from userID收回表test8_00的insert权限从用户userID。
  18. 6. 在备用用户下将pub.teacher复制到test8_00中,然后将其所有权限给主用户。
  19. 7. 按表中序号在相应窗口执行对应的命令(主用户访问备用用户表需要授权)。
  20. 8. 假设数据中有张老师,通过上面的操作以后,他在每次查询的时候的年龄是多少?根据你的判断得出结果,然后按步骤进行实验验证,在主用户下创建一个表test8_10(test varchar(20),age numeric (3)),插入10行数据,分表存放10个结果。

8-1

  1. create table test8_10(test varchar(20),age numeric (3));
  2. insert into test8_10 values(‘结果1’,88)
  3. insert into test8_10 values(‘结果2’,90)
  4. insert into test8_10 values(‘结果3’,90)
  5. insert into test8_10 values(‘结果4’,86)
  6. insert into test8_10 values(‘结果5’,90)
  7. insert into test8_10 values(‘结果6’,90)
  8. insert into test8_10 values(‘结果7’,86)
  9. insert into test8_10 values(‘结果8’,86)
  10. insert into test8_10 values(‘结果9’,76)
  11. insert into test8_10 values(‘结果10’,86)

实验9

  1. 实验一 条件数据插入
  2. 一、 实验内容
  3. 学会复制表结构、学会插入数据,特别是学会如何避免重复插入,也就是如何避免插入已经存在的数据。
  4. 二、 实验题目1
  5. 1. 创建表test9_01,表的结构同pub.student_11_1一样。
  6. 2. 为test9_01的sid创建唯一不重复索引。
  7. 3. 将pub用户下的Student中性别是“女”的数据添加到test9_01中。
  8. 4. 将pub用户下的Student_11_1中性别是“女”的数据添加到test9_01中,如果某个学号已经包含在test9_01中,这个记录就不要再插入了(即不要插入重复学号的数据)。
  9. 5. 将pub用户下的Student_11_2中性别是“女”的数据添加到test9_01中,如果某个学号已经包含在test9_01中,这个记录就不要再插入了(即不要插入重复学号的数据)。
  10. 6. 要求完成上述功能,请采用1create table1create index、3insert5SQL方式完成。
  11. 三、 实验题目2
  12. 7. 创建表test9_02,表的结构同pub.student_11_1一样。
  13. 8. 为test9_02的sid创建唯一不重复索引。
  14. 9. 将pub用户下的Student中性别是“女”的且pub.student_course中存在不及格成绩的同学添加到test9_02中。
  15. 10. 将pub用户下的Student_11_1中性别是“女”的且pub.student_course中存在不及格成绩的同学数据添加到test9_02中,如果某个学号已经包含在test9_02中,这个记录就不要再插入了(即不要插入重复学号的数据)。
  16. 11. 将pub用户下的Student_11_2中性别是“女”的且pub.student_course中存在不及格成绩的同学数据添加到test9_02中,如果某个学号已经包含在test9_02中,这个记录就不要再插入了(即不要插入重复学号的数据)。
  17. 12. 要求完成上述功能,请采用1create table1create index、3insert5SQL方式完成。

9-1

  1. create table test7_01 as
  2. select sid,name,birthday from pub.student;
  3. create index suoyin on test7_01 substr(name,1,1);
  4. create table test7_02 as
  5. select sid,name,birthday from pub.student;
  6. Update test7_02 set birthday=to_date('19881018','yyyymmdd') where substr(sid,12,1)='0';
  7. CREATE INDEX index02
  8. ON test7_02 (birthday,name);
  9. create view test7_03 as
  10. select * from
  11. (select sid,name,birthday,
  12. (select count(*) from pub.student
  13. where name like substr(t1.name,1,1)||'%'
  14. ) samefirstname
  15. from pub.student_testindex t1)
  16. where samefirstname=7
  17. create view test7_04 as
  18. select * from
  19. (select sid,name,birthday,
  20. (select count(*) from pub.student
  21. where birthday >= trunc(t1.birthday,'mm') and birthday <=last_day(t1.birthday)
  22. ) sameyearmonth,
  23. (select count(*) from pub.student
  24. where birthday >= trunc(t1.birthday,'YYYY') and birthday <= last_day(add_months(trunc(t1.birthday,'yyyy'),11))
  25. ) sameyear
  26. from pub.student_testindex t1
  27. ) where sameyearmonth=35
  28. create view test7_05 as
  29. select * from
  30. (select sid,name,birthday,
  31. (select count(*) from pub.student
  32. where birthday=t1.birthday+1
  33. ) nextbirthday
  34. from pub.student_testindex t1) where nextbirthday=7
  35. create table test9_01(
  36. sid char(12) not null ,
  37. name varchar2(10) not null,
  38. sex char(2),
  39. age int,
  40. birthday date,
  41. dname varchar2(30),
  42. class varchar2(10)
  43. );
  44. create index suo on test9_01(sid);
  45. insert into test9_01
  46. (
  47. select * from pub.student where sex='女'
  48. );
  49. insert into test9_01
  50. (
  51. select * from pub.student_11_1 where sex='女' and sid not in (select distinct sid from test9_01)
  52. );
  53. insert into test9_01
  54. (
  55. select * from pub.student_11_2 where sex='女' and sid not in (select distinct sid from test9_01)
  56. );

9-2

  1. create table test9_02(
  2. sid char(12) not null ,
  3. name varchar2(10) not null,
  4. sex char(2),
  5. age int,
  6. birthday date,
  7. dname varchar2(30),
  8. class varchar2(10)
  9. );
  10. create index su1 on test9_02(sid);
  11. insert into test9_02
  12. (
  13. select * from pub.student
  14. where sex='女' and
  15. sid in (select distinct sid from pub.student_course where score<60)
  16. );
  17. insert into test9_02
  18. (
  19. select * from pub.student_11_1
  20. where sex='女'
  21. and sid not in (select distinct sid from test9_02)
  22. and sid in (select distinct sid from pub.student_course where score<60)
  23. );
  24. insert into test9_02
  25. (
  26. select * from pub.student_11_2
  27. where sex='女'
  28. and sid not in (select distinct sid from test9_02)
  29. and sid in (select distinct sid from pub.student_course where score<60)
  30. );

关于要注意的一些问题

我在实验开始的三天后写完的,不知道有些东西后面会不会说到,这里就先说说

(1)提交的时候要注意,这是一个事务,提交对应的是commit,回滚rollback,这俩时清空当前当前窗口的日志,然后进行修改

commit,就代表当前执行确定了,无法再回撤

(2)关于数据,有很多数据非常奇怪,比如135分的数据结构,-1分的机组,没有性别的同学

对自己的查询自信一点

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/473176
推荐阅读
相关标签
  

闽ICP备14008679号