创建学生表:
create table student(
sid int auto_increment primary key not null,
sname varchar(20),
sxday date,
sex varchar(20)
)
查看:
desc student;
在表里添加数据:
insert into student values(1,“ss”,“1990-01-01”,“男”),(2,“小虎”,“1990-05-20”,“男”),(3,“小花”,“1991-03-11”,“女”),(4,“赵磊”,“1999-12-25”,“男”)
查看添加后的表:
select * from student;
创建老师表:
create table teacher(
tid int auto_increment primary key not null,
tname varchar(20)
)
查看:
desc teacher;
在表里添加数据:
insert into teacher values(1,“韩老师”),(2,“王老师”),(3,“刘老师”),(4,“李老师”),(5,“胡老师”),(6,“马老师”)
查看添加后的表:
select * from teacher;
创建课程表:
create table course(
cid int auto_increment primary key not null,
cname varchar(20),
tid int,
foreign key(tid) references teacher(tid)
)
查看:
desc course;
在表里添加数据:
insert into course values(1,“语文”,1),(2,“数学”,3),(3,“英语”,5),(4,“物理”,2),(5,“化学”,4),(6,“政治”,6)
查看添加后的表:
select * from course;
– 小虎同学的数学成绩
select sc.scores from scores sc
LEFT JOIN students s ON sc.sid=s.sid
LEFT JOIN courses c On sc.cid=c.cid
where s.sid=2 and c.cid=2
– 马老师都教了哪些课程
select t.tname,c.cname from teachers t INNER JOIN courses c on t.tid=c.tid where t.tname=‘马老师’
– 第三题
select m.name,m.scores from(select s.name,a.scores from scores a
LEFT JOIN students s ON a.sid=s.sid
LEFT JOIN courses c ON a.cid=c.cid
where c.cid=2 order by a.scores desc limit 1)as m