0
点赞
收藏
分享

微信扫一扫

#yyds干货盘点# 15 MySQL 外键

外键

两个表之间一定是通过某种介质联系,这个介质就是外键。所以外键是建立表之间的联系的必须的前提。外键也是一种约束。我们目前知道的约束有

#yyds干货盘点# 15 MySQL 外键_数据


创建外键有两种方式

1.创建表的时候

create table tbl_students (

id int not null primary key auto_increment,

name varchar(50) not null,

gender varchar(1) not null,

age int(11) not null,

cls_id int(11),

constraint stu_cls foreign key(cls_id) references tbl_classes(id)

)engine = innodb default charset=utf8;


2.更新成外键

alter table tbl_students add constraint stu_cls foreign key(cls_id) references tbl_classes(id);

#yyds干货盘点# 15 MySQL 外键_外键_02

因为这里面有两个没有班级的数据,因为连接不到数据,所以不让创建外键。也就是外键的值一定能在某个表中查到数据。

delete from tbl_students where id > 5;

#yyds干货盘点# 15 MySQL 外键_默认值_03


注意事项

因为我们现在的表已经有关系了,所以当我们删除一条数据的时候,如果这个表跟其他表有关系,删除的时候就会报错。

  • restrict(限制):默认值,抛异常
  • cascade:从主表中删除或更新对应的行,同时自动的删除或更新从表中匹配的行
  • set null:从主表中删除或更新对应的行,同时将从表中的外键列设为空
  • no action:拒绝删除或者更新父表
create table tbl_students (

id int not null primary key auto_increment,

name varchar(50) not null,

gender varchar(1) not null,

age int(11) not null,

cls_id int(11),

constraint stu_cls foreign key(cls_id) references tbl_classes(id) on delete cascade

)engine = innodb default charset=utf8;


或者前提是没有外键,如果有请先删除

alter table tbl_students add constraint stu_cls foreign key(cls_id) references tbl_classes(id) on delete cascade;

alter table tbl_students drop foreign key stu_cls; #删除外键

如果是默认,我删除一个班级。

#yyds干货盘点# 15 MySQL 外键_数据_04

如果是cascade

#yyds干货盘点# 15 MySQL 外键_外键_05

#yyds干货盘点# 15 MySQL 外键_默认值_06


举报

相关推荐

0 条评论