数据库设置:
create database sqllx;
use sqllx;
create table Student(sid varchar(10),sname varchar(10),sage datetime,ssex nvarchar(10));
insert into Student values
('01' , '赵雷' , '1990-01-01' , '男'),
('02' , '钱电' , '1990-12-21' , '男'),
('03' , '孙风' , '1990-05-20' , '男'),
('04' , '李云' , '1990-08-06' , '男'),
('05' , '周梅' , '1991-12-01' , '女'),
('06' , '吴兰' , '1992-03-01' , '女'),
('07' , '郑竹' , '1989-07-01' , '女'),
('08' , '王菊' , '1990-01-20' , '女');
select * from Student;
create table Course(cid varchar(10),cname varchar(10),tid varchar(10));
insert into Course values
('01' , '语文' , '02'),
('02' , '数学' , '01'),
('03' , '英语' , '03');
create table Teacher(tid varchar(10),tname varchar(10));
insert into Teacher values
('01' , '张三'),
('02' , '李四'),
('03' , '王五');
create table SC(sid varchar(10),cid varchar(10),score decimal(18,1));
insert into SC values
('01' , '01' , 80),
('01' , '02' , 90),
('01' , '03' , 99),
('02' , '01' , 70),
('02' , '02' , 60),
('02' , '03' , 80),
('03' , '01' , 80),
('03' , '02' , 80),
('03' , '03' , 80),
('04' , '01' , 50),
('04' , '02' , 30),
('04' , '03' , 20),
('05' , '01' , 76),
('05' , '02' , 87),
('06' , '01' , 31),
('06' , '03' , 34),
('07' , '02' , 89),
('07' , '03' , 98);
alter table Course rename co;
alter table teacher rename te;
alter table student rename stu;
#为方便书写,将table名改的比较简单一些了。
in 适合B表(sc)比A表(stu)数据量小的情况,exists适合B表比A表数据量大的情况,可参考链接:https://www.jianshu.com/p/f212527d76ff