山东大学软件学院2017-18学年数据库实验一、二、三、四

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

  • 实验一

创建学生信息表test1_student(学生编号、姓名、性别、年龄、出生日期、院系名称、班级)

create table test1_student

( sid char(12) not null,

name varchar2(10) not null,

sex char(2),

age int,

birthday date,

dname varchar2(30),

class varchar2(10) )

 

创建教师信息表test1_teacher(教师编号、姓名、性别、年龄、院系名称)

create table test1_teacher

( tid char(6) not null,

name varchar2(10) not null,

sex char(2),

age int,

dname varchar2(30))

 

创建课程信息表test1_course(课程编号、课程名称、先行课编号、学分)

 

create table test1_course

( cid char(6) not null,

name varchar2(40) not null,

fcid char(6),

credit numeric (4,1))

 

创建学生选课信息表test1_student_course(学号、课程号、成绩、教师编号)

create table test1_student_course

( sid char(12) not null,

cid char(6) not null,

score numeric (5,1),

tid char (6))

 

创建教师授课信息表test1_teacher_course(教师编号、课程编号)

 

create table test1_teacher_course

( tid char(6) not null,

cid char(6) not null)

 

给表test1_student插入学生信息

 

insert into test1_student values

(200800020101,'王欣','女',19,date'1994-02-02','计算机学院','2010')

insert into test1_student values

(200800020102,'李华','女',20,date'1995-03-03','软件学院','2009')

insert into test1_student values

(200800020103,'赵岩','男',21,date'1996-04-04','软件学院','2009')

 

给表test1_teacher插入教师信息

 

insert into test1_teacher values(100101,'张老师','男',44,'计算机学院')

insert into test1_teacher values(100102,'李老师','女',45,'软件学院')

insert into test1_teacher values(100103,'马老师','男',46,'计算机学院')

 

给表test1_course插入三行数据

 

insert into test1_course values(300001,'数据结构',null,2)

insert into test1_course values(300002,'数据库',300001,2.5)

insert into test1_course values(300003,'操作系统',300001,4)

 

给表test1_student_course插入三行数据

 

insert into test1_student_course values(200800020101,300001,91.5,100101)

insert into test1_student_course values(200800020101,300002,92.6,100102)

insert into test1_student_course values(200800020101,300003,93.7,100103)

 

给表test1_teacher_course插入三行数据

 

insert into test1_teacher_course values(100101,300001)

insert into test1_teacher_course values(100102,300002)

insert into test1_teacher_course values(100103,300003)

 

  • 实验二

找出没有选修任何课的学生的学号、姓名(即没有选课记录的学生)

采用集合差运算

 

create table test2_01 as

(select sid,name from pub.STUDENT)

 minus

( select pub.STUDENT.sid,pub.STUDENT.name

from pub.STUDENT,pub.STUDENT_COURSE

where (pub.STUDENT.SID)=(pub.STUDENT_COURSE.SID))

 

找出至少选修了学号为”200900130417”的学生所选修的一门课的学生的学号、姓名

采用多个in子查询

 

create table test2_02 as

( select sid,name from pub.STUDENT

where (pub.STUDENT.SID)

in (  select sid

from pub.STUDENT_COURSE

where cid

in (  select cid

from pub.STUDENT_COURSE

where sid=200900130417  ) ) )

 

找出至少选修了一门其先行课程号为“300002”号课程的学生的学号、姓名。

同2题采用多个in子查询

 

create table test2_03 as

( select sid,name

from pub.STUDENT

where sid

in (  select sid

from pub.STUDENT_COURSE

where cid

in (  select cid

from pub.COURSE

where fcid=300002 ) ) )

 

找出选修了“操作系统”并且也选修了“数据结构”的学生的学号、姓名。

集合的并集运算

 

create table test2_04 as

((select pub.STUDENT.SID,pub.STUDENT.NAME

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='操作系统')

intersect

( select pub.STUDENT.SID,pub.STUDENT.NAME

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='数据结构'))

 

查询20岁的所有有选课的学生的学号、姓名、平均成绩(avg_score,此为列名,下同)(平均成绩四舍五入到个位)、总成绩(sum_score)

  Round()函数可以四舍五入

 

create table test2_05 as

