--创建外键
# create table my_foreignl(
# id int primary key auto_increment,
# name varchar(20) not null comment
# '学生姓名',
# c_id int comment '班级id',
# --普通字段
# )charset utf8;
# --增加外键
# alter table my_foreign2 add
# --指定外键名
# constraint atudent_class_1
# --指定外键字段
# foreign key(c_id)
# --引用父表主键
# references my_class(id);
# --删除外键
# alter table my_foreignl drop
# foreign key my_foreignl_ibfk_l;
# --插入数据:外键字段在父表不存在
# insert into my_foreign2 values(null,'郭富城',4)--没有四号班级
# insert into my_foreign2 values(null,'项羽',1);
# insert into my_foreign2 values(null,'刘邦',2);
# insert into my_foreign2 values(null,'韩信',3);
# update my_foreign2 set c_id=2
# where id=4;
# --更新父表记录
# update my_class set id=4 where id=1; --失败;id=1记录已经被学生引用
# update my_class set id=4 where id=3; --可以:没有学生引用此班级
# --插入数据
# insert into my_foreignl values(null,'马超',3);
# --增加外键
# alter table my_foreignl add foreign key(c_id)references my_class(id);
# --失败:因为没有三号班
# --创建外键:指定模式:删除置空,更新级联
# create table my_foreign3(
# id int primary key auto_increment,name varchar(20) not null,
# c_id int,
# --增加外键
# foreign key(c_id)
# --引用表
# references my_class(id)
# --指定删除模式
# on delete set null
# --指定更新模式
# on update cascade
# )charset utf8;
# --插入数据
# insert into my_foreign3 values(
# null,'刘备',1),
# (null,'曹操',1),
# (null,'孙权',1),
# (null,'诸葛亮',2),
# (null,'周瑜',2);
# --解除my_foreign2表的外键
# alter table my_foreign2 drop foreign key student_class_1;
# --更新父表主键
# update my_class set id=3 where id=1;
# --删除父表主键
# delete from my_class where id=2;
# --联合查询
# select * from my_class
# union --默认去重
# select * from my_class;
# select * from my_class
# union all --不去重
# select * from my_class;
# select id,c_name,room from my_class
# union all --不去重
# select name,number,id from my_student;
# --需求:男生升序,女生降序(年龄)
# (select * from my_student
# where sex='男'
# order by age asc limit 999999)
# union
# (select * from my_student
# where sex='女'
# order by age desc limit 9999999);
# select * from my_student where c_id=(
# --标量子查询
# select id from my_class where c_name='Python1903');--id一定只有一个值(一行一列)
# insert into my_class values(1,'python1907','B407');
# --列子查询
# select * from my_student where c_id in(select id from my_class);
# --any,some,all
# select * from my_student where c_id=any(select id from my_class);
# select * from my_student where c_id=some(select id from my_class);
# select * from my_student where c_id=all(select id from my_class);
# select * from my_student where c_id!=any(select id from my_class); --所有结果(NULL除外)
# select * from my_student where c_id!=some(select id from my_class); --所有结果(NULL除外)
# select * from my_student where c_id!=all(select id from my_class); --2号班级(NULL除外)
# update my_student set height=188
# where name='王五';
# select * from my_student where age=(select max(age) from my_student)
# and
# height=(select max(height)from my_student);
# --行子查询
# select * from my_student
# --(age,height)称之为行元素
# where(age,height)=(select max(age),max(height)from my_student);
# select *from my_student order by age desc,height desc limit 1;
# --表字查询
# select * from my_student group by c_id order by height desc;--每个班选出第一个学生再按身高排序
# selecr * from (select * from my_student order by height desc)
# as student group by student.c_id;