sql练习题+答案[整理]

上传人:x****育 文档编号:143410913 上传时间:2020-08-29 格式:PDF 页数:14 大小:120.91KB
返回 下载 相关 举报
sql练习题+答案[整理]_第1页
第1页 / 共14页
sql练习题+答案[整理]_第2页
第2页 / 共14页
sql练习题+答案[整理]_第3页
第3页 / 共14页
sql练习题+答案[整理]_第4页
第4页 / 共14页
sql练习题+答案[整理]_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《sql练习题+答案[整理]》由会员分享,可在线阅读,更多相关《sql练习题+答案[整理](14页珍藏版)》请在金锄头文库上搜索。

1、1 (一)新建以下几个表 student( 学生表 ) : snosnamesexdeptbirthage 其中约束如下: (1)学号不能存在相同的 (2)名字为非空 (3)性别的值只能是男或女 (4)系包括这几个:信息系,计算机科学系,数学系, 管理系,中文系,外语系,法学系 (5)出生日期为日期格式 (6)年龄为数值型,且在0100之间 createtable student( sno smallintconstraint a primarykey, -设置学 生学号为 student 的主键 sname varchar ( 10) notnull, sex varchar ( 2) co

2、nstraint b check ( sex in( 男 , 女 ), -检查约束性别的值只能是男或女 dept varchar ( 20) constraint c check( dept in( 信 息系 , 计算机科学系 , 数学系 , 管理系 , 中文系 , 外 语系 , 法学系 ), -检查约束系包括这几个:信息 系,计算机科学系,数学系,管理系,中文系,外语系,法 学系 birth datetime , age smallintconstraint d check( age between 0 and 2 100) -检查约束年龄为数值型,且在100之间 ) cs( 成绩表 ):

3、snocnocj 其中约束如下: (1) sno 和 cno 分别参照 student 和 course 表中的 sno,cno 的字段 (2)cj( 成绩)只能在 0100之间,可以不输入值 createtable cs ( sno smallintnotnullreferences student ( sno), -定义成外键 cno smallintnotnullreferences course (cno), -定义成外键 cj smallintconstraint e check( cj between 0 and 100 ), -检查约束 cj( 成绩 ) 只能在 100之 间,可

4、以不输入值 constraint f primarykey( sno, cno) -定义学 生学号和课程号为sc表的主键 ) course (课程表) 3 cnocname 其约束如下: (1)课程号( cno)不能有重复的 (2)课程名( cname )非空 createtable course ( cno smallintnotnullconstraint g primary key, -设置课程号为 course 的主键 cname varchar ( 20) notnull ) (三)针对学生课程数据库查询 (1)查询全体学生的学号与姓名。 Select sno , sname fro

5、m student (2)查询全体学生的姓名、学号、所在系,并用别名显示出结果。 Select sname as 姓名 , sno as 学号 , dept as 所在地 from student (3)查询全体学生的详细记录。 select* from student (4)查全体学生的姓名及其出生年份。 select sname, birth from student (5)查询学校中有哪些系。 4 selectdistinct dept from student (6)查询选修了课程的学生学号。 select sno from cs where cno isnotnull (7)查询所有

6、年龄在 20岁以下的学生姓名及其年龄。 select sname, age from student where age 20 (8)查询年龄在 2023岁(包括 20岁和23岁)之间的学生的姓名、系别和 年龄。 select sname, dept , age from student where age between 20 and 23 (9)查询年龄不在 2023岁之间的学生姓名、系别和年龄。 selectsname , dept , age from student where age23 (10)查询信息系、数学系和计算机科学系生的姓名和性别。 select sname, sex

7、from student where dept = 信息系 or dept = 数学系 or dept = 计算机科学系 (11)查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和 性别。 select sname, sex from student where dept != 信息 系and dept != 数学系 and dept != 计算机科学系 5 (12)查询所有姓刘学生的姓名、学号和性别。 select sname, sno, sex from student where sname like( 刘%) (13)查询学号为 2009011的学生的详细情况。 (具体的学号值

8、根据表中数 据确定) select* from student where sno =5 (14)查询姓“欧阳”且全名为三个汉字的学生姓名 select sname from student where sname like( 欧阳 _ ) (15)查询名字中第 2个字为“晨”字的学生的姓名和学号 selectsname, sno from student where sname like(_ 晨 ) (16)查询所有不姓刘的学生姓名。 select sname, sno from student where sname not like( 刘%) (17)查询sql 课程的课程号和学分。 s

9、elect cno from course where cname=sql (18)查询以 DB_开头,且倒数第 3个字符为 i 的课程的详细情况。 select* from course where cname like(DB_%i_) 6 (19)查询缺少成绩的学生的学号和相应的课程号。 select sno , cno from cs where cj isnull (20)查所有有成绩的学生学号和课程号。 select sno , cno from cs where cj isnotnull (21)查询计算机系年龄在 20岁以下的学生姓名。 selectsname from stud

10、ent where age 3 8 (32)查询有 3门以上课程是 90分以上的学生的学号及 (90分以上的)课程 数。 select sno ,count ( cno) as 课程数 from cs where cj 90 groupby sno havingcount ( cno)=3 (33)查询学生 2006011选修课程的总学分。 selectsum( course ) from course , cs where = and =2006011 (34)查询每个学生选修课程的总学分。 select sno , sum ( cj ) from cs , course where = g

11、roupby sno union select sno , 0 from student where sno notin( select sno from cs ) (35)查询每个学生及其选修课程的情况。 select, course .*from cs , course where = 9 (36)查询选修 2号课程且成绩在 90分以上的所有学生的学号、姓名 select sno , sname from student where sno =(select sno from cs where cno =2 and cj 90) (37)查询每个学生的学号、姓名、选修的课程名及成绩。 se

12、lect, sname, from student, course , cs where = and = (38)查询与“刘晨”在同一个系学习的学生(分别用嵌套查询和连接查 询) -嵌套查询 select* from student where dept in ( select dept from student where sname= 刘晨 ) -连接查询 selectstu1 .* from student as stu1 , student as stu2 where = and = 刘晨 -exists查询 select* from student s1 where exists(

13、select* from student s2 where = and = 刘晨 ) 10 (39)查询选修了课程名为“信息系统”的学生学号和姓名 select sno , sname from student where sno in ( select sno from cs where cno in( select cno from course where cname= 信息系统 ) (40)查询其他系中比信息系任意一个(其中某一个 ) 学生年龄小的学生姓 名和年龄 select sname, age from student where age any ( select age fro

14、m student where dept = 信息系 ) (41)查询其他系中比信息系所有学生年龄都小的学生姓名及年 龄。分别用 ALL谓词和集函数 -用ALL select sname, age from student where age all ( select age from student where dept = 信息系 ) -聚合函数 select sname, age from student where age ( selectmin( age) from student where dept = 信息 系 ) (42)查询所有选修了 1号课程的学生姓名。(分别用嵌套查询和

15、连查询) 11 -嵌套查询 select sname from student where sno in ( select sno from cs where cno =1) -连接查询 select sname from student, cs where = and =1 (43)查询没有选修 1号课程的学生姓名。 select sname from student where sno in ( select sno from cs where cno != 1) (44)查询选修了全部课程的学生姓名。 select sname from student where notexists (

16、 select* from course where notexists ( select* from cs where = and =) (45)查询至少选修了学生 95002选修的全部课程的学生号码。 selectdistinct sno from sc scx where not exists ( select* from cs scy where =95002and not exists ( select* from sc scz where = and =) 12 (46)查询计算机科学系的学生及年龄不大于19岁的学生的信息。 select* from student where dept = 计算机科学系 or age19 (47)查询选修了课程 1或者选修了课程 2的学生的信息。 selectstudent .*from studen

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 习题/试题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号