( select pub.STUDENT.SID sid,pub.STUDENT.NAME name,round(avg(pub.STUDENT_COURSE.SCORE)) avg_score,sum(pub.STUDENT_COURSE.SCORE) sum_score

from pub.STUDENT,pub.STUDENT_COURSE

where pub.STUDENT.AGE='20'

and (pub.STUDENT.SID)= (pub.STUDENT_COURSE.SID)

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

 

查询所有课以及这门课的最高成绩,test2_06有两个列:课程号cid、最高成绩max_score

 

create table test2_06 as

( select cid,max(pub.STUDENT_COURSE.SCORE) max_score

from pub.STUDENT_COURSE

group by cid)

 

查询所有不姓张、不姓李、也不姓王的学生的学号sid、姓名name

  运用like查询张李王开头的名字

 

create table test2_07 as

( ( select sid,name

from pub.STUDENT)

minus

( select sid,name

from pub.STUDENT

where name like '张%'

or name like'李%'

or name like'王%'))

 

查询学生表中每一个姓氏及其人数(不考虑复姓),text2_08有两个列:second_name、p_count

  运用substr()函数来截取姓氏

 

create table test2_08 as

( select (substr(name, 0, 1)) second_name, (count(*)) p_count

from pub.STUDENT

group by substr(name, 0, 1))

 

查询选修了300003号课程的学生的sid、name、score

 

create table test2_09 as

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

from pub.STUDENT,pub.STUDENT_COURSE

where (pub.STUDENT.SID)=(pub.STUDENT_COURSE.SID)

and (pub.STUDENT_COURSE.CID)=300003)

 

  • 实验三

将pub用户下的Student_31及数据复制到主用户的表test3_01,删除表中的学号不全是数字的那些错误数据,学号应该是数字组成,不能够包含字母空格等非数字字符。

方法一:(不用delete,运用正则表达式直接选出全是数字的数据)

create table test3_01 as

( select * from pub.STUDENT_31

where regexp_like( sid,'^[[:digit:]]{12}$'))

 

方法二:(运用delete删除那些不全是数字的数据)

  create table test3_01 as( select * from pub.STUDENT_31 )

  delete from test3_01

where sid in

( select sid from pub.STUDENT_31 where sid not in

( select sid from pub.STUDENT_31 where regexp_like(sid,'^[[:digit:]]{12}$')))

 

将pub用户下的Student_31及数据复制到主用户的表test3_02,删除表中的出生日期和年龄(截止到2012年的年龄,即年龄=2012-出生年份)不一致的那些错误数据。

 

运用extract()函数将birthday中的年份提取

运用decode()函数将age与2012-extract()进行比较,相同为1不同为0,并更名为compare,删除为0的那些数据

 

create table test3_02 as(select * from pub.STUDENT_31)

delete from test3_02 where sid in

( select sid from test3_02,( select sid a_sid,decode(age,2012-extract(year from birthday),1,0) compare from test3_02) where sid = a_sid and compare = 0)

 

将pub用户下的Student_31及数据复制到主用户的表test3_03,删除表中的性别有错误的那些错误数据(性别只能够是“男”、“女”或者空值)。

 

create table test3_03 as(select * from pub.STUDENT_31)

delete from test3_03 where sex not in

(select sex from test3_03 where sex = '男' or sex='女' or sex is null)

 

将pub用户下的Student_31及数据复制到主用户的表test3_04,删除表中的院系名称有空格的、院系名称为空值的或者院系名称小于3个字的那些错误数据。

 

运用length()函数判断字符串长短

运用trim()函数去掉字符串前后的空格

运用replace()函数去掉字符串中间的空格

 

create table test3_04 as(select * from pub.STUDENT_31)

delete from test3_04 where sid in

( select sid from test3_04

where dname is null

or length(dname)<3

or length(dname) > length(trim(dname))

or length(dname) > length(replace(dname,' ','') ) )

 

将pub用户下的Student_31及数据复制到主用户的表test3_05,删除表中的班级不规范的那些错误数据,不规范是指和大多数不一致。

 

通过观察表的内容解决实际问题,发现大部分为XXXX四位数,所以删除长度大于四的数据即可

create table test3_05 as(select * from pub.STUDENT_31)

delete from test3_05 where length(class) > 4

 

将pub用户下的Student_31及数据复制到主用户的表test3_06,删除表中的错误数据,不规范的数据也被认为是那些错误数据。

 

将之前5个实验题中的语句依次执行即可

create table test3_06 as( select * from pub.STUDENT_31 )

delete from test3_06 where sid in (select sid from pub.STUDENT_31 where sid not in (select sid from pub.STUDENT_31 where regexp_like(sid,'^[[:digit:]]{12}$')))

delete from test3_06 where sid in (select sid from test3_06,(select sid a_sid,decode(age,2012-extract(year from birthday),1,0) compare from test3_06) where sid = a_sid and compare = 0)

delete from test3_06 where name in(select name from test3_06 where length(name)<2 or length(name)>length(trim(name)) or length(name) > length(replace(name,' ','')))

delete from test3_06 where sex <>'男' and sex<>'女' and sex is not null

delete from test3_06 where sid in(select sid from test3_06 where dname is null or length(dname)<3 or length(dname)>length(trim(dname)) or length(dname) > length(replace(dname,' ',''))  )

delete from test3_06 where length(class)>4

 

将pub用户下的Student_32及数据复制到主用户的表test3_07,删除其中的错误数据,错误指如下情况:学号在学生信息pub.student中不存在的;

 

create table test3_07 as( select * from pub.STUDENT_COURSE_32)

  delete from test3_07 where sid not in ( select sid from pub.student )

 

将pub用户下的Student_32及数据复制到主用户的表test3_08,删除其中的错误数据,错误指如下情况:课程号和教师编号在教师授课表pub.teacher_course中不同时存在的,即没有该教师教该课程;

 

Where后可以用括号来多项同时查询

 

create table test3_08 as( select * from pub.STUDENT_COURSE_32 )

delete from test3_08 where (tid,cid) not in ( select tid,cid from pub.TEACHER_COURSE )

 

将pub用户下的Student_32及数据复制到主用户的表test3_09,删除其中的错误数据,错误数据指如下情况:成绩数据有错误(需要先找到成绩里面的错误)。

 

成绩正确数据是0-100分之内的,所以用between语句

 

create table test3_09 as( select * from pub.STUDENT_COURSE_32 )

delete from test3_09 where score not in (select score from test3_09 where score between 0 and 100)

 

将pub用户下的Student_32及数据复制到主用户的表test3_10,删除其中的错误数据,错误指如下情况:

将上题语句依次执行

create table test3_10 as( select * from pub.STUDENT_COURSE_32 )

delete from test3_10 where sid not in ( select sid from pub.student )

delete from test3_10 where cid not in ( select cid from pub.COURSE )

delete from test3_10 where tid not in ( select tid from pub.TEACHER )

delete from test3_10 where (tid,cid) not in ( select tid,cid from pub.TEACHER_COURSE )

delete from test3_10 where score not in (select score from test3_10 where score between 0 and 100)

  • 实验四

 

  1. 将pub用户下的Student_41及数据复制到主用户的表test4_01中,使用alter table语句为表增加列:“总成绩:sum_score”并用update语句统计总成绩

 

create table test4_01 as select * from pub.STUDENT_41

alter table test4_01 add (sum_score numeric(6, 1))

update test4_01

set sum_score = (select sum(score)

    from pub.STUDENT_COURSE

    where pub.STUDENT_COURSE.SID = test4_01.SID);

 

  1. 将pub用户下的Student_41及数据复制到主用户的表test4_02中, 使用alter table语句为表增加列:“平均成绩:avg_score”(1位小数) 并用update语句统计

 

create table test4_02 as select * from pub.STUDENT_41

alter table test4_02 add (avg_score numeric(4, 1))

update test4_02

set avg_score = (select round(avg(score),1)

    from pub.STUDENT_COURSE

    where pub.STUDENT_COURSE.SID = test4_02.SID);

 

  1. 将pub用户下的Student_41及数据复制到主用户的表test4_03中, 使用alter table语句为表增加列:“总学分:sum_credit” 并用update语句统计

 

create table test4_03 as select * from pub.STUDENT_41

alter table test4_03 add (sum_credit numeric(4, 1))

update test4_03

