sql练习题+答案.doc

上传人:h**** 文档编号:817272 上传时间:2018-11-02 格式:DOC 页数:16 大小:98KB
下载 相关 举报
sql练习题+答案.doc_第1页
第1页 / 共16页
sql练习题+答案.doc_第2页
第2页 / 共16页
sql练习题+答案.doc_第3页
第3页 / 共16页
sql练习题+答案.doc_第4页
第4页 / 共16页
sql练习题+答案.doc_第5页
第5页 / 共16页
点击查看更多>>
资源描述

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

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

3、ent 和 course 表中的 sno,cno 的字段(2)cj(成绩)只能在 0100 之间,可以不输入值create table cs(sno smallint not null references student(sno),-定义成外键cno smallint not null references course(cno),-定义成外键cj smallint constraint e check(cj between 0 and 100),-检查约束cj(成绩)只能在100之间,可以不输入值constraint f primary key(sno,cno)-定义学生学号和课程号为sc

4、表的主键)course(课程表)cno cname其约束如下:(1)课程号(cno)不能有重复的(2)课程名(cname)非空create table course(cno smallint not null constraint g primary key,-设置课程号为course 的主键cname varchar(20) not null)(三)针对学生课程数据库查询(1) 查询全体学生的学号与姓名。Select sno,sname from student(2) 查询全体学生的姓名、学号、所在系,并用别名显示出结果。Select sname as 姓名,sno as 学号, dept

5、as 所在地 from student(3) 查询全体学生的详细记录。select * from student(4) 查全体学生的姓名及其出生年份。select sname,birth from student(5) 查询学校中有哪些系。select distinct dept from student(6) 查询选修了课程的学生学号。select sno from cs where cno is not null(7) 查询所有年龄在20岁以下的学生姓名及其年龄。select sname,age from student where age 23(10) 查询信息系、数学系和计算机科学系生

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

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

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

9、查询有3门以上课程是90分以上的学生的学号及(90分以上的)课程数。select sno, count(cno) as 课程数 from cs where cj90 group by sno having count(cno)=3(33) 查询学生2006011选修课程的总学分。select sum(course) from course,cs where o=cs.sno and cs.sno=2006011(34) 查询每个学生选修课程的总学分。select sno,sum(cj)from cs,coursewhere o=ogroup by snounionselect sno, 0 f

10、rom studentwhere sno not in (select sno from cs) (35) 查询每个学生及其选修课程的情况。select cs.sno,course.* from cs,course where o=o (36) 查询选修2号课程且成绩在90分以上的所有学生的学号、姓名select sno,sname from student where sno=(select sno from cs where cno=2 and cj90)(37) 查询每个学生的学号、姓名、选修的课程名及成绩。select student.sno,sname,course.course,c

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

展开阅读全文
相关资源
相关搜索
资源标签

当前位置:首页 > 教育教学资料库 > 参考答案

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。