数据库期中(二)

教务系统中存储着学生信息,包括学号、姓名、专业;课程信息,包括课程号、课程名,
学分,学生的选课信息,包括,学号、课程号、成绩。
1)写出课程信息、选课信息的数据库模式;
学生 ( 学号 , 姓名 , 专业 );
课程 ( 课程号 , 课程名 , 学分 );
选课 ( 学号 , 课程号 , 成绩 );

2)写出创建课程信息的建表命令;

Create table 课程 (
课程号 char(6),
课程名 varchar(30),
学分 decimal(2,1),
Primary key ( 课程号 )
);
create table stu(
sno char ( 12 ),
name varchar ( 120 ) not null ,
major varchar ( 30 ) not null ,
Primary key (sno)
);
Coment on table stu is ‘ 学生信息表 ’;
Coment on column stu.sno is ‘ 学号 ’;
ALTER TABLE stu COMMENT=' 学生信息表 ';
ALTER TABLE stu MODIFY sno CHAR(6) COMMENT ' 学号 ';
Create table course(
cno char(7),
cname varchar(30) not null,
credit decimal(2,1) not null,
Primary key (cno)
);
Create table sc(
sno char(12),
cno char(7),
score decimal(4,1),
Primary key (sno,cno)
);

3)写出下面查询的关系代数表达式:

        a)查询出成绩小于 60 分的学生的学号;

Select sno From sc Where score < 60;

        b)查询出成绩小于 60 分的学生的学号、姓名;

Select stu.sno, name
From stu, sc
Where stu.sno = sc.sno
And score < 60 ;

        c)查询出从来没有选修课程的学生学号;

Select sno
From stu
Where sno not in ( Select sno From sc);

        d)查询出成绩最小的学生的学号;

Select sno
From sc
Where score = ( select min (score) from sc);

        e)查询出至少有 2 个学生选修的课程号;

Select distinct sc.cno
From sc, sc s1
Where sc.cno = s1.cno
and sc.sno <> s1.sno;
         f) 查询出选修了所有课程的学生学号
Select distinct sno From sc a Where not exists (
        select * From course b Where not exists (
                 select * from sc where sno = a.sno  and cno = b.cno));

        g)求至少选择了C001和C003两门课程的学生学

select  distinct  sno  from  sc  A  where  not  exists(

       select  *  from   course B where  cno   in ('C002','C003')  and   not  exists(

              select * from  sc C where   A.sno=C.sno   and   B.cno=C.cno));

R(X,Y)÷S(Y,Z)的运算用结构化语言 SQL 语句可表达为下列形式:
select distinct R.X from R R1
where not exists ( select S.Y
                               from S
                               where not exists ( select * from R R2
                                                            where R2.X = R1.X and R2.Y = S.Y));

猜你喜欢

转载自blog.csdn.net/a45667/article/details/124076174
今日推荐