set sum_credit = (select sum(credit)

    from pub.COURSE, pub.STUDENT_COURSE

    where pub.STUDENT_COURSE.SID = test4_03.SID

    and pub.STUDENT_COURSE.SCORE >= 60

    and pub.STUDENT_COURSE.CID = pub.COURSE.CID);

 

  1. 将pub用户下的Student_41及数据复制到主用户的表test4_04中。根据列院系名称dname到pub.department找到对应院系编号did,将院系编号填回dname,如果没有对应院系编号,则dname中内容不变

 

create table test4_04 as select * from pub.STUDENT_41

update test4_04

set dname = (select did

       from pub.DEPARTMENT

       where test4_04.DNAME = pub.DEPARTMENT.DNAME)

where exists(select *

    from pub.DEPARTMENT

    where test4_04.DNAME = pub.DEPARTMENT.DNAME)

 

  1. 将pub用户下的Student_41及数据复制到主用户的表test4_05中。使用alter table语句为表增加4个列:总成绩 平均成绩 总学分 院系编号 did vachar(2)

 

create table test4_05 as select * from pub.STUDENT_41

alter table test4_05 add (sum_score numeric(6, 1))

update test4_05

set sum_score = (select sum(score)

       from pub.STUDENT_COURSE

       where pub.STUDENT_COURSE.SID = test4_05.SID);

 

alter table test4_05 add (avg_score numeric(4, 1))

update test4_05

set avg_score = (select round(avg(score),1)

       from pub.STUDENT_COURSE

       where pub.STUDENT_COURSE.SID = test4_05.SID);

 

alter table test4_05 add (sum_credit numeric(4, 1))

update test4_05

set sum_credit = (select sum(credit)

       from pub.COURSE, pub.STUDENT_COURSE

       where pub.STUDENT_COURSE.SID = test4_05.SID

       and pub.STUDENT_COURSE.SCORE >= 60

       and pub.STUDENT_COURSE.CID = pub.COURSE.CID);

 

alter table test4_05 add (did varchar(2))

update test4_05

set did= (select did

       from pub.DEPARTMENT

       where test4_05.DNAME = pub.DEPARTMENT.DNAME)

where exists(select *

    from pub.DEPARTMENT

    where test4_05.DNAME = pub.DEPARTMENT.DNAME)

 

update test4_05

set did = (select did

       from pub.DEPARTMENT_41

       where test4_05.DNAME = pub.DEPARTMENT_41.DNAME)

where exists(select *

    from pub.DEPARTMENT_41

    where test4_05.DNAME = pub.DEPARTMENT_41.DNAME)

 

update test4_05

set did = '00'

where did is null

 

  1. 将pub用户下的Student_41及数据复制到主用户的表test4_06中。剔除性名列中所有空格。

 

create table test4_06 as select * from pub.STUDENT_42

update test4_06

set name = (replace(name, ' ', ''))

 

  1. 将pub用户下的Student_41及数据复制到主用户的表test4_07中。对性别进行规范。

 

create table test4_07 as select * from pub.STUDENT_42

update test4_07

set sex = (replace(sex, ' ', ''))

update test4_07

set sex = (replace(sex, '性', ''))

 

  1. 将pub用户下的Student_41及数据复制到主用户的表test4_08中。对班级进行规范。

 

create table test4_08 as select * from pub.STUDENT_42

update test4_08

set class = (replace(class, '级', ''))

 

  1. 将pub用户下的Student_41及数据复制到主用户的表test4_09中。年龄为空值的根据出生日期设置学生年龄(截止到2012年的年龄)年龄不为空值的不改变。

 

create table test4_09 as select * from pub.STUDENT_42

update test4_09

set age = (2012 - extract(year from birthday))

where age is NULL

 

  1. 综合题,综合6,7,8,9题之外剔除院系名称列中所有空格

 

create table test4_10 as select * from pub.STUDENT_42

update test4_10

set name = (replace(name, ' ', ''))

update test4_10

set dname = (replace(dname, ' ', ''))

update test4_10

set sex = (replace(sex, ' ', ''))

update test4_10

set sex = (replace(sex, '性', ''))

update test4_10

set class = (replace(class, '级', ''))

update test4_10

set age = (2012 - extract(year from birthday))

where age is NULL

 

猜你喜欢

转载自blog.csdn.net/qq_37950762/article/details/82217686