sqlServer习题与答案Word文档下载推荐.docx
- 文档编号:17450942
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:23
- 大小:22.73KB
sqlServer习题与答案Word文档下载推荐.docx
《sqlServer习题与答案Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《sqlServer习题与答案Word文档下载推荐.docx(23页珍藏版)》请在冰豆网上搜索。
5.求各个课程号及相应的选课人数。
(groupby)
6.查询选修了1门以上课程的学生学号。
(having)
7.请说明union的作用。
1.selectcount(*)fromstudent
2.selectcount(distinctsno)as人数fromsc
3.selectavg(score)as平均成绩fromscwherecno=1001
4.selectmax(score)as最高分数fromscwherecno=1
5.selectcno,count(*)as选课人数fromscgroupbycno
6.selectcno,count(*)as选课人数fromscgroupbycnohavingcount(*)>
1
4.查询选修1001号课程的学生最高分数。
1.selectcount(*)as总人数fromstudent>
2.selectcount(distinctsno)as总人数fromsc
>
3.selectavg(score)as平均成绩fromscwherecno=1001>
4.selectmax(score)fromscwherecno=1001
5.selectcno,count(*)as人数,max(score)fromscgroupbycno
6.selectsnofromscgroupbysnohavingcount(cno)>
7.在列数和列的顺序相同且数据类型相同的前提下,将多个select语句返回的结果组合到同一个结果当中。
请举例说明Withcube和Withrollup的作用。
selectcno,cname,count(cno)as人数fromcoursegroupbycno,cnamewithcube说明每一个分组统计的总数
selectcno,cname,count(cno)as人数fromcoursegroupbycno,cnamewithrollup说明每一个小分组的统计总数
3.使用compute汇总所有学生的成绩平均分。
selectsno,cno,scorefromsccomputeavg(score)统计所有内容,求出平均成绩
4.使用computeby汇总每个人的选修课程数。
select*fromscorderbysno,cnocomputecount(cno)bysno按SNO,CNO分组进行统计
使用ANSI连接和sqlserver连接两种方式完成:
1.查询每个学生的学号、姓名及其选修课程的课程号、成绩。
使用ANSI:
selectstudent.sno,sname,sc.sno,cnofromstudentinnerjoinsconstudent.sno=sc.sno
使用sqlserver:
selectstudent.sno,sname,sc.sno,cnofromstudent,scwherestudent.sno=sc.sno
2.查询出'
101'
号学生选修的课程的课程名称和学分
selectcname,ccreditfromcourseinnerjoinscono=o
selectcname,ccreditfromcourse,scwhereo=oandsc.sno='
查询出选修‘1002’号课程的学生的学号、姓名。
使用ANSIselectstudent.sno,snamefromstudentinnerjoinsconstudent.sno=sc.snoando='
1002'
selectstudent.sno,snamefromstudent,scwherestudent.sno=sc.snoando='
--查询与“name2”在同一个系学习的学生信息。
select*fromstudentwheresdeptin(selectsdeptfromstudentwheresname='
name2'
)andsname!
='
查男女各有多少人selectssex,count(*)as人数fromstudentgroupbyssex
按降序排列:
groupby是分组orderby是排序
selectssex,count(*)as人数fromstudentgroupbyssexorderbyssexdesc
选课多余2的人数selectcno,count(*)fromscgroupbycnohavingcount(*)>
'
2'
查询出‘101’号学生选修的课程的课程名称和学分。
使用sqlserver:
selectcname,ccreditfromsc,coursewheresno='
ando=o
使用ANSI:
selectcname,ccreditfromcourseinnerjoinscono=oandsno='
嵌套查询:
selectcname,ccreditfromcoursewherecnoin(selectcnofromscwheresno='
)
exists查询:
selectcname,ccreditfromcoursewhereexists(select*fromscwherecno=oandsno='
--查询选修课程号为“1001”的所有男生的姓名和该科成绩。
sqlserver:
selectsname,scorefromstudent,scwherestudent.sno=sc.snoando='
1001'
andstudent.ssex='
ANSI:
selectsname,scorefromstudentinnerjoinsconstudent.sno=sc.snoandstudent.ssex='
ando='
--查询出‘101’号学生选修的课程的学分总和。
--使用sqlserver:
selectsum(ccredit)as总学分fromsc,coursewheresc.sno='
ando=o
--使用ANSI:
selectsum(ccredit)as总学分fromcourseinnerjoinsconsc.sno='
--嵌套查询:
selectsum(ccredit)as总学分fromcoursewherecnoin(selectcnofromscwheresno='
--exists:
selectsum(ccredit)fromcoursewhereexists(selectcnofromscwheresno='
ando=o)
--查询出每个学生已经修过的总学分。
selectsno,sum(ccredit)as总学分fromsc,coursewhereo=ogroupbysc.snoorderbysum(ccredit)desc
selectsno,sum(ccredit)as总学分fromscinnerjoincourseono=ogroupbysc.sno
orderbysum(ccredit)desc
--查询出选修‘c语言’的学生的学号、姓名和成绩
selectstudent.sno,sname,score
fromstudent,sc,course
whereame='
c'
andstudent.sno=sc.sno
--查询出选修了学分是4的课程的学号
selecto,sno
fromsc,course
whereo=o
andcourse.ccredit='
4'
--查询出选修了学分是4的课程的姓名
selectcname,sno
--查询出没有选修学分是4的课程的学号
selectsno,o
andcourse.ccredit<
1.将一个新生记录(学号:
111;
姓名:
陈冬;
性别:
男;
所在系:
IS;
年龄:
18岁)插入到Student表中。
insertinto
student(sno,sname,ssex,sage,sdept)
values('
111'
'
陈冬'
18'
IS'
2.插入一条选课记录(sno:
'
,cno:
1111'
),新插入的记录在score列上将会取空值。
能插入吗?
存在外键则不能
3.student数据库中,有一个表Deptage(Sdept,Avgage)用来存放每个系的学生平均成绩,但还没有数据。
请你对每一个系求学生的平均年龄,并把结果存入表Deptage。
selectsdept,avg(sage)asavgage
intoavger
fromstudent
groupbysdept
任务2(update):
1.将学生'
的年龄改为19岁。
updatestudent
setsage='
19'
wheresno='
2.将所有学生的年龄增加1岁。
setsage=sage+1
3.将信息系全体学生的成绩置零。
updatesc
setscore=0
wheresnoin(
selectsnofromstudent
wheresdept='
信息'
任务3(delete):
1.删除学号为'
102'
的学生选课记录。
deletesc
2.删除所有的学生选课记录。
deletesc
3.删除信息系所有学生的选课记录。
wheresnoin(
selectsno
)
4.你能删除student表中学号为'
的学生记录吗?
问什么?
不能删除
DELETE语句与REFERENCE约束"
FK_sc_student1"
冲突。
该冲突发生于数据库"
students"
,表"
dbo.sc"
column'
sno'
。
任务1:
回答以下问题:
1.什么是视图
2.使用视图的优点
3.创建视图的注意事项
步骤1:
每个人独立完成,15分钟。
步骤2:
提问学生任务完成情况,5分钟
步骤3:
教师补充点评,5分钟。
任务2:
创建一个视图view1,查询选修课程号为"
1001"
的所有女生的姓名和该科成绩。
createviewview1
as
selectsname,scorefromstudentinnerjoinsconstudent.sno=sc.sno
wheressex='
女'
andcno='
任务3:
完成以下操作:
创建一个视图v1,在视图中包含sc表中及格的选课信息。
createviewv1
asselect*fromsc
wherescore>
=60
1.插入数据记录:
将这两条记录插入视图v1中('
105'
69)('
50)。
观察视图和表的记录变化。
insertintov1
values('
69)
50)
2.修改数据记录:
修改视图v1,将('
69)的成绩改为99
UPDATEv1
setscore='
99'
wheresno='
3.删除数据记录:
修改视图v1,删除105号学生的选课记录。
deletefromv1
每个人独立完成,5分钟。
与你的同组搭档得出一个小组结果,5分钟。
学生介绍任务完成情况,3分钟。
步骤4:
教师补充点评,2分钟。
任务4:
创建视图v1,
createviewv1as
selectstudent.sno,sname,cno,score
fromstudentinnerjoinsc
onstudent.sno=sc.sno
完成:
1.将满足sno='
1003'
的记录sname改为n1.
UPDATEv2
setsname='
n1'
2.将满足sno='
1004'
的记录sname改为n1,cno改为1002.
不行
3.将sno='
109'
sname='
name9'
的记录插入v1.
4.将sno='
110'
nam10'
score=99的记录插入v1,能够正确执行吗?
5.删除sno='
的记录,可以吗?
不可以,因为关系到多个基表
通过学习教材及课件回答以下问题:
1.标识符命名规则
答:
标识符分为标准标识符和分隔标识符两大类
2.如何注释
3.局部变量的声明、赋值方式
4.全局变量的特点,使用。
5.声明一个变量x并为x赋值,查询成绩大于x(x是局部变量)的学生学号和选修的课程号.
declare@xint
set@x=90
selectsno,cno,scorefromscwherescore>
=@x
6.声明一个变量avgscore,并将sc表中成绩的平均分赋值给变量avgscore。
你有几种赋值方式?
declare@avgscoreint
set@avgscore=(selectavg(score)fromsc)
select@avgscore平均成绩
go
declare@xint
set@x=60
selectsno,cnofromscwherescore>
@x
declare@avgscoreint
print@avgscore
select@avgscore=avg(score)fromsc
--no是student表的identity列
insertintostudent(sno,sname)values('
name10'
select@@identityidentity1
select*fromstudentwheresno='
orsno='
select@@rowcount
usestudents
select@@trancountastrancount1,@@servernameservername1,@@versionversion1,@@identityidentity1,@@languagelanguage1
1.(if)如果sc表中所有学生的平均成绩大于80,显示'
成绩优异'
,并显示出成绩大于80的选课记录。
否则显示学生成绩一般。
set@avgscore=(selectavg(score)fromscwherescoreisnotNULL)
if@avgscore>
=80
begin
print'
select*fromscwherescore>
=80
end
else
成绩一般'
--在student中查询学生信息,如果存在此生,则将学生的信息输出;
如果不存在此生,则输出数据库中没有该生信息
declare@namenchar(8)
set@name='
name1'
ifexists(select*fromstudentwheresname=@name)
begin
print'
select*fromstudentwheresname=@name
end
else
print'
数据库中没有该生信息。
--3.(while)使用while循环,对course表中的学分总和进行检查,
--若学分总和小于50,对每门课程学分加1,直到学分总和不小于50为止。
while((selectsum(ccredit)fromcourse)<
50)
updatecourse
setccredit=ccredit+1
if(selectsum(ccredit)fromcourse)>
=50
break
//使用while循环,对course表中的学分总和进行检查,若学分总和小于50,
//对每门课程学分加1,直到学分总和不小于50为止。
declare@sumcint
set@sumc=(selectsum(ccredit)fromcourse)
WHILE@sumc<
50
BEGIN
UPDATEcourse
SETccredit=ccredit+1
set@sumc=(selectsum(ccredit)fromcourse)
END
WHILE(SELECTsum(ccredit)FROMcourse)<
50
--4.(trycatch)在try中删除
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- sqlServer 习题 答案