1、设有以下三个关系:
学生关系:student(sno,sname,dep,age,sex)
选课关系:sc(sno,cno,grade)
课程关系:course(cno,cname,score)
1)建数据库,在数据库中建立以上三个关系。
要求:学生关系中:sno为主键,sname不允许为空,age默认值18,sex取值范围’男’、’女’、默认为’男’。
课程关系中:cno为主键,cname不为空,在cname上建立唯一索引。
选课关系中:sno、cno组合为主键,grade允许为空,sno为外键(参照学生关系的sno,级联删除、级联修改),cno为外键(参照课程关系的cno,限制删除、限制修改)。
2)试用SQL的查询语句表示下列查询。
(1) 检索学分score等于4的所有课程的课程号和课程名。
select cno,cname
from course
where score=4;
(2) 检索年龄在20和23岁之间的学生的学号与姓名。
select sno,sname
from student
where age between 20 and 23 ;
(3) 检索Wang同学不学的课程的课程号。
select cno
from course
where cno not in
(select cno
from sc,student
where sc.sno =student.sno
and student.sname='Wang');
(4) 检索所有姓李的学生情况。
select *
from student
where sname like '李%';
(5) 检索所有学生情况及其选课情况。(可以用f1键查看左外联接)
select student.*,sc.*
from student left outer join sc
on student.sno = sc.sno;
(6) 检索所有选修了课程的学生情况和选课情况。
select student.*,sc.*
from student , sc
where student.sno = sc.sno;
(7) 检索至少选修两门课程的学生学号和姓名。只能用嵌套查询!
select sno,sname
from student
where sno in
(select sno
搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新人文社科SQL作业参考答案全文阅读和word下载服务。
相关推荐: