alter table my_class add id varchar(10);
alter table my_class change id id varchar(10) first;
alter table my_class change class room varchar(10);
truncate my_class;
insert into my_class values('1','Java01','A101'),('2','Python01','B101');
insert into my_class (id,name,room) value ('3','Java02','B203');
update my_student set c_id='3' where name in ('Tom','Lily','张三');
update my_student set c_id='1' where name in ('Lucy','Anthony');
update my_student set c_id='2' where name ='李四';
select * from my_student inner join my_class on my_student.c_id=my_class.id;
select * from my_student inner join my_class on c_id = my_class.id;
select s.*,c.name as c_name,c.room from my_student as s inner join my_class as c on s.c_id=c.id;
select s.*,c.name as c_name,c.room from my_student as s inner join my_class as c where s.c_id=c.id;
---------外连接
select * from my_class;
select * from my_student;
--左连接
select s.* ,c.name as c_name,c.room from my_student as s left join my_class as c
on s.c_id=c.id;
--右连接
select s.*,c.name as c_name,c.room from my_student as s right join my_class as c on c.id=s.c_id;
--左右连接可以互转的
select s.*,c.name as c_name,c.room from my_class as c right join my_student as s on c.id = s.c_id;
--创建学生数据库
create database school;
use school;
create table Student(
StuId varchar(5) primary key,
StuName varchar(10) not null,
StuAge int,
StuSex char(1) not null
)charset utf8;
insert into Student values('1000','张无忌',18,'男'),
('1001','周芷若',19,'女'),
('1002','杨过',19,'男'),
('1003','小龙女',17,'女'),
('1004','赵敏',18,'女');
insert into Student (StuId,StuName,StuAge,StuSex) value ('1005','张三丰',20,'男');
insert into Student (StuId,StuName,StuAge,StuSex) value ('1006','令狐冲',19,'男');
insert into Student (StuId,StuName,StuAge,StuSex) values ('1007','任盈盈',20,'女'),
('1008','邱灵珊',19,'女'),
('1009','韦小宝',18,'男'),
('1010','康敏',17,'女');
--创建老师表
create table Teacher(
TeaId varchar(3) primary key,
TeaName varchar(10) not null
)charset utf8;
insert into Teacher values('001','张伟'),
('002','叶平'),
('003','张军'),
('004','李小璐'),
('005','乔丹'),
('006','科比');
create table Course(
CourseId varchar(3) primary key,
CourseName varchar(20) not null,
TeaId varchar(3) not null,
foreign key(TeaId) references Teacher(TeaId)
)charset utf8;
insert into Course values ('001','企业管理','005'),
('002','篮球','006'),
('003','马克思','001'),
('004','Java基础','003'),
('005','数据结构','002'),
('006','舞蹈','004');
create table Score(
StuId varchar(5) not null,
CourseId varchar(3) not null,
Score float,
foreign key(StuId) references Student(StuId),
foreign key(CourseId) references Course(CourseId)
)charset utf8;
insert into Score values ('1001','003',90),
('1002','001',85),
('1003','002',65),
('1004','005',55),
('1005','004',95),
('1006','003',80),
('1007','002',75),
('1008','005',99),
('1009','001',79),
('1010','004',88);