学生表
1.1 student(Sid,Sname,Sage,Ssex)
1.2 Sid 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
课程表
2.1 course(Cid,Cname,Tid)
2.2 Cid 课程编号,Cname 课程名称,Tid 教师编号
教师表
3.1 teacher(Tid,Tname)
3.2 Tid 教师编号,Tname 教师姓名
成绩表
4.1 sc(Sid,Cid,score)
4.2 Sid 学生编号,Cid 课程编号,score 分数
根据需求创建以上四张表,添加对应的测试数据,测试数据如下:
create table student(Sid varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into student values('01' , '赵雷' , '1990-01-01' , '男');
insert into student values('02' , '钱电' , '1990-12-21' , '男');
insert into student values('03' , '孙风' , '1990-05-20' , '男');
insert into student values('04' , '李云' , '1990-08-06' , '男');
insert into student values('05' , '周梅' , '1991-12-01' , '女');
insert into student values('06' , '吴兰' , '1992-03-01' , '女');
insert into student values('07' , '郑竹' , '1989-07-01' , '女');
insert into student values('08' , '王菊' , '1990-01-20' , '女');
create table course(Cid varchar(10),Cname varchar(10),Tid varchar(10));
insert into course values('01' , '语文' , '02');
insert into course values('02' , '数学' , '01');
insert into course values('03' , '英语' , '03');
create table teacher(Tid varchar(10),Tname varchar(10));
insert into teacher values('01' , '张三');
insert into teacher values('02' , '李四');
insert into teacher values('03' , '王五');
create table sc(Sid varchar(10),Cid varchar(10),score decimal(18,1));
insert into sc values('01' , '01' , 80);
insert into sc values('01' , '02' , 90);
insert into sc values('01' , '03' , 99);
insert into sc values('02' , '01' , 70);
insert into sc values('02' , '02' , 60);
insert into sc values('02' , '03' , 80);
insert into sc values('03' , '01' , 80);
insert into sc values('03' , '02' , 80);
insert into sc values('03' , '03' , 80);
insert into sc values('04' , '01' , 50);
insert into sc values('04' , '02' , 30);
insert into sc values('04' , '03' , 20);
insert into sc values('05' , '01' , 76);
insert into sc values('05' , '02' , 87);
insert into sc values('06' , '01' , 31);
insert into sc values('06' , '03' , 34);
insert into sc values('07' , '02' , 89);
insert into sc values('07' , '03' , 98);
select a.*, b.*, c.* from (select * from sc where Cid = '01') a
left join (select * from sc where Cid = '02') b
on a.Sid = b.Sid
left join student c
on a.Sid = c.Sid
where a.score > b.score;
update sc set score=100 where Sid="01" and Cid=(select Cid from course where Cname="语文");
select a.*, b.* from (select * from sc where Cid = '01') a
left join (select * from sc where Cid = '02') b
on a.Sid = b.Sid
where b.Sid is not null;
select Sid, Sname from student where Sid in
(select Sid from sc group by Sid having avg( score) >= 70);
select a.Sid, a.score, b.Sname from (
select Sid, avg(score) as score from sc group by Sid having avg(score) >= 85)a
left join student b
on a.Sid = b.Sid;
select a.Sid, b.Sname, c.Cname, a.score from sc a
left join student b
on a.Sid = b.Sid
left join course c
on a.Cid = c.Cid
where score > 70;
select * from student where year(Sage) = 1990;
select Cid, count(distinct Sid) as counts from sc group by C
select Ssex, count(distinct Sid) as counts from student group by Ssex;
select * from student where Sname like '%风%';