数据库设计
学生表
student(Sid,Sname,Sage,Ssex)
Sid 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别课程表
course(Cid,Cname,Tid)
Cid 课程编号,Cname 课程名称,Tid 教师编号教师表
teacher(Tid,Tname)
Tid 教师编号,Tname 教师姓名4.成绩表
SC(Sid,Cid,score)
sid 学生编号,C# 课程编号,score 分数
题目
sql语句
- 学生表
DROP TABLE IF EXISTS `student`;
create table student(
Sid VARCHAR(10) not null PRIMARY KEY,
Sname VARCHAR(10) not null,
Sage DATE ,
Ssex VARCHAR(2) not null
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
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' , '女');
- 课程表
DROP TABLE IF EXISTS `course`;
create table course(
Cid varchar(10) not null PRIMARY KEY,
Cname varchar(10) not null,
Tid VARCHAR(10) not null
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into course values('01' , '语文' , '02');
insert into course values('02' , '数学' , '01');
insert into course values('03' , '英语' , '03');
- 教师表
DROP TABLE IF EXISTS `teacher`;
create table teacher(
Tid varchar(10) not null PRIMARY KEY,
Tname VARCHAR(10)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into teacher values('01' , '张三');
insert into teacher values('02' , '李四');
insert into teacher values('03' , '王五');
- 成绩表
DROP TABLE IF EXISTS `score`;
create table score (
Sid varchar(10) NOT NULL,
Cid varchar(10) NOT NULL,
score decimal(10,1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into score values('01' , '01' , 80);
insert into score values('01' , '02' , 90);
insert into score values('01' , '03' , 99);
insert into score values('02' , '01' , 70);
insert into score values('02' , '02' , 60);
insert into score values('02' , '03' , 80);
insert into score values('03' , '01' , 80);
insert into score values('03' , '02' , 80);
insert into score values('03' , '03' , 80);
insert into score values('04' , '01' , 50);
insert into score values('04' , '02' , 30);
insert into score values('04' , '03' , 20);
insert into score values('05' , '01' , 76);
insert into score values('05' , '02' , 87);
insert into score values('06' , '01' , 31);
insert into score values('06' , '03' , 34);
insert into score values('07' , '02' , 89);
insert into score values('07' , '03' , 98);
题目
练习
select A.Sid, A.Cid, A.score, B.Cid, B.score from (select * from score where Cid='01') A left JOIN (select * from score where Cid='02') B on A.Sid = B.Sid where A.score>B.score;
select * from (select * from score where Cid='01') A left JOIN (select * from score where Cid='02') B on A.Sid = B.Sid where B.Sid is not null;
select * from (select * from score where Cid='01') A left JOIN (select * from score where Cid='02') B on A.Sid = B.Sid;
select * from score where Cid='02' and Sid not in (select Sid from score where Cid='01');
select B.Sid, B.Sname, A.d from (select Sid, AVG(score) d from score GROUP BY Sid) A left join student B on A.Sid=B.Sid where A.d>60;
select * from student where Sid in (select DISTINCT(Sid) from score);
select A.Sid, A.Sname, B.c, B.s from student A LEFT JOIN (select Sid, COUNT(*) c,sum(score) s from score GROUP BY Sid)B on A.Sid=B.Sid;
select * from student where Sid in (select DISTINCT(Sid) from score);
select Sid from score where Cid IN (select c.Cid from teacher t LEFT JOIN course c on t.Tid = c.Tid WHERE t.Tname='张三');