-- 创建外键
create table my_foreign1(
idint primary key auto_increment,
name varchar (20)not null comment
'学生姓名',
c_idint comment'班级id',
-- 增加外键
foreign key(c_id)references
my_class(id)
)charset utf8;
-- 创建表
create table my_foreign2(
idint primary key auto_increment,
name varchar (20)not null comment
'学生姓名',
c_idint comment'班级id'-- 普通字段
)charset utf8;
-- 增加外键
alter table my_foreign2add
-- 指定外键的名字
constraint student_class_1-- 可以指定多个外键 但是名字不能相同
-- 指定外键的字段
foreign key(c_id)
-- 引用父表主键
references my_class(id);
-- 删除外键
alter table my_foreign1drop
foreign key my_foreign1_ibfk_1;-- my_foreign1_ibfk_1 通过外键的名字来删
-- 插入数据;外键字段在父表不存在
insert into my_foreign2values (
null,'郭富城',4);-- 没有4号班级
insert into my_foreign2values (
null,'项羽',1);
insert into my_foreign2values (
null,'刘邦',2);
insert into my_foreign2values (
null,'韩信',3);
-- 更新父表的记录
update my_classset id=4 where id=1;-- 失败;id=1记录已经被学生引用
update my_foreign2set c_id=2 where id=4;-- 更新
update my_classset id=4 where id=3;-- 可以;没有学生引用此班级
-- mysql中添加外键约束遇到一下情况:
-- cannot add foreign key constraint
-- 出现这个问题的原因是,外键的使用:
-- 1. 外键字段不能为该表的主键;
-- 2. 外键字段参考字段必须为参考表的主键
-- 插入数据
insert into my_foreign1values (
null,'马超','3'
);
-- 增加外键
alter table my_foreign1add
foreign key(c_id)references
my_class(id); -- 失败;因为没有3号班了
-- 创建外键,指定模式;删除置空;更新级联
create table my_foreign3(
idint primary key auto_increment,
name varchar (20)not null,
c_idint,
-- 增加外键
foreign key (c_id)
-- 引用表
references my_class(id)
-- 指定删除模式
on delete set null
-- 指定更新模式
on update cascade
)charset utf8;
-- 插入数据
insert into my_foreign3values (
null,'刘备',1),
(null,'曹操',1),
(null,'孙权',1),
(null,'祝贺量',2),
(null,'周瑜',2);
-- 解除My_foreign2表的外键
alter table my_foreign2drop
foreign key student_class_1;
-- 更新父表主键
update my_classset id=3 where id=1;
-- 删除父表主键
delete from my_classwhere 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,roomfrom my_class
union all -- 不去重
select name,number,idfrom my_student;
-- 需求;男生升序;女生降序(年龄)
(select * from my_student
where sex='男'
order by ageasc limit9999999)
union
(select * from my_student
where sex='女'
order by agedesc limit9999999);
select * from my_studentwhere
c_id=(
-- 标量子查询
select idfrom my_classwhere
c_name='python1903');-- id一定只有一个值(一行一列)
insert into my_classvalues (1,
'python1907','B407');
-- 列子查询
select * from my_studentwhere
c_idin(select idfrom my_class);
-- any,some,all
select * from my_studentwhere
c_id=any(select idfrom my_class);
select * from my_studentwhere
c_id=some(select idfrom my_class);
select * from my_studentwhere
c_id=all(select idfrom my_class);
select * from my_studentwhere
c_id!=any(select idfrom my_class);-- 所有结果(null除外)
select * from my_studentwhere
c_id!=some(select idfrom my_class);-- 所有结果(null除外)
select * from my_studentwhere
c_id!=all(select idfrom my_class); -- 所有2号班级(null除外)
select * from my_studentwhere
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);
update my_studentset height=188
where name='王五';
select * from my_studentorder by
agedesc,heightdesc limit1;
select * from my_studentorder by
heightdesc;
-- 表子查询
select * from my_studentgroup by
c_idorder by heightdesc; -- 每个班选出第一个学生再按身高排序
select * from (select * from
my_studentorder by heightdesc)
as studentgroup by student.c_id;
作者:BJ000
链接:https://www.jianshu.com/p/9a17ce48da21
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。