sql练习题 答案.docx

上传人:b****2 文档编号:50629 上传时间:2022-10-01 格式:DOCX 页数:12 大小:15.29KB
下载 相关 举报
sql练习题 答案.docx_第1页
第1页 / 共12页
sql练习题 答案.docx_第2页
第2页 / 共12页
sql练习题 答案.docx_第3页
第3页 / 共12页
sql练习题 答案.docx_第4页
第4页 / 共12页
sql练习题 答案.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

sql练习题 答案.docx

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

sql练习题 答案.docx

sql练习题答案

(一)新建以下几个表

student(学生表):

sno

sname

sex

dept

birth

age

其中约束如下:

(1)学号不能存在相同的

(2)名字为非空

(3)性别的值只能是’男’或’女’

(4)系包括这几个:

信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系

(5)出生日期为日期格式

(6)年龄为数值型,且在0~100之间

createtablestudent(

snosmallintconstraintaprimarykey,----设置学生学号为student的主键

snamevarchar(10)notnull,

sexvarchar

(2)constraintbcheck(sexin('男','女')),----检查约束——性别的值只能是’男’或’女’

deptvarchar(20)constraintccheck(deptin('信息系','计算机科学系','数学系','管理系','中文系','外语系','法学系')),----检查约束——系包括这几个:

信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系

birthdatetime,

agesmallintconstraintdcheck(agebetween0and100)----检查约束——年龄为数值型,且在~100之间

 

cs(成绩表):

sno

cno

cj

其中约束如下:

(1)sno和cno分别参照student和course表中的sno,cno的字段

(2)cj(成绩)只能在0~100之间,可以不输入值

createtablecs(

snosmallintnotnullreferencesstudent(sno),----定义成外键

cnosmallintnotnullreferencescourse(cno),----定义成外键

cjsmallintconstraintecheck(cjbetween0and100),----检查约束——cj(成绩)只能在~100之间,可以不输入值

constraintfprimarykey(sno,cno)----定义学生学号和课程号为sc表的主键

course(课程表)

cno

cname

其约束如下:

(1)课程号(cno)不能有重复的

(2)课程名(cname)非空

createtablecourse(

cnosmallintnotnullconstraintgprimarykey,----设置课程号为course的主键

cnamevarchar(20)notnull

(三)针对学生课程数据库查询

(1)查询全体学生的学号与姓名。

Selectsno,snamefromstudent

(2)查询全体学生的姓名、学号、所在系,并用别名显示出结果。

Selectsnameas'姓名',snoas'学号',deptas'所在地'fromstudent

(3)查询全体学生的详细记录。

select*fromstudent

(4)查全体学生的姓名及其出生年份。

selectsname,birthfromstudent

(5)查询学校中有哪些系。

selectdistinctdeptfromstudent

(6)查询选修了课程的学生学号。

selectsnofromcswherecnoisnotnull

(7)查询所有年龄在20岁以下的学生姓名及其年龄。

selectsname,agefromstudentwhereage<20

(8)查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。

selectsname,dept,agefromstudentwhereagebetween20and23

(9)查询年龄不在20~23岁之间的学生姓名、系别和年龄。

selectsname,dept,agefromstudentwhereage<20orage>23

(10)查询信息系、数学系和计算机科学系生的姓名和性别。

selectsname,sexfromstudentwheredept='信息系'ordept='数学系'ordept='计算机科学系'

(11)查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。

selectsname,sexfromstudentwheredept!

='信息系'anddept!

='数学系'anddept!

='计算机科学系'

(12)查询所有姓刘学生的姓名、学号和性别。

selectsname,sno,sexfromstudentwheresnamelike('刘%')

(13)查询学号为2009011的学生的详细情况。

(具体的学号值根据表中数据确定)

select*fromstudentwheresno=5

(14)查询姓“欧阳”且全名为三个汉字的学生姓名

selectsnamefromstudentwheresnamelike('欧阳_')

(15)查询名字中第2个字为“晨”字的学生的姓名和学号

selectsname,snofromstudentwheresnamelike('_晨')

(16)查询所有不姓刘的学生姓名。

selectsname,snofromstudentwheresnamenotlike('刘%')

(17)查询sql课程的课程号和学分。

selectcnofromcoursewherecname='sql'

(18)查询以"DB_"开头,且倒数第3个字符为i的课程的详细情况。

select*fromcoursewherecnamelike('DB[_]%i__')

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

selectsno,cnofromcswherecjisnull

(20)查所有有成绩的学生学号和课程号。

selectsno,cnofromcswherecjisnotnull

(21)查询计算机系年龄在20岁以下的学生姓名。

selectsnamefromstudentwhereage<20anddept='计算机科学系'

(22)查询信息系、数学系和计算机科学系学生的姓名和性别。

(使用多个条件表达式)

selectsname,sexfromstudentwheredept='信息系'ordept='数学系'ordept='计算机科学系'

(23)查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。

(使用多个条件表达式)

selectsname,dept,agefromstudentwhereagebetween20and23

(24)查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。

selectsno,cjfromcswherecno=3orderbycjdesc

(25)查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。

select*fromstudentorderbydeptasc,agedesc

(26)查询学生总人数。

selectcount(*)fromstudent

(27)查询选修了课程的学生人数。

selectcount(sno)fromcswherecnoisnotnull

(28)计算1号课程的学生平均成绩。

selectavg(cj)fromcswherecno=1

(29)查询选修1号课程的学生最高分数。

selectmax(cj)fromcswherecno=1

(30)求各个课程号及相应的选课人数。

selecto,count(cs.sno)fromcourseleftjoincs

ono=ogroupbyo

(31)查询选修了3门以上课程的学生学号。

selectsno,count(cno)fromcsgroupbysnohavingcount(cno)>3

(32)查询有3门以上课程是90分以上的学生的学号及(90分以上的)课程数。

selectsno,count(cno)as'课程数'fromcswherecj>90

groupbysnohavingcount(cno)>=3

(33)查询学生2006011选修课程的总学分。

selectsum(course)fromcourse,cswhereo=cs.snoandcs.sno=2006011

(34)查询每个学生选修课程的总学分。

selectsno,sum(cj)fromcs,course

whereo=o

groupbysno

union

selectsno,0fromstudent

wheresnonotin(selectsnofromcs)

(35)查询每个学生及其选修课程的情况。

selectcs.sno,course.*fromcs,coursewhereo=o

(36)查询选修2号课程且成绩在90分以上的所有学生的学号、姓名

selectsno,snamefromstudentwheresno=(selectsnofromcswherecno=2andcj>90)

(37)查询每个学生的学号、姓名、选修的课程名及成绩。

selectstudent.sno,sname,course.course,cs.cj

fromstudent,course,cswherestudent.sno=cs.snoando=o

(38)查询与“刘晨”在同一个系学习的学生(分别用嵌套查询和连接查询)

----嵌套查询

select*fromstudentwheredeptin

(selectdeptfromstudentwheresname='刘晨')

----连接查询

selectstu1.*fromstudentasstu1,studentasstu2

wherestu1.dept=stu2.deptandstu2.sname='刘晨'

----exists查询

select*fromstudents1whereexists(

select*fromstudents2wheres1.dept=s2.deptand

s2.sname='刘晨'

(39)查询选修了课程名

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

当前位置:首页 > 经管营销 > 经济市场

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

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