数据库系统概论实验报告.docx
《数据库系统概论实验报告.docx》由会员分享,可在线阅读,更多相关《数据库系统概论实验报告.docx(22页珍藏版)》请在冰豆网上搜索。
![数据库系统概论实验报告.docx](https://file1.bdocx.com/fileroot1/2023-1/6/76e41f8a-1444-43e5-a402-b36d2f314eba/76e41f8a-1444-43e5-a402-b36d2f314eba1.gif)
数据库系统概论实验报告
《数据库系统概论》
实验报告书
专业班级
学号
姓名
指导教师
安徽工业大学计算机学院
实验一:
数据定义/数据操纵语言
实验目的
熟悉Oracle上机环境及Oracle客户端的配置;熟练掌握和使用DDL语言,建立、修改和删除数据库表、主键、外键约束关系和索引;熟练掌握和使用DML语言,对数据进行增加、修改和删除操作。
实验内容
1.SQL数据定义语句:
例1-1:
(建立数据库表)建立教学数据库的四个数据库表,其中Student表中不包含SSEX(C,2)字段,Sname字段为Sname(C,8)且可为空。
例1-2:
(修改数据库表)在Student表中增加SEX(C,2)字段。
例1-3:
(修改列名)将Student表中列名SEX修改为SSEX。
例1-4:
(修改数据库表)将Student表中把Sname字段修改为Sname(C,10)且为非空。
例1-5:
(建立索引)为Score表按课程号升序、分数降序建立索引,索引名为SC_GRADE。
例1-6:
(删除索引)删除索引SC_GRADE。
例1-7:
(建立数据库表)建立数据库表S1(SNO,SNAME,SD,SA),其字段类型定义与Student表中的相应字段(SNO,SNAME,SDEPT,SAGE)的数据类型定义相同。
例1-8:
(修改数据库表)删除成绩表Score的参照完整性约束关系。
例1-9:
(修改数据库表)添加成绩表Score的参照完整性约束关系。
例1-10:
(修改数据库表名)将数据库表S1改名为Student_Temp。
2.SQL数据操纵语句:
例2-1:
(插入数据)按前面各表中的数据分别插入到教学数据库的四个数据库表中。
例2-2:
(多行插入)将表Student中在计算机系(‘CS’)的学生数据插入到表Student_Temp中。
例2-3:
(利用查询来实现表的定义与数据插入)求每一个学生的平均成绩,把结果存入数据库表Student_Gr中。
例2-4:
(修改数据)将Student_Temp表中所有学生的年龄加2。
例2-5:
(修改数据)将Course表中‘程序设计’课时数修改成与‘数据结构’的课时数相同。
例2-6:
(插入数据)向Score表中插入数据(‘98001’,‘001’,95),根据返回信息解释其原因。
例2-7:
(插入数据)向Score表中插入数据(‘97001’,‘010’,80),根据返回信息解释其原因。
例2-8:
(删除数据)删除Student表中学号为‘96001’的学生信息,根据返回信息解释其原因。
例2-9:
(删除数据)删除Course表中课程号为‘003’的课程信息,根据返回信息解释其原因。
例2-10:
(删除数据)删除学生表Student_Temp中学号以96打头的学生信息。
例2-11:
(删除数据)删除数据库表Student_Temp中所有学生的数据。
例2-12:
(删除表)删除数据库表Student_Temp和Student_Gr。
实验程序
createtablestudent(
snochar(5)primarykey,
snamechar(8),
sdeptchar
(2)notnull,
sclasschar
(2)notnull,
sagenumeric
(2)
)
createtablecourse(
cnochar(3)primarykey,
cnamechar(16)unique,
ctimenumeric(3)
)
createtableteach(
tnamechar(8),
tsexchar
(2),
cnochar(3),
tdatedatetime,
tdeptchar
(2),
primarykey(tname,cno,tdept),
foreignkey(cno)referencescourse(cno)
)
createtablescore(
snochar(5),
cnochar(3),
scorenumeric(5,2)
primarykey(sno,cno),
foreignkey(sno)referencesstudent(sno),
foreignkey(cno)referencescourse(cno)
)
altertablestudentaddsexchar
(2)
sp_rename'student.sex','ssex','column'
altertablestudentaltercolumnsnamechar(10)notnull
createuniqueindexsc_gradeonscore(sno,scoredesc)
dropindexscore.sc_grade
createtables1(
snochar(5)primarykey,
snamechar(8),
sdchar
(2),
sanumeric
(2)
)
altertablescoredropconstraintfk_score_student
altertablescoreaddconstraintfk_score_student
foreignkey(sno)
referencesstudent(sno)onupdatecascadeondeletecascade
sp_rename's1','student_temp'
insertintostudentvalues('96001','马小燕','CS','01',21,'女');
insertintostudentvalues('96002','黎明','CS','01',18,'男');
insertintostudentvalues('96003','刘东明','MA','01',18,'男');
insertintostudentvalues('96004','赵志勇','IS','02',20,'男');
insertintostudentvalues('97001','马蓉','MA','02',19,'女');
insertintostudentvalues('97002','李成功','CS','01',20,'男');
insertintostudentvalues('97003','黎明','IS','03',19,'女');
insertintostudentvalues('97004','李丽','CS','02',19,'女');
insertintostudentvalues('96005','司马志明','CS','02',18,'男');
insertintoCoursevalues('001','数学分析',144);
insertintoCoursevalues('002','普通物理',144);
insertintoCoursevalues('003','微机原理',80);
insertintocoursevalues('004','数据结构',72);
insertintocoursevalues('005','操作系统',80);
insertintocoursevalues('006','数据库原理',80);
insertintocoursevalues('007','编译原理',60);
insertintocoursevalues('008','程序设计',40);
insertintoteachvalues('王成刚','男','004','1999.9.5','CS');
insertintoteachvalues('李正科','男','003','1999.9.5','CS');
insertintoteachvalues('严敏','女','001','1999.9.5','MA');
insertintoteachvalues('赵高','男','004','1999.9.5','MA');
insertintoteachvalues('刘玉兰','女','006','2000.2.23','CS');
insertintoteachvalues('王成刚','男','004','2000.2.23','IS');
insertintoteachvalues('马悦','女','008','2000.9.6','CS');
insertintoscorevalues('96001','001',77);
insertintoscorevalues('96001','003',89);
insertintoscorevalues('96001','004',86);
insertintoscorevalues('96001','005',82);
insertintoscorevalues('96002','001',88);
insertintoscorevalues('96002','003',92);
insertintoscorevalues('96002','006',90);
insertintoscorevalues('96005','004',92);
insertintoscorevalues('96005','005',90);
insertintoscorevalues('96005','006',89);
insertintoscorevalues('96005','007',76);
insertintoscorevalues('96003','001',69);
insertintoscorevalues('97001','001',96);
insertintoscorevalues('97001','008',95);
insertintoscorevalues('96004','001',87);
insertintoscorevalues('96003','003',91);
insertintoscorevalues('97002','003',91);
insertintoscorevalues('97002','004',NULL);
insertintoscorevalues('97002','006',92);
insertintoscorevalues('97004','005',90);
insertintoscorevalues('97004','006',85);
insertintostudent_tempvalues('96001','马小燕','CS',21)
insertintostudent_tempvalues('96002','黎明','CS',18)
insertintostudent_tempvalues('97002','李成功','CS',20)
insertintostudent_tempvalues('97004','李丽','CS',19)
insertintostudent_tempvalues('96005','司马志明','CS',18)
selectavg(score)avg1intostudent_grfromscore
groupbysno
updatestudent_temp
setsa=sa+2;
select*fromstudent_gr
updatecourse
setctime=(
selectctime
fromcoursec1
whereame='数据结构'
)wherecname='程序设计'
插入的数据(‘98001’,‘001’,95)中,student中没有98001这个学号
插入的数据(‘97001’,‘010’,80)中,course中没有010这个课程号
删除的数据中,96001这个学号在score中有记录
删除的数据中,003这个课程号在score中有记录
deletefromstudent_temp
wheresnolike'96%'
deletefromstudent_temp
droptablestudent_temp,student_gr
实验总结
本次试验让我初步认识了Oracle上机环境及Oracle客户端的配置;熟习,建立、修改和删除数据库表、主键、外键约束关系和索引;熟练掌握怎么对数据进行增加、修改和删除操作。
实验二:
数据查询语言
实验目的:
体会SQL语言数据查询功能的丰富和复杂。
实验内容:
SQL数据查询语句。
例3-1:
(选择表中的若干列)求全体学生的学号、姓名、性别和年龄。
例3-2:
(不选择重复行)求选修了课程的学生学号。
例3-3:
(选择表中的所有列)求全体学生的详细信息。
例3-4:
(使用表达式)求全体学生的学号、姓名和出生年份。
例3-5:
(使用列的别名)求学生的学号和出生年份,显示时使用别名“学号”和“出生年份”。
例3-6:
(比较大小条件)求年龄大于19岁的学生的姓名和年龄。
例3-7:
(比较大小条件)求计算机系或信息系年龄大于18岁的学生的姓名、系和年龄。
例3-8:
(确定范围条件)求年龄在19岁与22岁(含20岁和22岁)之间的学生的学号和年龄。
例3-9:
(确定范围条件)求年龄不在19岁与22岁之间的学生的学号和年龄。
例3-10:
(确定集合条件)求在下列各系的学生信息:
数学系、计算机系。
例3-11:
(确定集合条件)求不是数学系、计算机系的学生信息。
例3-12:
(匹配查询)求姓名是以“李”打头的学生。
例3-13:
(匹配查询)求姓名中含有“志”的学生。
例3-14:
(匹配查询)求姓名长度至少是三个汉字且倒数第三个汉字必须是“马”的学生。
例3-15:
(匹配查询)求选修课程001或003,成绩在80至90之间,学号为96xxx的学生的学号、课程号和成绩。
例3-16:
(匹配查询)求课程名中包含’_’字符的课程号、课程名和学时数。
例3-17:
(涉及空值查询)求缺少学习成绩的学生的学号和课程号。
例3-18:
(控制行的显示顺序)求选修003课程或004课程的学生的学号、课程号和分数,要求按课程号升序、分数降序的顺序显示结果。
例3-19:
(组函数)求学生总人数。
例3-20:
(组函数)求选修了课程的学生人数。
例3-21:
(组函数)求计算机系学生的平均年龄。
例3-22:
(组函数)求选修了课程001的最高、最低与平均成绩。
例3-23:
(分组查询)求各门课程的平均成绩与总成绩。
例3-24:
(分组查询)求各系、各班级的人数和平均年龄。
例3-25:
(分组查询)输入以下查询语句并执行,观察出现的其结果并分析其原因。
SELECTSNAME,SDEPT,COUNT(*)FROMSTUDENTWHERESDEPT=’CS’GROUPBYSDEPT;
例3-26:
(分组查询)分析以下语句为什么会出现错误。
并给出正确的查询语句。
SELECTSAGEFROMSTUDENTGROUPBYSNO;
例3-27:
(分组查询)求学生人数不足3人的系及其相应的学生数。
例3-28:
(分组查询)求各系中除01班之外的各班的学生人数。
例3-29(涉及空值的查询)分别观察各组函数、行的显示顺序以及分组查询与空值的关系。
例3-30:
(连接查询)求选修了课程001且成绩在70分以下或成绩在90分以上的学生的姓名、课程名称和成绩。
例3-31:
(连接查询与表的别名)求选修了课程的学生的学生姓名、课程号和成绩。
例3-32:
(自身连接查询)求年龄大于’李丽’的所有学生的姓名、系和年龄。
例3-33:
(外部连接查询)求选修了课程002或003的学生的学号、课程号、课程名和成绩,要求必须将002和003课程的相关信息显示出来。
例3-34:
(子查询)求与‘黎明’年龄相同的学生的姓名和系。
例3-35:
(子查询)求选修了课程名为’数据结构’的学生的学号和姓名。
例3-36:
(子查询ANY)求比数学系中某一学生年龄大的学生的姓名和系。
例3-37:
(子查询ALL)求比数学系中全体学生年龄大的学生的姓名和系。
例3-38:
(子查询EXISTS)求选修了课程004的学生的姓名和系。
例3-39:
(返回多列的子查询)求与‘李丽’同系且同龄的学生的姓名和系。
例3-40:
(多个子查询)求与‘‘黎明’同系,且年龄大于‘李丽’的学生的信息。
例3-41:
(子查询中使用表连接)求数学系中年龄相同的学生的姓名和年龄。
例3-42:
(连接或嵌套查询)检索至少选修王成刚老师所授课程中一门课程的女学生姓名。
例3-43:
(嵌套与分组查询)检索选修某课程的学生人数多于3人的教师姓名。
例3-44:
(集合查询)列出所有教师和同学的姓名和性别。
例3-45:
(相关子查询)求未选修课程004的学生的姓名。
例3-46:
(相关子查询)求选修了全部课程的学生的姓名。
例3-47:
(相关子查询)求至少选修了学生‘96002’所选修的全部课程的学生的学号。
例3-48:
(相关子查询)求成绩比所选修课程平均成绩高的学生的学号、课程号、和成绩。
例3-49:
(相关子查询)查询被一个以上的学生选修的课程号。
例3-50:
(相关子查询)查询所有未选课程的学生姓名和所在系。
实验程序
selectsno,sname,ssex,sage
froms
selectsno
fromsc
groupbysno
selects.sno,sname,sdept,sclass,ssex,sage,sc.score,o,ame,c.ctime
froms,sc,c
wheres.sno=sc.sno
ando=o;
selectsno,sname,2009-sagebirthday
froms;
selectsno学号,2009-sage出生年份
froms;
selectsname,sage
froms
wheresage>19;
selectsname,sdept,sage
froms
wheresdeptin('CS','IS')
andsage>18;
selectsno,sage
froms
wheresagebetween19and22;
selectsno,sage
froms
wheresagenotbetween19and22;
select*
froms
wheresdeptin('CS','MA');
select*
froms
wheresdeptnotin('CS','MA');
selectsno,sname
froms
wheresnamelike'李%';
selectsno,sname
froms
wheresnamelike'%志%';
select*froms
wheresnamelike'%马__';
selectsno,cno,score
fromsc
wherecnoin('001','003')
andscorebetween80and90
andsnolike'96%'
select*
fromc
wherecnolike'%\_&'escape'\'
selectsno,cno
fromsc
wherescoreisnull;
select*
fromsc
wherecnoin('003','004')
orderbycno,scoredesc
selectcount(sno)
froms
selectcount(distinctsno)
fromsc
selectavg(sage)
froms
wheresdept='cs'
selectmax(score),min(score),avg(score)
fromsc
wherecno='001'
selectcno,avg(score),sum(score)
fromsc
groupbycno
selectsdept,sclass,count(sclass),avg(sage)
froms
groupbysdept,sclass
orderbysdept
select*froms
selectsname,sdept,count(*)
froms
wheresdept='cs'
groupbysdept,sname
在结果的查询中,只有一行,而sname有好几个,故不能显示出
selectsage
froms
groupbysno,sage
selectsdept,count(*)
froms
groupbysdept
havingcount(*)<3
selectsdept,sclass,count(*)
froms
wheresclass!
='01'
groupbysdept,sclass
orderbysdept
selectsname,cname,score
fromsc,s,c
wheresc.sno=s.sno
ando=o
ando='001'
andscorenotbetween70and90
selectsname,cno,score
fromscsc1,ss1
wheres1.sno=sc1.sno
andoisnotnull
selects1.sname,s1.sdept,s1.sage
fromss1,ss2
wheres1.sage>s2.sage
ands2.sname='李丽'
selectsc.sno,o,ame,score
fromsc,c
whereoin('002','003')and
o=o
selectsname,sdept
froms
wheresagein(
selectsage
froms
wheresname