以下答案可能存在错误,敬请谅解!
1.1.2
、有三张表
,
学生表
S,
课程
C,
学生课程表
SC,
学生可以选修
多门课程
,
一门课程可以被多个学生选修
,
通过
SC
表关联。
1
)写出建表语句;
Create table S (
Id int auto_incretment primary key,
name varchar(20);
)
Create table C(
Id int auto_increment primary key,
Name varchar(20)
)
Create table SC(
Sid int foreign key references S(id),
Cid int foreign key references S(id),
)
2
) 写出
SQL
语句
,
查询选修了所有选修课程的学生;
Select name from S where ((select count(*) from SC where Sid=S.id)=(select count(*) from
C))
3
) 写出
SQL
语句
,
查询选修了至少
5
门以上的课程的学生。
Select name from S where id in (select sid from Sc where (select count(*) from SC where
C.id=SC.Cid) > 5))
Select name from s where (select count(*) from sc where sc.sid =s.id ) >= 5
答:
1
)建表语句如下(
mysql
数据库):
create table s(id integer primary key, name varchar(20));
create table c(id integer primary key, name varchar(20));
create table sc(
sid integer references s(id),
cid integer references c(id),
primary key(sid,cid)
);
2
)
SQL
语句如下:
select stu.id, stu.name from s stu
where (select count(*) from sc where sid=stu.id)
= (select count(*) from c);
3
)
SQL
语句如下:
select stu.id, stu.name from s stu
where (select count(*) from sc where sid=stu.id)>=5;
108
、数据库表
(Test)
结构如下:【基础】
评论