sqlServer习题与答案.docx

上传人:b****9 文档编号:28770155 上传时间:2023-07-19 格式:DOCX 页数:28 大小:22.67KB
下载 相关 举报
sqlServer习题与答案.docx_第1页
第1页 / 共28页
sqlServer习题与答案.docx_第2页
第2页 / 共28页
sqlServer习题与答案.docx_第3页
第3页 / 共28页
sqlServer习题与答案.docx_第4页
第4页 / 共28页
sqlServer习题与答案.docx_第5页
第5页 / 共28页
点击查看更多>>
下载资源
资源描述

sqlServer习题与答案.docx

《sqlServer习题与答案.docx》由会员分享,可在线阅读,更多相关《sqlServer习题与答案.docx(28页珍藏版)》请在冰豆网上搜索。

sqlServer习题与答案.docx

sqlServer习题与答案

从学生表Student(Sno,Sname,Ssex,Sage,Sdept)中查询出全体学生的学号与姓名

1.查询全体学生的详细记录

2.显示前5条纪录

3.显示前50%条纪录

4.查询所有年龄在17岁以下的学生姓名及其年龄。

5.某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。

查询缺少成绩的学生的学号和相应的课程号。

(成绩为null)

6.查所有有成绩的学生学号和课程号

7.查询学生的所有信息,按学号的降序排列

1.select*fromstudent

2.selecttop5*fromstudent

3.selecttop50percent*fromstudent

4.selectsname,sagefromstudentwheresage<17

5.selectsno,cnofromscwherescoreisNULL

6.selectsno,cnofromscwherescoreisnotNULL

7.select*fromstudentorderbysnodesc

8查询选修了课程的学生学号

9.查全体学生的姓名及其出生年份,显示两列:

姓名、出生年份

10.查询年龄在15~17岁(包括15岁和17岁)之间的学生的姓名、年龄。

11.查询年龄不在15~17岁之间的学生姓名、系别和年龄。

12.查询年龄不在15~17岁之间的男生姓名、系别和年龄。

13.将上题查询的结果插入一个新表中。

8.selectdistinctsnofromsc

9.selectsname,2010-sageas出生年份fromstudent

10.selectsname,sagefromstudentwheresagebetween15and17

11.selectsname,sdept,sagefromstudentwheresageisnotbetween15and17

12.selectsname,sdept,sagefromstudentwheressex='男'andsageisnotbetween15and17

13.selectsname,sdept,sageintonewtablefromstudentwheressex='男'andsageisnotbetween15and17

1.查询学生总人数。

2.查询选修了课程的学生人数。

3.计算1001号课程的学生平均成绩。

4.查询选修1号课程的学生最高分数。

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

1.查询学生总人数。

2.查询选修了课程的学生人数。

3.计算1001号课程的学生平均成绩。

4.查询选修1001号课程的学生最高分数。

5.求各个课程号及相应的选课人数。

(groupby)

6.查询选修了1门以上课程的学生学号。

(having)

7.请说明union的作用。

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)>1

>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'号学生选修的课程的课程名称和学分

使用ANSI:

selectcname,ccreditfromcourseinnerjoinscono=o

使用sqlserver:

selectcname,ccreditfromcourse,scwhereo=oandsc.sno='101'

查询出选修‘1002’号课程的学生的学号、姓名。

使用ANSIselectstudent.sno,snamefromstudentinnerjoinsconstudent.sno=sc.snoando='1002'

使用sqlserver:

selectstudent.sno,snamefromstudent,scwherestudent.sno=sc.snoando='1002'

--查询与“name2”在同一个系学习的学生信息。

select*fromstudentwheresdeptin(selectsdeptfromstudentwheresname='name2')andsname!

='name2'

查男女各有多少人selectssex,count(*)as人数fromstudentgroupbyssex

按降序排列:

groupby是分组orderby是排序

selectssex,count(*)as人数fromstudentgroupbyssexorderbyssexdesc

