-- 课程表,(名字起的有点烂)
create table CLASS
(
ID INTEGER,
NAME VARCHAR2(50)
)
insert into class (ID, NAME)
values (1, '语文');
insert into class (ID, NAME)
values (2, '数学');
insert into class (ID, NAME)
values (3, '英语');
insert into class (ID, NAME)
values (4, '物理');
--学生表
create table STUDENT
(
ID INTEGER,
NAME VARCHAR2(8)
)
insert into student (ID, NAME)
values (1, 'a');
insert into student (ID, NAME)
values (2, 'b');
insert into student (ID, NAME)
values (3, 'c');
insert into student (ID, NAME)
values (4, 'd');
insert into student (ID, NAME)
values (5, 'e');
--中间表
create table STU_CLASS
(
SID INTEGER,
CID INTEGER,
SCORE INTEGER
)
insert into stu_class (SID, CID, SCORE)
values (1, 1, 58);
insert into stu_class (SID, CID, SCORE)
values (1, 2, 58);
insert into stu_class (SID, CID, SCORE)
values (2, 1, 58);
insert into stu_class (SID, CID, SCORE)
values (2, 2, 58);
insert into stu_class (SID, CID, SCORE)
values (3, 1, 79);
insert into stu_class (SID, CID, SCORE)
values (3, 2, 79);
insert into stu_class (SID, CID, SCORE)
values (4, 2, 79);
insert into stu_class (SID, CID, SCORE)
values (5, 1, 23);
insert into stu_class (SID, CID, SCORE)
values (5, 3, 24);
以上是我所创建的三张表
下面是SQL,至于存储过程,没有创建也只是用SQL写了一下:
--查询所有选“数学”课程的学生学号,姓名和专业
select s.id, s.name
from student s, stu_class sc
where s.id = sc.sid and sc.cid = 2;
--删除所有选择数学的同学的选课记录
delete from stu_class sc
where sc.sid in (select sid from stu_class where cid = 2)
--查询有哪些课程没有被任何同学报选
select c.name
from class c
where c.id not in(select distinct(cid) from stu_class);
--能够查询出选修任何课程的学生信息及相应的分数
select s.*, sc.score from student s, stu_class sc where s.id = sc.sid and sc.cid = 3;
--以表格的形式返回本课程的平均分数
select avg(score) from stu_class where cid = 2;