1、大数据库课程设计报告材料完整版数据库课程设计班 级 物联网1202 学 号 3120611027 姓 名 杨璐 指导老师 年轶 2014 年 1 月一、引言1目的课程设计为学生提供了一个既动手又动脑,独立实践的机会,将课本上的理论知识和实际有机的结合起来,锻炼学生的分析解决实际问题的能力。提高学生适应实际,实践编程的能力。课程设计的目的:(1)加深对数据库系统、软件工程、程序设计语言的理论知识的理解和应用水平;(2)在理论和实验教学基础上进一步巩固已学基本理论及应用知识并加以综合提高;(3)学会将知识应用于实际的方法,提高分析和解决问题的能力,增强动手能力;(4)为毕业设计和以后工作打下必要基
2、础。2题目题目2.设计一个大学教学数据库应用系统。该系统涉及学生、教师、课程、分组、登记。数据见附表2。因时间关系,只要求每个学生任选1个题目,如有时间有兴趣,可做另外一题,酌情加分。3要求运用数据库基本理论与应用知识,在微机RDBMS(SQL Server)的环境上建立一个数据库应用系统。要求把现实世界的事物及事物之间的复杂关系抽象为信息世界的实体及实体之间联系的信息模型,再转换为机器世界的数据模型和数据文件,并对数据文件实施检索、更新和控制等操作。 (1)用E-R图设计选定题目的信息模型;(2)设计相应的关系模型,确定数据库结构;(3)分析关系模式各属于第几范式,阐明理由;(4)设计应用系
3、统的系统结构图;(5)通过设计关系的主码约束、外码约束和使用CHECK实现完整性控制;(6)完成实验内容所指定的各项要求;(7)分析遇到的问题,总结并写出课程设计报告;(8)自我评价二、系统的分析与设计1概念设计 2逻辑设计STUDENT(sno,sname,address,zip,state,city,sex) sno是主码TEACHER(tno,tname,phone,salary) tno是主码COURSE(cno,cname,department,credit) cno是主码SECTION(secno,cno,tno,sqty) secno、tno、cno是主码,cno、tno是外码E
4、NROLL(secno,cno,sno,grade) cno,sno是主码,cno,sno是外码3系统功能结构4完整性设计/*包含:关系主码、外码、CHECK约束。并给出相应的SQL语句*/COURSE的完整性约束: cno CHAR(10) PRIMARY KEYSTUDENT的完整性约束: sno CHAR(10) PRIMARY KEYTEACHER的完整性约束: tno CHAR(10) PRIMARY KEYSECTION的完整性约束: PRIMARY KEY(secno,tno,cno),FOREIGN KEY(tno) REFERENCES TEACHER(tno),FOREIG
5、N KEY(cno) REFERENCES COURSE(cno)ENROLL的完整性约束: PRIMARY KEY(cno,sno),FOREIGN KEY(sno) REFERENCES STUDENT(sno),FOREIGN KEY(cno) REFERENCES COURSE(cno)三、系统的实现题目2 1.创建数据库创建名为“SCTE”的数据库2.创建各基本表CREATE TABLE COURSE(cno CHAR(10) PRIMARY KEY, cname CHAR(30), department CHAR(30), credit SMALLINT ); CREATE TAB
6、LE STUDENT(sno CHAR(10) PRIMARY KEY, sname CHAR(30), address CHAR(30), zip CHAR(10), city CHAR(20), state CHAR(10), sex CHAR(2) ); CREATE TABLE TEACHER (tno CHAR(10) PRIMARY KEY, tname CHAR(20), phone CHAR(10), salary NUMERIC(10,2) ); CREATE TABLE SECTION (secno CHAR(2), tno CHAR(10), cno CHAR(10),
7、sqty INT, PRIMARY KEY(secno,tno,cno), FOREIGN KEY(tno) REFERENCES TEACHER(tno), FOREIGN KEY(cno) REFERENCES COURSE(cno) ); CREATE TABLE ENROLL (cno CHAR(10), secon CHAR(2), sno CHAR(10), grade INT, PRIMARY KEY(cno,sno), FOREIGN KEY(sno) REFERENCES STUDENT(sno), FOREIGN KEY(cno) REFERENCES COURSE(cno
8、) );3.完成数据的录入COURSE表录入数据INSERTINTO COURSEVALUES(450,Western Civilization,History,3);INSERTINTO COURSEVALUES(730,Calculus Iv,Math,4);INSERTINTO COURSEVALUES(290,English Composition,English,3);INSERTINTO COURSEVALUES(480,Compiler Writing,Computer Science,3);SELECT*FROM COURSESTUDENT表录入数据INSERTINTO STU
9、DENT VALUES(148,Susan powell,534 East River Dr,19041,Haverford,PA,F);INSERTINTO STUDENT VALUES(210,Bob Dawson,120 South Jefferson,02891,Newport,RI,M);INSERTINTO STUDENT VALUES(298,Howard Mansfield,290 Wynkoop Drive,22180,Vienna,VA,M);INSERTINTO STUDENT VALUES(348,Susan Pugh,534 East Hampton Dr,06107
10、,Hartford,CN,F);INSERTINTO STUDENT VALUES(349,Joe Adams,73 Emmerson Street,19702,Newark,DE,M);INSERTINTO STUDENT VALUES(354,Janet Ladd,441 10th Street,18073,Pennsburg,PA,F);INSERTINTO STUDENT VALUES(410,Bill Jone,120 South Harrison,92660,Newport,CA,M);INSERTINTO STUDENT VALUES(473,Carol Dean,983 Par
11、k Avenue,02169,Boston,MA,F);INSERTINTO STUDENT VALUES(548,Allen thomas,238 West Ox Road,60624,Chicago,IL,M);INSERTINTO STUDENT VALUES(558,Val Shipp,238 Westport Road,60556,Chicago,IL,F);INSERTINTO STUDENT VALUES(649,John Anderson,473 Emmory Street,10008,New York,NY,M);INSERTINTO STUDENT VALUES(654,J
12、anet Yhomas,441 6th Street,16510,Erie,PA,F);SELECT*FROM STUDENTTEACHER录入数据INSERTINTO TEACHERVALUES(303,Dr.Horn,257-3049,27540.00);INSERTINTO TEACHERVALUES(290,Dr.Lowe,257-2390,31450.00);INSERTINTO TEACHERVALUES(430,Dr.Engle,56-4621,38200.00);INSERTINTO TEACHERVALUES(180,Dr.Cooke,257-8088,29560.00);I
13、NSERTINTO TEACHERVALUES(560,Dr.Olsen,257-8086,31778.00);INSERTINTO TEACHERVALUES(784,Dr.Scango,257-3046,32098.00);SELECT*FROM TEACHERSECTION表录入数据INSERTINTO SECTION VALUES(1,303,450,2);INSERTINTO SECTION VALUES(1,290,730,6);INSERTINTO SECTION VALUES(1,430,290,3);INSERTINTO SECTION VALUES(1,180,480,3)
14、;INSERTINTO SECTION VALUES(2,560,450,2);INSERTINTO SECTION VALUES(2,784,480,2);SELECT*FROM SECTIONENROLL表录入数据INSERTINTO ENROLLVALUES(730,1,148,3);INSERTINTO ENROLLVALUES(450,2,210,3);INSERTINTO ENROLLVALUES(730,1,210,1);INSERTINTO ENROLLVALUES(290,1,298,3);INSERTINTO ENROLLVALUES(480,2,298,3);INSERT
15、INTO ENROLLVALUES(730,1,348,2);INSERTINTO ENROLLVALUES(290,1,349,4);INSERTINTO ENROLLVALUES(480,1,348,4);INSERTINTO ENROLLVALUES(480,1,410,2);INSERTINTO ENROLLVALUES(450,1,473,2);INSERTINTO ENROLLVALUES(730,1,473,3);INSERTINTO ENROLLVALUES(480,2,473,0);INSERTINTO ENROLLVALUES(290,1,548,2);INSERTINTO
16、 ENROLLVALUES(730,1,558,3);INSERTINTO ENROLLVALUES(730,1,649,4);INSERTINTO ENROLLVALUES(480,1,649,4);INSERTINTO ENROLLVALUES(450,1,654,4);INSERTINTO ENROLLVALUES(450,2,548,1);SELECT*FROM ENROLLCOURSESTUDENTTEACHERSECTIONENROLL4.检索系名为“Math”和“English”的课程信息SELECT *FROM COURSEWHERE department = Math OR
17、department = English5.按字母顺序列出教师姓名和电话号码SELECT tname,phoneFROM TEACHERORDER BY tname6.检索电话号码不是以“257”打头的教师姓名和电话号码SELECT tname,phoneFROM TEACHERWHERE phone not like 257%7.检索数学系所有成绩大于3的课程名、系名、学分SELECT cname,department,creditFROM COURSEWHERE cno IN(SELECT cno FROM ENROLL WHERE grade3 AND cno=730);8.检索没有选修
18、任何课的学生姓名、学号SELECT sno,sname FROM STUDENTWHERE NOT EXISTS(SELECT * FROM ENROLL WHERE STUDENT.sno=ENROLL.sno);9.检索没有选修课程“Calculus Iv”的学生学号SELECT snoFROM STUDENTWHERE sno NOT IN (SELECT STUDENT.snoFROM COURSE,STUDENT,ENROLLWHERE cname=Calculus Iv AND COURSE.cno=ENROLL.cno AND STUDENT.sno=ENROLL.sno);*1
19、0.检索至少选修教师“Dr. Lowe”所开全部课程的学生学号SELECT DINSTINCT snoFROM ENROLL ENROLLXWHERE NOT EXISTS(SELECT *FROM ENROLL ENROLLYWHERE cno in( SELECT cno FROM TEACHER,SECTION, WHERE tname=Dr.Lowe AND TEACHER.tno=SECTION.tno) AND NOT EXISTS (SELECT *FROM ENROLL ENROLLZ WHERE ENROLLZ.sno=ENROLLX.sno AND ENROLLZ.cno=
20、ENROLLY.cno)(修改数据验证,该老师教授课程号为730和500,只有学号148的学生同时选择了这两门课)SECTIONENROLL结果11.检索每门课学生登记的人数、相应的课程名、课程号、分组号SELECT DISTINCT secno,ENROLL.cno,cname,sqtyFROM ENROLL,COURSE,SECTIONWHERE SECTION.cno=ENROLL.cno AND COURSE.cno=ENROLL.cno12.检索选修两门以上课程的学生姓名SELECT snameFROM STUDENTWHERE sno in (SELECT sno FROM ENR
21、OLL GROUP BY sno HAVING COUNT(*)2)13.检索只有男生选修的课程和学生名SELECT cname,snameFROM COURSE ,STUDENT ,ENROLL WHERE STUDENT.sno=ENROLL.sno AND COURSE.cno=ENROLL.cno AND COURSE.cno in (SELECT cno FROM COURSEWHERE cno not in (SELECT DISTINCT cnoFROM STUDENT ,ENROLL WHERE sex =F and STUDENT.sno=ENROLL.sno)14.检索所有
22、学生都选修的课程名、学生名、授课教师名、该生成绩SELECT cname,sname,tname,gradeFROM STUDENT ,TEACHER ,SECTION ,ENROLL ,COURSE WHERE COURSE.cno=ENROLL.cno AND STUDENT.sno=ENROLL.sno AND TEACHER.tno=SECTION.tno AND SECTION.cno=ENROLL.cnoAND SECTION.secno=ENROLL.seconAND COURSE.cno=(SELECT cno FROM ENROLL GROUP BY cno HAVING C
23、OUNT (*)=12)15.删去名为“Joe Adams”的所有记录DELETE FROM STUDENTWHERE sname=Joe Adams;SELECT *FROM STUDENT16.把教师“Scango”的编号改为“666”UPGRADE TEACHERSET tno=666WHERE tname=Dr.Scango SELECT *FROM TEACHER17.统计教师“Engle”教的英语课的学生平均分SELECT AVG(grade)AVGFROM ENROLLWHERE grade in(SELECT gradeFROM TEACHER,SECTION,ENROLLWH
24、ERE tname=Dr.Engle AND TEACHER.tno=SECTION.tno AND SECTION.cno=ENROLL.cnoAND ENROLL.secno=SECTION.secno)18.统计各门课程的选课人数SELECT COURSE.cname,COUNT(ENROLL.sno)FROM ENROLL,COURSEWHERE ENROLL.cno=COURSE.cno GROUP BY COURSE.cname;19.输出如下报表:学生名课程名教师名成绩SELECT sname 学生名,cname 课程名,tname 教师名,grade 成绩FROM ENROLL
25、,STUDENT,COURSE,TEACHER,SECTIONWHERE ENROLL.sno=STUDENT.sno AND SECTION.cno = COURSE.cno AND ENROLL.secno = SECTION.secno AND ENROLL.cno = SECTION.cno AND SECTION.tno = TEACHER.tno*20.定义并验证触发器,当登记表增加一条新的记录时,自动在分组表中更新相应属性。CREATE TRIGGER ENROLL_1ON ENROLLAFTER INSERTASUPDATE SECTIONSET sqty=sqty+1FROM
26、 SECTION,insertedWHERE inserted.secno=SECTION.secnoAND o=SECTION.cno验证结果:INSERTINTO ENROLLVALUES(450,2,354,1)SELECT *FROM SECTION原始数据执行结果四、课程设计小结在本次课设之前,我已经在前段学习期间上机编写过类似相关的SQL语句来完成对于学生-课程-选课的信息查询,这次在原先实验的基础上添加了分组和教师表,创建数据库、基本表和录入数据的过程与上机实验基本相同,因此很容易就完成了前三个步骤,在这个过程中对于数据库、表的创建以及数据的输入的操作都更加的熟练。题目4-19都
27、是对于表中数据进行查询、修改、删除等操作,并且运用了ORDER BY、GROUP BY等功能短语以及COUNT、AVG等聚集函数。前两题都是基本的查询,比较简单,但由于输入数据时Dr.Engle中的.多按了一个空格,结果在调试的时候出现的是错误的结果,结果不正确(见截图。第10题是本次课设最难的题目,自己也尝试着去做,但是由于理解的失误以及数据的巧合性,错误的SQL语句得出的结果却是正确的。这就导致了自己以为解决了这道难题,以至于在给老师检查的时候被指出严重的错误,感到很羞愧。后来仔细分析题意,按照所有的关键字来编写查询语句,参照书上关于not exists语句以及嵌套循环的语句,用谓词演算将
28、题目转换成逻辑运算,等价为“没有这样课程y,老师Dr.Lowe教授了这门课,而学生X没有选”。p表示“老师教授了课程y”,q表示“学生X选了课y”,等价于y(pq)。参照书上的例题,分层次地编写功能语句。因为题目的巧合性所以在检验时修改了数据,添加了该老师新开另一门课,只有学号148的学生同时选择了这门老师的两门课。然后再去验证结果发现是正确的。第13题的题目刚开始理解的不透彻,所以直接查询了男生选修的课程,得到的结果有10个,但照表检查可以看出有些课程仍是有女生选的,与题目“只有男生选修”的条件不符,因此重新分析,发现可以使用嵌套查询先用子查询把女生选择的课程选出来,然后父查询在子查询的结果之外(not in)查询,这样就把女生选修的课程出去,这样就避免出现选择的课程男、女生都选修的错误。(见截图)第15、16题是对于学生、教师表的修改和删除。在执行过程中程序一直报错,始终找不出问题所在。后来经过询问同学,发现SECTION和ENROLL表中引用了这两个表中的sno和tno,由于其引用关系,所以无法对于这两个表进行修改。因此需要先删除外码的引用关系,然后才能对其进行修改
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1