数据库笔试题1.docx
- 文档编号:15345859
- 上传时间:2023-07-03
- 格式:DOCX
- 页数:10
- 大小:17.14KB
数据库笔试题1.docx
《数据库笔试题1.docx》由会员分享,可在线阅读,更多相关《数据库笔试题1.docx(10页珍藏版)》请在冰点文库上搜索。
数据库笔试题1
数据库笔试题
题一:
新建学生-课程数据库三个表:
学生表:
Student(Sno,Sname,Ssex,Sage,Sdept)Sno为主码;
课程表:
Course(Cno,Cname,Cpno,Credeit)Cno为主码;
学生选修表:
SC(Sno,Cno,Grade)Sno,Cno,为主码;
Student
学号
Sno姓名
Sname性别
Ssex年龄
Sage所在系
Sdept
95001李勇男20CS
95002刘晨女19IS
95003王敏女18MA
95004张立男19IS
学号
Sno课程名
Cname先行课
Cpno学分
Credit
1数据库54
2数学2
3信息系统14
4操作系统63
5数据构造74
6数据解决2
7Pascal语言64
Course:
SC:
学号
Sno课程号
Cno成绩
Grade
95001192
95001285
95001388
95002290
95002380
一:
查询表中列和行
1:
查询全体学生学与姓名
selesno,snamefromstudent
2:
查询全体学生姓名、学号、所在系。
selesno,sname,sdeptfromstudent
3:
查询全体学生详细记录
sele*fromstudent
4:
查询全体学生姓名及出生年份
selesno,sagefromstudent
5:
查询全体学生姓名,出生年份及所在系,要用小写字母表达系名
6:
查询选修了课程学生学号
selesno,cnofromsc
7:
查询选修了课程学生姓名
seledistinctsnamefromstudent,scwherestudent.sno=sc.sno
二:
条件查询:
惯用查询条件
查询条件谓词
比较=,<,>,>=,<=,!
=,<>,!
>,!
<;
not+上述比较运算符
拟定范畴Betweenand,NotbetweenAnd,
拟定集合IN,notIN
字符匹配Like,NotLike
空值IsNull,ISNOTNULL
多重条件AND,OR
1:
查询计算机系全体学生姓名
selesnamefromstudentwheresdept=”CS”
2:
查询所有年龄在20岁如下学生姓名及其年龄
selesname,sagefromstudentwheresage<20
3:
查询考试成绩有不及格学生学号
selestudent.snofromstudent,scwherestudent.sno=sc.snoandgrade<60
4:
查询年龄在20到23间学生姓名,系别及年龄
selesname,sdept,sagefromstudentwheresagebetween20and23
5:
查询年龄不在20到23间学生姓名,系别及年龄
selesname,sdept,sagefromstudentwheresagenotbetween20and23
6:
查询信息系(IS),数学系(MA)和计算机系(CS)学生姓名和性别
selesname,ssexfromstudentwheresdeptin("IS","MA","CS")
7:
查询不是信息系(IS),数学系(MA)和计算机系(CS)学生姓名和性别
selesname,ssexfromstudentwheresdeptnotin("IS","MA","CS")
8:
查询学号为”95001”学生详细状况
sele*fromstudentwheresno=95001
9:
查询所有姓刘学生姓名,学号和性别(wherenamelike‘刘%’)
selesname,sno,ssexfromstudentwheresnamelike'刘%'
10:
查询姓”欧阳”且命名为三个中文学生姓名
selesnamefromstudentwheresnamelike'欧阳_'
11:
查询名字中第2个字为”阳”字学生姓名和学号(wheresnamelike‘__阳%’)
selesname,snofromstudentwheresnamelike'__阳%'
12:
查询所有不姓刘学生姓名
selesnamefromstudentwheresnamenotlike'刘%'
13:
查询DB_Design课程课程号和学分(wherecnamelike‘Db\_Design’Escape’\’)
selecno,greditfromcoursewherecnamelike‘Db\_Design’Escape’\’
14:
查询以”DB_”开头,且倒数第3个字符为i课程详细状况(wherecnamelike‘DB\_%i__’escape’\’)
‘DB\_%i__’escape’\’)selecno,greditfromcoursewherecnamelike‘Db\_%i__’escape’\’
15:
查询缺少成绩学生学号和相应课程号
selestudent.sno,cnofromstudent,scwheregradeisnull
16:
查询所有成绩学生学号和课程号(wheregradeisnotnull)
selestudent.sno,cnofromstudent,scwheregradeisnotnull
17:
查询计算机系年龄在20岁如下学生姓名
selesnamefromstudentwheresdept=”CS”andsage<20
18:
查询选修了3号课程学生学号及其成绩,分数降序排列
selestudent.sno,gradefromstudent,scwherestudent.sno=sc.snoando=3orderbygradedesc
19:
查询全体学生状况,成果按所在系号升序排列,同一系中学生按年龄降序
sele*fromstudentorderbysdept,sagedesc
三:
使用集函数
count,sum,avg,max,min
1:
查询学生总人数
2:
查询选修了课程学生人数(selectcount(distinctsno))
3:
计算1号课程学生平均成绩
4:
查询选修1号课程学生最高分数
5:
求各个课程号及相应选课人数(selsectcno,count(sno);fromsc;groupbycno)
6:
查询选修了3门以上课程学生学号
selectsno
fromsc
groupbysno
havingcount(*)>3
四:
连接查询:
<1>等值与非等值连接查询
在连接查询中用来连接两个有条件称为连接条件或连接谓词,,当连接运算符号为”=”时,称为等值连接,使用如,=,<,>,<=,>=,!
=连接时称非等值连接
1:
查询每个学生及其选修课程状况
selectstudent.*,sc.*
fromstudent,sc
wherestudent.sno=sc.sno
<2>自身连接
连接操作在同一种表中进行连接查询
2:
查询每一门课间接先修课(即先修课先修课)
selectfirst.cno,o
fromcoursefirst,coursesecond
whereo=o
五:
复合条件连接
1:
查询选修2号课程且成绩在90分以上所有学生。
Selectstudent,sname
formstudent,sc
Wherestudent.sno=sc.snoAnd
So=’2’andsc.grade>90
六:
嵌套查询
1:
带有谓词in子查询
<1>查询与“刘晨”在同一种系学习学生
selectsno,sname,sdept
fromstudent
wheresdeptin(
selectsdept
fromstudent
wheresname=”刘晨”)
或:
selects1.sname,s1.sdept
fromstudents1,students2
wheres1.dept=s2.deptands2.name=”刘晨”
<2>查询选修了课程名为“信息系统”学生学号和姓名
selectsno,sname
fromstudent
wheresnoin
(selectsno
fromsc
wherecnoin
(selectcno
fromcourse
wherecname-“信息系统”)
或:
selectsno,sname
fromstudent,sc,course
wherestudent.sno=sc.snoand
o=oand
ame=’信息系统’)
2:
带有Any或all谓词子查询
<1>查询其她系中比信息系中某一学生年龄小学生姓名和年龄
selectsname,sage
fromstudent
wheresage fromstudent wheresdept=’is’ andsdept<>’is’ 或用集函数: selectsname,sage fromstudent wheresage< (selectmax(sage) fromstudent wheresdept=’is’) andsdept<>’is’ <2>查询其她系中比信息系所有学生年龄都小学生姓名及年龄 selectsname,sage fromstudent wheresage (selectsage fromstudent wheresdept=’is’) andsdept<>’is’ 3带有Exitst谓词子查询 <1>查询所有选修了1号课程学生姓名 selectsname fromstudent whereexists (select* fromsc wheresno=student.snoandcno=’1’) <2>查询没有选修1号课程学生姓名 selectsname formstudent wherenotexists (select* formsc wheresno=stuedent.snoandcno=’1’) <2>查询选修所有所有课程学生姓名 selectsname fromstudent wherenotexists (select* fromcourse wherenotexists (select* fromsc wheresno=student.sno andcno=o) <3>查询到少选修了学生95002选修所有课程学生号码 selectdistinctsno fromscscx wherenotexists (select* fromscscy wherescy.sno=’95002’and notexists (select* fromscscz wherescz.sno=scx.snoand o=o)[/color] [color=purple][/color] [color=purple][/color][/size] [size=4][color=purple]二: 题一: 表A数据如下: FYearFNum 1 2 3 4 5 6 按如下格式显示: 年度 汇总 6 15 方案一: createtable表名(FIDvarchar(10),Field1varchar(100)) go insertinto表名select1,'A' insertinto表名select1,'B' insertinto表名select1,'C' insertinto表名select2,'D' insertinto表名select2,'E' insertinto表名select2,'F' go --创立一种合并函数 createfunctionf_merge(@namevarchar(100)) returnsvarchar(8000) as begin declare@strvarchar(8000) set@str='' select@str=@str+','+cast(Field1asvarchar(100))from表名whereFID=@name set@str=stuff(@str,1,1,'') return(@str) End go --select*from表名 --调用自定义函数得到成果: selectFID,dbo.f_merge(FID)astelfrom表名groupbyFID droptable表名 dropfunctionf_merge 方案二: select'汇总'as 年度 [],[] from (selectfyear,fnumfromT)assourceTable pivot ( sum(fnum) forfyearin([],[]) ) aspivotTable 回头发现可以用SQLpivot办法很简朴 题二: 表A数据如下: FID Field1 1 A 1 B 1 C 2 D 2 E 2 F 规定按如下格式显示: FID Field1 1 A,B,C 2 D,E,F 如何做到? createtable表名(FIDvarchar(10),Field1varchar(100)) go insertinto表名select1,'A' insertinto表名select1,'B' insertinto表名select1,'C' insertinto表名select2,'D' insertinto表名select2,'E' insertinto表名select2,'F' go --创立一种合并函数 createfunctionf_merge(@namevarchar(100)) returnsvarchar(8000) as begin declare@strvarchar(8000) set@str='' select@str=@str+','+cast(Field1asvarchar(100))from表名whereFID=@name set@str=stuff(@str,1,1,'') return(@str) End go --select*from表名 --调用自定义函数得到成果: selectFID,dbo.f_merge(FID)astelfrom表名groupbyFID droptable表名 dropfunctionf_merge
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 笔试