数据库原理与应用实验资料课后习题整理.docx
- 文档编号:29858591
- 上传时间:2023-07-27
- 格式:DOCX
- 页数:16
- 大小:223.36KB
数据库原理与应用实验资料课后习题整理.docx
《数据库原理与应用实验资料课后习题整理.docx》由会员分享,可在线阅读,更多相关《数据库原理与应用实验资料课后习题整理.docx(16页珍藏版)》请在冰豆网上搜索。
数据库原理与应用实验资料课后习题整理
/*createdatabase学生选课
onprimary
(
name=学生选课,
filename='C:
\ProgramFiles\MicrosoftSQLServer\学生选课_data.mdf',
size=4MB,
maxsize=10MB,
filegrowth=1MB
)
logon
(
name=学生选课_log,
filename='C:
\ProgramFiles\MicrosoftSQLServer\学生选课_log.ldf',
size=1MB,
maxsize=6MB,
filegrowth=1%
)*/
alterdatabase学生选课
addfile
(
name=xs_data,
filename='C:
\ProgramFiles\MicrosoftSQLServer\xs_data.mdf',
size=3
)
alterdatabase学生选课
addlogfile
(
name=xs_log,
filename='C:
\ProgramFiles\MicrosoftSQLServer\xs_log.mdf',
filegrowth=10%
)
alterdatabase学生选课
modifyfile
(
name=xs_log,
size=3,
maxsize=5
)
创建代码
CreateTablestudent
(
snochar(8)primarykey,
snamechar(8)notnull,
ageint,
sexchar
(2),
deptvarchar(50)
);
Createtablecourse
(
cnochar(4)primarykey,
cnamechar(20)notnull,
creditfloat,
pcnovarchar(20),
describevarchar(100)
);
CreatetableSC
(
snochar(8),
cnochar(4),
gradefloat,
primarykey(sno,cno),
foreignkey(sno)REFERENCESstudent(sno),
foreignkey(cno)REFERENCEScourse(cno)
);
0101
计算机基础
5
0101
可自学
0102
C++程序设计
4
NULL
可自学
0206
离散数学
4
0102
可自学
0208
数据结构
4
0101
可自学
0209
操作系统
4
0101
可自学
0210
微机原理
5
0101
可自学
0211
图形学
3
0102
可自学
0212
数据库原理
4
0102
可自学
0301
计算机网络
3
0102
可自学
0302
软件工程
3
0102
可自学
NULL
NULL
NULL
NULL
NULL
09001101
0101
68
09001101
0206
76
09001103
0101
62
09001106
0209
75
09001106
0210
77
09001106
0212
75
09001106
0301
87
09001108
0101
68
09001109
0212
88
09001109
0302
76
09001110
0101
66
NULL
NULL
NULL
09001101
张琳
18
男
计算机系
09001102
程明
18
男
计算机系
09001103
王艳
19
女
计算机系
09001104
严萍萍
20
女
计算机系
09001105
王红敏
19
女
信息管理系
09001106
孙祥新
18
男
信息管理系
09001107
吕占英
19
女
信息管理系
09001108
李义
19
男
机械工程系
09001109
牟万里
18
男
机械工程系
09001110
刘丽霞
20
女
机械工程系
09001121
吕淑霞
19
女
计算机系
NULL
NULL
NULL
NULL
NULL
Insertintostudent(sno,sname,age,sex,dept)
Values('09001121','吕淑霞',19,'女','计算机系')
Insertintosc(sno,cno,grade)values('09001101','0210','95')
Deletefromscwheresno='09001101'andcno='0210'
数据表的修改代码
use学生选课
altertablestudent
addconstraintagecheck(age>15)
use学生选课
altertablestudent
addclassvarchar(50)
use学生选课
altertablestudent
altercolumnclassvarchar(20)
在已经建好的数据库上实现数据的查询,与替换,以及更新
查询数学系的学生的学号与姓名
视图的建立:
CREATEVIEWIS_S
AS
SELECTSNO,SNAME,AGE
FROMS
WHERESDEPT=’IS’;
createviewview_stu_grade
asselectstudent.sno,sname,cname,grade
fromstudent,sc,course
wherestudent.sno=sc.snoando=o
视图的建立代码1
CREATEVIEWstudent2
as(注释:
这里的“student2”只是一个名字而已,并不牵扯
到数据库中的内容,此名字为操作者自行命名的。
SELECTsno,sname不过在建立的时候名字不能够重复,否则会出错。
)
fromstudent
wheredept='计算机系'
实验总结:
在建立视图时记得需要选中需要选择的数据库,否则系统将会默认主机自带的数据,
可以先点击你要建的的数据库然后新建查询,或者直接在你想建视图的数据库的地方右击—》新建查询。
还有记得在命令结尾不要加分号。
createviewstudent2
asselectsno,sname,age
fromstudent
wheredept='信息管理系'
视图2
withcheckoptioncreateviewsc1(sno,sname,grade)
asselectstudent.sno,sname,grade
fromstudent,sc
wherestudent.dept='计算机系'andstudent.sno=sc.snoando='09001101'
有个问题:
就是在建设完视图后,视图命令成功完成,可是在查找的时候视图是空的。
如下图:
对数据的查询
1
select*
fromstudent
2
selectsname,'yearofbirth:
',2009-age,lower(dept)
fromstudent
3
selectsname,'yearofbirth:
'birth,2009-agebirthday,
lower(dept)department
fromstudent
4
selectdistinctsno注释distinct是限定条件的意思,就是把重复的东西只是显示一次。
fromsc;
5
selectsname,age
fromstudent
whereage<20
6
selectdistinctcno
fromsc
wheregrade<90
7
selectsname,dept,age
fromstudent
whereagebetween20and23
selectsname,dept,age
fromstudent
whereagenotbetween20and23
8
selectsname,sex
fromstudent
wheredeptin('计算机系','信息管理系','机械工程系')
selectsname,sex
fromstudent
wheredeptnotin('计算机系','机械工程系')
9
selectsname,sno,sex
fromstudent
wheresnamelike'王%'注释:
模糊查询
实验四
/*查询数学系的学生的学号与姓名*/
selectsno,sname
fromstudent
wheredept='数学系'
/*查询选修了课程的学生的学号和姓名*/
selectdistinctsno
fromsc
/*查询选修课程号为的学生的学号和成绩,
并对查询结果按成绩降序排列
如果成绩相同则按学号升序排列。
*/
selectsno,grade
fromsc
wherecno='0101'
orderbygradedesc
/*查询选修课程号为的成绩在到之间的学生学号和成绩,
并将成绩按其.8输出。
*/
selectstudent.sname,sc.sno,grade,0.8*grade
fromsc,student
where(cno='0212')and(gradebetween80and90)andstudent.sno=sc.sno
/*查询数学系和计算机系姓张的学生的信息。
*/
selectsname,sno,sex
fromstudent
wheresnamelike'张%'
索引的建立:
代码
use学生选课
createindexIX_dept1onstudent(dept)注释:
(IX_dept1中的IX为程序索引标示dept1
为新建索引的名字,为操作者自行命的。
)
代码删除普通索引时:
use学生选课
DROPindexIX_Deptonstudent(记住一定要记得注明所删除索引的表的位置,如本例
IX_Dept在student内)
删除主键(索引)代码格式;
Usedatabasename
Altertabletable_name
DropindexPK_primeaykey_1(index_name)
查询索引:
use学生选课
execsp_helpindexstudent注释:
(student还是为文件名!
)
查询学生总数:
selectcount(*)
fromstudent
/*查询选修了课程的学生人数*/
selectcount(distinctsno)
fromsc
计算选修0101号课程学生的平均成绩
selectavg(grade)
fromsc注释:
这些均是某些函数的应用
wherecno='0101'
/*计算选修0101号课程学生的最高成绩*/
selectmax(grade)
fromsc
wherecno='0101'
/*查询学生选修课程的总学分数*/
selectsum(credit)
fromsc,course
wheresno='09001106'ando=o
/*求个课程号及相应的选课人数*/
selectcno,count(sno)
fromsc
groupbycno
查询选修了门课程以上的学生学号*/
selectsno
fromsc
groupbysno
havingcount(*)>3
/*查询每个学生及选修课程的情况*/
selectstudent.*,sc.*
fromsc,student
wherestudent.sno=sc.sno
给数据表改名程序代码:
use学生选课
execsp_rename'student','s'
创建次文件组(目的是为以后扩充数据库做准备)
use学生选课
alterdatabase学生选课
addfilegroupfg1
此代码执行后再次新建查询如下:
use学生选课
alterdatabase学生选课/*(fg1为程序员自己命得名)*/
addfile
(name=学生选课_sec,
filename='D:
\学生选课_sec.ndf',
size=5,
maxsize=20,
filegrowth=10%
)
tofilegroupfg1
查询
use学生选课
selectsno,cno,grade/*sno,cno,grade为所要查询表里的数据列,操作者需要显示什么就把那列写上)*/
fromsc
wheregrade<='80'
2^^^^^^^^^^^^^^^^^^^^
use学生选课
selectsno,cno,grade/*sno,cno,grade为所要查询表里的数据列,操作者需要显示什么就把那列写上)*/
fromsc
wheregrade<='80'andgrade<>'75'/*(这里如果是交集那么应该如是写)*/
Isnull的用法
use学生选课
selectcno,cname,pcno
fromcourse
wherepcnoisnotnull
数据库select语句最后orderby语句
use学生选课
select*
froms
orderbysnamedesc/*(desc为降序排列,asc为升序排列)*/
高级查询程序代码:
/*游标的应用*/
use学生选课
declarecs_cursorscrollcursor
for
selectsno,sname,age,sex,dept
fromstudent
forreadonly
opencs_cursor
fetchfromcs_cursor
/*游标的应用*/
use学生选课
declarecs_cursorscrollcursor/*cursor是光标,游标指针的意思,scroll是屏幕滚动的意思*/
for
selectsno,sname,age,sex,dept
fromstudent
forreadonly
opencs_cursor
fetchfromcs_cursor
closecs_cursor
deallocatecs_cursor
存储过程程序代码:
/*createprocedurestudent_grade
as
selectsno,sname,cname,grade
fromstudent,course,sc
wherestudent.sno=sc.snoando=o*/
createprocedureproc_exp@ssnochar(20)
as
declare@stud_avg(grade)fromsc
whereo=@ssno
alterprocedureproc_exp@ssnochar(20)
as
declare@savgint
select@savg=avg(grade)fromsc
wheresc.sno=@ssno
if@savg>60
print'此学生综合成绩合格,成绩为'+convert(char
(2),@savg)+'分'
else
print'此学生综合成绩不合格,成绩为'+convert(char
(2),@savg)+'分'
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 原理 应用 实验 资料 课后 习题 整理