当前位置:文档之家› 山东大学《数据库系统》上机实验答案 详细整理 2013最新版

山东大学《数据库系统》上机实验答案 详细整理 2013最新版

数据库实验(一)熟悉环境、建立/删除表、插入数据Drop table 表名update dbtest set test=1select * from dbscore1.教师信息(教师编号、姓名、性别、年龄、院系名称)test1_teacher:tid char 6 not null、name varchar 10 not null、sex char 2、age int、dname varchar 10。

根据教师名称建立一个索引。

1、create table test1_teacher(tid char(6) primary key,name varchar(10) not null,sex char(2),age int,dname varchar(10))2.学生信息(学生编号、姓名、性别、年龄、出生日期、院系名称、班级)test1_student:sid char 12 not null、name varchar 10 not null、sex char2、age int、birthday date(oracle的date类型是包含时间信息的,时间信息全部为零)、dname varchar 10、class varchar(10)。

根据姓名建立一个索引。

2、create table test1_student(sid char(12) primary key,name varchar(10) not null,sex char(2),age int,birthday date,dname varchar(10),class varchar(10))3.课程信息(课程编号、课程名称、先行课编号、学分)test1_course:cid char 6 not null、name varchar 10 not null、fcid char 6、credit numeric 2,1(其中2代表总长度,1代表小数点后面长度)。

根据课程名建立一个索引。

3、create table test1_course(cid char(6) primary key,name varchar(10) not null,fcid char(6),credit numeric(2,1))4.学生选课信息(学号、课程号、成绩、教师编号)test1_student_course:sid char 12 not null、cid char 6 not null、score numeric 5,1(其中5代表总长度,1代表小数点后面长度)、tid char 6。

4、 create table test1_student_course(sid char(12) ,cid char(6) ,score numeric(5,1),tid char(6),primary key(sid,cid),FOREIGN KEY (sid) REFERENCES test1_student(sid),FOREIGN KEY (cid) REFERENCES test1_course(cid),FOREIGN KEY (tid) REFERENCES test1_teacher(tid))5.教师授课信息(教师编号、课程编号)test1_teacher_course:tid char 6 not null,cid char 6 not null。

5、create table test1_teacher_course(tid char(6) ,cid char(6) ,primary key(tid,cid),FOREIGN KEY (tid) REFERENCES test1_teacher(tid),FOREIGN KEY (cid) REFERENCES test1_course(cid))二、创建索引1、create index index_table1 on test1_teacher(name);2、create index index_table2 on test1_student(name);3、create index index_table3 on test1_course(name);三、插入数据1、insert into test1_teacher values('100101','张老师','男',44,'计算机学院');insert into test1_teacher values('100102','李老师','女',45,'软件学院');insert into test1_teacher values('100103','马老师','男',46,'计算机学院');2、insert into test1_student values('200800020101','王欣','女',19,to_date('19940202','yyyymmdd'),'计算机学院','2010');insert into test1_student values('200800020102','李华','女',20,to_date('19950303','yyyymmdd'),'软件学院','2009');insert into test1_student values('200800020103','赵岩','男',18,to_date('19960404','yyyymmdd'),'软件学院','2009');3、insert into test1_course values('300001','数据结构','',2);insert into test1_course values('300002','数据库','300001',2.5); insert into test1_course values('300003','操作系统','300001',4);4、Insert into test1_student_coursevalues('200800020101','300001',91.5,'100101');insert into test1_student_coursevalues('200800020101','300002',92.6,'100102');insert into test1_student_coursevalues('200800020101','300003',93.7,'100103');5、insert into test1_teacher_course values('100101','300001');insert into test1_teacher_course values('100102','300002');insert into test1_teacher_course values('100103','300003');数据库实验(二)检索查询1、找出没有选修任何课程的学生的学号、姓名。

create table test2_01 as select sid ,namefrom pub.studentwhere sid not in(select sidfrom pub.student_course)2、找出至少选修了学号为“200900130417”的学生所选修的一门课的学生的学号、姓名。

create table test2_02 as select distinct student.sid,namefrom pub.student, pub.student_coursewhere student_course.sid = student.sid and student_course.cid in (select cidfrom pub.student_coursewhere sid='200900130417')3、找出至少选修了一门其先行课程号为“300002”号课程的学生的学号、姓名。

create table test2_03 as select distinct student.sid,namefrom pub.student, pub.student_coursewhere student_course.sid = student.sid and student_course.cid in (select cidfrom pub.coursewhere fcid='300002')4、找出选修了“操作系统”并且也选修了“数据结构”的学生的学号、姓名。

create table test2_04 as select sid,namefrom pub.studentwhere sid in(select sidfrom pub.student_course,pub.coursewhere student_course.cid=course.cidand name ='操作系统')and sid in(select sidfrom pub.student_course,pub.coursewhere student_course.cid=course.cidand name ='数据结构')5、查询20岁的所有有选课的学生的学号、姓名、平均成绩(avg_score,此为列名,下同)(平均成绩四舍五入到个位)、总成绩(sum_score)create table test2_05 as select student.sid,name,cast(avg(score) as numeric(5,0)) avg_score,sum(score) sum_scorefrom pub.student,pub.student_coursewhere student.sid = student_course.sid and age ='20'group by student.sid,name6、查询所有课以及这门课的最高成绩,test2_06有两个列:课程号cid、最高成绩max_scorecreate table test2_06 as select sid,max(score) max_scorefrom pub.student_coursegroup by cid7、查询所有不姓张、不姓李、也不姓王的学生的学号sid、姓名namecreate table test2_07 as select sid,namefrom pub.studentwhere name not in(select namefrom pub.studentwhere name like '张%' or name like '李%' or name like '王%')8、查询学生表中每一个姓氏及其人数(不考虑复姓),test2_08有两个列:second_name、p_countcreate table test2_08 as select substr(name,1,1) second_name,count(*) p_countfrom pub.studentgroup by substr(name,1,1)9、查询选修了300003号课程的学生的sid、name、scorecreate table test2_09 as select student.sid,,scorefrom pub.student,pub.student_coursewhere student.sid = student_course.sidand cid ='300003'10、查所有有成绩记录的学生sid和cidcreate table test2_10 as select sid,cidfrom pub.student_coursewhere score is not null数据库实验(三)复制表、删除数据1.将pub用户下的Student_31及数据复制到主用户的表test3_01,删除表中的学号不是12位数字的错误数据。

相关主题