数据库原理与应用实验指导书1.docx
- 文档编号:30536191
- 上传时间:2023-08-16
- 格式:DOCX
- 页数:60
- 大小:535.64KB
数据库原理与应用实验指导书1.docx
《数据库原理与应用实验指导书1.docx》由会员分享,可在线阅读,更多相关《数据库原理与应用实验指导书1.docx(60页珍藏版)》请在冰豆网上搜索。
数据库原理与应用实验指导书1
《数据库原理与应用》
实验指导书及课程设计指导书
山东交通学院信息工程系
计算机应用教研室沈祥玖编写
2007.07
目录
实验一:
数据库的建立和维护
实验二:
数据库的简单查询
实验三:
数据库SQL语言操作与应用
实验四:
数据库完整性、安全与恢复技术操作
实验五:
数据库综合应用实例
数据库课程设计:
图书馆信息系统
实验一:
数据库的建立和维护
实验目的
熟练掌握建立数据库和表,向数据库输入数据、修改数据和删除数据的操作。
实验内容
建立数据库并设计各表,输入多条实际数据,并实现数据的增、删、改操作。
实验步骤:
创建用于学生管理数据库,数据库名为XSGL,包含学生的基本信息,课程信息和选课信息。
数据库XSGL包含下列3个表:
(l)student:
学生基本信息。
(2)course:
课程信息表。
(3)sc:
学生选课表。
各表的结构分别如表1、表2和表3所示。
表1学生信息表:
student
列名数据类型长度是否允许为空值
sno字符型10否
sname字符型10否
ssex字符型2否
sage整数型是
sdept字符型4否
表2课程信息表:
course
列名数据类型长度是否允许为空值
cno字符型3否
cname字符型30否
credit整数型是
pcno字符型3是
表3学生选课表:
sc
列名数据类型长度是否允许为空值
sno字符型10否
cno字符型30否
grade整数型是
一.数据库的建立:
1.用可视化界面建立:
(a)在SQLSERVER中用企业管理器-新建数据库;
(b)在ACCESS中-新建数据库;
2.命令方式建立:
(a)在SQLSERVER中,在查询分析器中使用T-SQL语句:
CREATEDATABASEXSGL
ON(NAME='XSGL_DATA',
FILENAME='E:
\XSGL.MDF',
SIZE=10MB,
MAXSIZE=50MB,
FILEGROWTH=5%)
LOGON
(NAME='XSGL_Log',
FILENAME='e:
\XSGL_Log.ldf',
SIZE=2MB,
MAXSIZE=5MB,
FILEGROWTH=1MB)
(b)在ACCESS中在命令窗口使用SQL语句:
CREATEDATABASEXSGL
二.表的建立:
1.用可视化界面建立:
I:
在ACCESS中—数据库XSGL—表—新建
II:
在SQLSERVER中用企业管理器—数据库—XSGL—表—右键—新建表;
2.命令方式建立:
(a)在ACCESS的命令窗口用下列SQL语句:
opendatabaseXSGL
Createtablestudent(snoCHAR(5),snameCHAR(10),ssexCHAR
(2),sageint,sdeptCHAR(4))
Createtablecourse(cnoCHAR
(2),cnameCHAR(30),creditINT,pcnoCHAR
(2)NULL)
Createtablesc(snoCHAR(5),cnoCHAR
(2),gradeINTNULL)
(b)在SQLSERVER中查询分析器的编辑窗口中用下列SQL语句:
useXSGL
Createtablestudent(snoCHAR(5),snameCHAR(10),ssexCHAR
(2),sageint,sdeptCHAR(4))
go
Createtablecourse(cnoCHAR
(2),cnameCHAR(30),creditINT,pcnoCHAR
(2)NULL)
go
Createtablesc(snoCHAR(5),cnoCHAR
(2),gradeINTNULL)
go
三.表数据的添加:
1.用可视化方法:
I:
在ACCESS中数据库—XSGL—表—(表名)
II:
在SQLSERVER中用企业管理器—数据库—XSGL—表-表名—右键-打开表-返回所有行;
输入下列数据:
snosnamessexsagesdept
95001李勇男20CS
95002刘晨女19IS
95003王敏女18MA
95004张立男19IS
95005刘云女18CS
cnocnamecreditpcno
1数据库45
2数学6
3信息系统31
4操作系统46
5数据结构47
6数据处理3
7PASCAL语言46
snocnograde
95001192
95001285
95001388
95002290
95002380
95003285
95004158
95004285
2.在ACCESS的命令窗口或SQLSERVER中查询分析器的编辑窗口中使用下列SQL语句插入数据:
insertintostudent(sno,sname,ssex,sage,sdept)values('95001','李勇','男',20,'CS')
insertintostudent(sno,sname,ssex,sage,sdept)values('95002','刘晨','女',19,'IS')
insertintostudent(sno,sname,ssex,sage,sdept)values('95003','王敏','女',18,'MA')
insertintostudent(sno,sname,ssex,sage,sdept)values('95004','张立','男',19,'IS')
insertintostudent(sno,sname,ssex,sage,sdept)values('95005','刘云','女',18,'CS')
insertintocourse(cno,cname,credit,pcno)values('1','数据库',4,'5')
insertintocourse(cno,cname,credit,pcno)values('2','数学',6,null)
insertintocourse(cno,cname,credit,pcno)values('3','信息系统',3,'1')
insertintocourse(cno,cname,credit,pcno)values('4','操作系统',4,'6')
insertintocourse(cno,cname,credit,pcno)values('5','数据结构',4,'7')
insertintocourse(cno,cname,credit,pcno)values('6','数据处理',3,null)
insertintocourse(cno,cname,credit,pcno)values('7','PASCAL语言',4,'6')
insertintosc(sno,cno,grade)values('95001','1',92)
insertintosc(sno,cno,grade)values('95001','2',85)
insertintosc(sno,cno,grade)values('95001','3',88)
insertintosc(sno,cno,grade)values('95002','2',90)
insertintosc(sno,cno,grade)values('95002','3',80)
insertintosc(sno,cno,grade)values('95003','2',85)
insertintosc(sno,cno,grade)values('95004','1',58)
insertintosc(sno,cno,grade)values('95004','2',85)
四.表数据的修改:
1.用可视化方法:
I:
在ACCESS中,数据库—XSGL—表—(表名);
II:
在SQLSERVER中用企业管理器—数据库—XSGL—表-表名—右键-打开表-返回所有行;
在表格中将相应的数据修改即可。
2.命令方法:
将所有学生的年龄增加一岁:
updatestudentsetsage=sage+1
将4号课程的学分改为4:
updatecoursesetcredit=4wherecno=4
设置7号课程没有先行课:
updatecoursesetpcno=nullwherecno=7
将95001号学生的1号课程的成绩增加3分:
updatescsetgrade=grade+3wheresno=’95001’andcno=’1’
五.表数据的删除:
1.用可视化方法:
I:
在ACCESS中,数据库—XSGL—表—(表名);
单击行左边的删除标记块,使之变黑即从逻辑上删除了该记录。
II:
在SQLSERVER中用企业管理器—数据库—XSGL—表-表名—右键-打开表-返回所有行;
单击左边的行标记,选定某一行,或单击后拖动选择相邻的多行,再右击鼠标选择弹出式菜单中的删除。
2.命令方法:
删除学号为95005的学生的记录:
deletefromstudentwheresno=’95005’
删除所有的课程记录:
deletefromcourse
删除成绩为不及格(少于60分)的学生的选课记录:
deletefromscwheregrade<60
思考:
比较用可视化界面与命令方式在数据的插入、修改、删除方面的优缺点。
实验二:
数据库的简单查询
实验目的:
掌握简单表的数据查询、数据排序和数据联结查询的操作方法。
实验内容:
简单查询操作和连接查询操作。
实验步骤:
一.单表查询:
1.查询全体学生的学号和姓名:
selectsno,snamefromstudent
2.查询全体学生的所有信息:
select*fromstudent
或者selectsno,sname,ssex,sage,sdeptfromstudent
3.查询全体学生的姓名,出生年份,和所在系,并用小写字母表示所有系名:
ACCESS中:
selectsname,'出生年份为:
',year(date()-sage,lower(sdept)fromstudent
SQLServer中:
selectsname,'出生年份为:
',year(getdate())-sage,lower(sdept)fromstudent
4.给上例的结果集指定列名:
ACCESS中:
selectsname,'‘出生年份为:
'出生,year(date()-sage年份,lower(sdept)系名fromstudent
SQLServer中:
selectsname,'出生年份为:
'出生,year(getdate())-sage年份,lower(sdept)系名fromstudent
5.查询选修了课程的学生的学号:
selectdistinctsnofromsc
比较:
selectsnofromsc
6.查询年龄在20岁以下的学生的姓名及其年龄:
selectsname,sagefromstudentwheresage<20
7.查询考试成绩有不及格的学生的学号:
selectdistinctsnofromscwheregrade<60
比较:
selectsnofromscwheregrade<60
8.查询年龄在20-30岁直接的学生的姓名,姓名,所在系:
selectsname,ssex,sdeptfromstudentwheresagebetween20and30
9.查询IS,CS,MA系的所有学生的姓名和性别:
selectsname,ssexfromstudentwheresdeptin('IS','MA','CS')
10.查找所有姓’李’的学生的姓名,学号和性别:
selectsname,sno,ssexfromstudentwheresnamelike'李%'
比较:
将学生表中的’95001’号学生的姓名’李勇’改为’李勇勇’,再执行:
selectsname,sno,ssexfromstudentwheresnamelike'李_'
11.查询没有先行课的课程的课程号cno和课程名cname:
selectcno,cnamefromcoursewherepcnoisnull
二.查询结果排序
12.查询选修了3号课程的学生的学号和成绩,并按分数降序排列:
selectsno,gradefromscwherecno='3'orderbygradeDESC
23.查询全体学生的情况,查询结果按所在系号升序排列,同一系中的学生按年龄降序排列:
select*fromstudentorderbysdeptASC,sageDESC
三.连接查询:
14.查询每个学生及其选修课程的情况:
selectstudent.*,sc.*fromstudent,scwherestudent.sno=sc.sno
比较:
笛卡尔集:
selectstudent.*,sc.*fromstudent,sc
自然连接:
selectstudent.sno,sname,ssex,sdept,cno,gradefromstudent,scwherestudent.sno=sc.sno
15.查询每一门课程的间接先行课(只求两层即先行课的先行课):
selectFo,Second.pcno间接先行课fromcourseFirst,courseSecondwhereFirst.pcno=So
比较:
selectFo,Second.pcno间接先行课fromcourseFirst,courseSecondwhereFirst.pcno=SoandSecond.pcnoisnotnull
16.列出所有学生的基本情况和选课情况,若没有选课,则只列出基本情况信息:
SQLServer中:
selects.sno,sname,ssex,sdept,cno,gradefromstudents,scscwheres.sno*=sc.sno
ACCESS中:
selects.sno,sname,ssex,sdept,cno,gradefromstudentsleftjoinscscons.sno=sc.sno
17.查询每个学生的学号,姓名,选修的课程名和成绩:
selectS.sno,sname,cname,gradefromstudentS,courseC,scSCwhereS.sno=SC.snoandC.cno=SC.cno
思考:
如何求出不及格学生的学号,姓名,不及格的课程名以及成绩。
实验三:
数据库SQL语言操作与应用
实验目的:
加深对嵌套查询语句的理解。
实验内容:
使用IN、比较符、ANY或ALL和EXISTS操作符进行嵌套查询操作。
实验步骤:
一.使用带IN谓词的子查询
1.查询与’刘晨’在同一个系学习的学生的信息:
select*fromstudentwheresdeptin
(selectsdeptfromstudentwheresname='刘晨')
比较:
select*fromstudentwheresdept=
(selectsdeptfromstudentwheresname='刘晨')的异同
比较:
select*fromstudentwheresdept=
(selectsdeptfromstudentwheresname='刘晨')andsname<>'刘晨V
比较:
selectS1.*fromstudentS1,studentS2whereS1.sdept=S2.sdeptandS2.sname='刘晨'
2.查询选修了课程名为’信息系统’的学生的学号和姓名:
SQLServer中:
selectsno,snamefromstudentwheresnoin
(selectsnofromscwherecnoin
(selectcnofromcoursewherecname='信息系统'))
ACCESS中:
selectsno,snamefromstudentwheresnoin
(selectsnofromsc,coursewhereo=o
andcname='信息系统')
3.查询选修了课程’1’和课程’2’的学生的学号:
selectsnofromstudentwheresnoin(selectsnofromscwherecno='1')
andsnoin(selectsnofromscwherecno='2')
比较:
查询选修了课程’1’或课程’2’的学生的sno:
selectsnofromscwherecno='1'orcno='2'
比较连接查询:
selectA.snofromscA,scBwhereA.sno=B.snoandA.cno='1'andB.cno='2'
二.使用带比较运算的子查询
4.查询比’刘晨’年龄小的所有学生的信息:
select*fromstudentwheresage<
(selectsagefromstudentwheresname='刘晨')
三.使用带Any,All谓词的子查询
5.查询其他系中比信息系(IS)某一学生年龄小的学生姓名和年龄;
selectsname,sagefromstudentwheresage (selectsagefromstudentwheresdept='IS') andsdept<>'IS' 6.查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄: selectsname,sagefromstudentwheresage (selectsagefromstudentwheresdept='IS') andsdept<>'IS' 7.查询与计算机系(CS)系所有学生的年龄均不同的学生学号,姓名和年龄: selectsno,sname,sagefromstudentwheresage<>all (selectsagefromstudentwheresdept='CS') 四.使用带Exists谓词的子查询和相关子查询 8.查询与其他所有学生年龄均不同的学生学号,姓名和年龄: selectsno,sname,sagefromstudentAwherenotexists (select*fromstudentBwhereA.sage=B.sageandA.sno<>B.sno) 9.查询所有选修了1号课程的学生姓名: selectsnamefromstudentwhereexists (select*fromscwheresno=student.snoandcno='1') 10.查询没有选修了1号课程的学生姓名: selectsnamefromstudentwherenotexists (select*fromscwheresno=student.snoandcno='1') 11.查询选修了全部课程的学生姓名: SQLServer中: selectsnamefromstudentwherenotexists (select*fromcoursewherenotexists (select*fromscwheresno=student.snoandcno=o)) 11.查询至少选修了学生95002选修的全部课程的学生的学号: SQLServer中: selectdistinctsnofromscAwherenotexists (select*fromscBwheresno='95002'andnotexists (select*fromscCwheresno=A.snoandcno=B.cno)) 12.求没有人选修的课程号cno和cnamecname: selectcno,cnamefromcourseCwherenotexists (select*fromscwhereo=C.cno) 13*.查询满足条件的(sno,cno)对,其中该学号的学生没有选修该课程号cno的课程 SQLServer中: selectsno,cnofromstudent,coursewherenotexists (select*fromscwherecno=oandsno=student.sno) 14*.查询每个学生的课程成绩最高的成绩信息(sno,cno,grade): select*fromscAwheregrade= (selectmax(grade)fromscwheresno=A.sno) 思考: 如何查询所有学生都选修了的课程的课程号cno? 五使用聚集函数: 1.查询学生总人数: SelectCount(*)as学生总数fromstudent 2.查询选修了课程的学生总数: selectcount(distinctsno)as选课学生总数fromsc 3.查询所有课程的总学分数和平均学分数,以及最高学分和最低学分: selects
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 原理 应用 实验 指导书