选课多余2的人数selectcno,count(*)fromscgroupbycnohavingcount(*)>'2'

查询出‘101’号学生选修的课程的课程名称和学分。

使用sqlserver:

selectcname,ccreditfromsc,coursewheresno='101'ando=o

使用ANSI:

selectcname,ccreditfromcourseinnerjoinscono=oandsno='101'

嵌套查询:

selectcname,ccreditfromcoursewherecnoin(selectcnofromscwheresno='101')

exists查询:

selectcname,ccreditfromcoursewhereexists(select*fromscwherecno=oandsno='101')

--查询选修课程号为“1001”的所有男生的姓名和该科成绩。

sqlserver:

selectsname,scorefromstudent,scwherestudent.sno=sc.snoando='1001'andstudent.ssex='男'

ANSI:

selectsname,scorefromstudentinnerjoinsconstudent.sno=sc.snoandstudent.ssex='男'ando='1001'

--查询出‘101’号学生选修的课程的学分总和。

--使用sqlserver:

selectsum(ccredit)as总学分fromsc,coursewheresc.sno='101'ando=o

--使用ANSI:

selectsum(ccredit)as总学分fromcourseinnerjoinsconsc.sno='101'ando=o

--嵌套查询:

selectsum(ccredit)as总学分fromcoursewherecnoin(selectcnofromscwheresno='101')

--exists:

selectsum(ccredit)fromcoursewhereexists(selectcnofromscwheresno='101'ando=o)

--查询出每个学生已经修过的总学分。

sqlserver:

selectsno,sum(ccredit)as总学分fromsc,coursewhereo=ogroupbysc.snoorderbysum(ccredit)desc

--使用ANSI:

selectsno,sum(ccredit)as总学分fromscinnerjoincourseono=ogroupbysc.sno

orderbysum(ccredit)desc

--查询出选修‘c语言’的学生的学号、姓名和成绩

--使用sqlserver:

selectstudent.sno,sname,score

fromstudent,sc,course

whereame='c'

andstudent.sno=sc.sno

ando=o

--查询出选修了学分是4的课程的学号

--使用sqlserver:

selecto,sno

fromsc,course

whereo=o

andcourse.ccredit='4'

--查询出选修了学分是4的课程的姓名

--使用sqlserver:

selectcname,sno

fromsc,course

whereo=o

andcourse.ccredit='4'

--查询出没有选修学分是4的课程的学号

--使用sqlserver:

selectsno,o

fromsc,course

whereo=o

andcourse.ccredit<>'4'

 

1.将一个新生记录(学号:

111;姓名:

陈冬;性别:

男;所在系:

IS;年龄:

18岁)插入到Student表中。

insertinto

student(sno,sname,ssex,sage,sdept)

values('111','陈冬','男','18','IS')

2.插入一条选课记录(sno:

'111',cno:

'1111'),新插入的记录在score列上将会取空值。

能插入吗?

存在外键则不能

3.student数据库中,有一个表Deptage(Sdept,Avgage)用来存放每个系的学生平均成绩,但还没有数据。

请你对每一个系求学生的平均年龄,并把结果存入表Deptage。

selectsdept,avg(sage)asavgage

intoavger

fromstudent

groupbysdept

 

任务2(update):

1.将学生'101'的年龄改为19岁。

updatestudent

setsage='19'

wheresno='101'

2.将所有学生的年龄增加1岁。

updatestudent

setsage=sage+1

3.将信息系全体学生的成绩置零。

updatesc

setscore=0

wheresnoin(

selectsnofromstudent

wheresdept='信息')

任务3(delete):

1.删除学号为'102'的学生选课记录。

deletesc

wheresno='102'

2.删除所有的学生选课记录。

deletesc

3.删除信息系所有学生的选课记录。

deletesc

wheresnoin(

selectsno

fromstudent

wheresdept='信息')

4.你能删除student表中学号为'101'的学生记录吗?

问什么?

不能删除

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='1001'

 

任务3:

完成以下操作:

创建一个视图v1,在视图中包含sc表中及格的选课信息。

createviewv1

asselect*fromsc

wherescore>=60

1.插入数据记录:

将这两条记录插入视图v1中('105','1001',69)('105','1002',50)。

观察视图和表的记录变化。

insertintov1

values('105','1001',69)

insertintov1

values('105','1002',50)

2.修改数据记录:

修改视图v1,将('105','1001',69)的成绩改为99

UPDATEv1

setscore='99'

wheresno='105'andcno='1001'。

3.删除数据记录:

修改视图v1,删除105号学生的选课记录。

deletefromv1

wheresno='105'

步骤1:

每个人独立完成,5分钟。

步骤2:

与你的同组搭档得出一个小组结果,5分钟。

步骤3:

学生介绍任务完成情况,3分钟。

步骤4:

教师补充点评,2分钟。

任务4:

创建视图v1,

createviewv1as

selectstudent.sno,sname,cno,score

fromstudentinnerjoinsc

onstudent.sno=sc.sno

完成:

1.将满足sno='101'andcno='1003'的记录sname改为n1.

UPDATEv2

setsname='n1'

wheresno='101'andcno='1003'

2.将满足sno='101'andcno='1004'的记录sname改为n1,cno改为1002.

不行

3.将sno='109',sname='name9'的记录插入v1.

4.将sno='110',sname='nam10',score=99的记录插入v1,能够正确执行吗?

5.删除sno='101'andcno='1004'的记录,可以吗?

不可以,因为关系到多个基表

任务1:

通过学习教材及课件回答以下问题:

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

5.声明一个变量x并为x赋值,查询成绩大于x(x是局部变量)的学生学号和选修的课程号.

declare@xint

set@x=60

selectsno,cnofromscwherescore>@x

6.声明一个变量avgscore,并将sc表中成绩的平均分赋值给变量avgscore。

你有几种赋值方式?

declare@avgscoreint

set@avgscore=(selectavg(score)fromsc)

print@avgscore

select@avgscore=avg(score)fromsc

print@avgscore

>>--no是student表的identity列

>>insertintostudent(sno,sname)values('110','name10')

>>select@@identityidentity1

>>go

>>

>>

>>select*fromstudentwheresno='101'orsno='102'

>>go

>>select@@rowcount

>>go

>>

>>

>>

>>usestudents

>>go

>>select@@trancountastrancount1,@@servernameservername1,@@versionversion1,@@identityidentity1,@@languagelanguage1

>>go

>>--no是student表的identity列

>>insertintostudent(sno,sname)values('110','name10')

>>select@@identityidentity1

1.(if)如果sc表中所有学生的平均成绩大于80,显示'成绩优异',并显示出成绩大于80的选课记录。

否则显示学生成绩一般。

declare@avgscoreint

set@avgscore=(selectavg(score)fromscwherescoreisnotNULL)

if@avgscore>=80

begin

print'成绩优异'

select*fromscwherescore>=80

end

else

begin

print'成绩一般'

end

--在student中查询学生信息,如果存在此生,则将学生的信息输出;如果不存在此生,则输出数据库中没有该生信息

declare@namenchar(8)

set@name='name1'

ifexists(select*fromstudentwheresname=@name)

begin

print''

select*fromstudentwheresname=@name

end

else

begin

print'数据库中没有该生信息。

'

end

--3.(while)使用while循环,对course表中的学分总和进行检查,

--若学分总和小于50,对每门课程学分加1,直到学分总和不小于50为止。

while((selectsum(ccredit)fromcourse)<50)

begin

updatecourse

setccredit=ccredit+1

if(selectsum(ccredit)fromcourse)>=50

break

end

 

//使用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

BEGIN

UPDATEcourse

SETccredit=ccredit+1

END

--4.(trycatch)在try中删除

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 企业管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1