前言:

外键表示了两个实体之间的联系

外键 foreing key: a表中的一个字段的值指向另b表的主键

  • b: 主表
  • a: 从表

主表:主键(主关键字) = 从表:外键(外关键字)

1、外键操作

1.1、增加外键

基本语法:

方式一:创建表的时候增加外键

[constraint `外键名`] foreign key (外键字段) references 主表(主键);

方式二:创建表后增加外键

alter table 从表 add [constraint `外关键字`] foreign key (外键字段) references 主表(主键);

示例1: 创建表的时候增加外键

-- 创建外键
create table my_foreign(
    id int primary key auto_increment,
    name varchar(10) not null,
    class_id int,
    -- 创建时,会自动增加普通索引
    foreign key (class_id) references my_class(id)
);

mysql> show create table my_foreign;
create table `my_foreign` (
  `id` int(11) not null auto_increment,
  `name` varchar(10) collate utf8mb4_general_ci not null,
  `class_id` int(11) default null,
  primary key (`id`),
  key `class_id` (`class_id`),
  constraint `my_foreign_ibfk_1` foreign key (`class_id`) references `my_class` (`id`)
) engine=innodb default charset=utf8mb4 collate=utf8mb4_general_ci


mysql> desc my_foreign;
+----------+-------------+------+-----+---------+----------------+
| field    | type        | null | key | default | extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | no   | pri | null    | auto_increment |
| name     | varchar(10) | no   |     | null    |                |
| class_id | int(11)     | yes  | mul | null    |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mul多索引 外键本身也是索引

示例2: 创建表后增加外键

-- 查看原来的表
mysql> desc my_class;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| name  | varchar(10) | no   | uni | null    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

mysql> show create table my_class;
create table `my_class` (
  `id` int(11) not null auto_increment,
  `name` varchar(10) collate utf8mb4_general_ci not null,
  primary key (`id`),
  unique key `name` (`name`)
) engine=innodb auto_increment=4 default charset=utf8mb4 collate=utf8mb4_general_ci


mysql> desc my_student;
+----------+-------------+------+-----+---------+----------------+
| field    | type        | null | key | default | extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | no   | pri | null    | auto_increment |
| name     | varchar(10) | yes  |     | null    |                |
| class_id | int(11)     | yes  |     | null    |                |
| age      | int(11)     | yes  |     | null    |                |
| gender   | int(11)     | yes  |     | null    |                |
+----------+-------------+------+-----+---------+----------------+

mysql> show create table my_student\g
*************************** 1. row ***************************
       table: my_student
create table: create table `my_student` (
  `id` int(11) not null auto_increment,
  `name` varchar(10) collate utf8mb4_general_ci default null,
  `class_id` int(11) default null,
  `age` int(11) default null,
  `gender` int(11) default null,
  primary key (`id`)
) engine=innodb auto_increment=10 default charset=utf8mb4 collate=utf8mb4_general_ci

-- 增加外键
alter table my_student add constraint fk_class_id foreign key (class_id) references my_class(id);

mysql> show create table my_student\g
*************************** 1. row ***************************
       table: my_student
create table: create table `my_student` (
  `id` int(11) not null auto_increment,
  `name` varchar(10) collate utf8mb4_general_ci default null,
  `class_id` int(11) default null,
  `age` int(11) default null,
  `gender` int(11) default null,
  primary key (`id`),
  key `fk_class_id` (`class_id`),
  constraint `fk_class_id` foreign key (`class_id`) references `my_class` (`id`)
) engine=innodb auto_increment=10 default charset=utf8mb4 collate=utf8mb4_general_ci

1.2、删除外键

外键不允许修改,只能先删除再添加

alter table 从表 drop foreign key 外键名字;

示例:

alter table my_student drop foreign key `fk_class_id`;
mysql> show create table my_student\g
*************************** 1. row ***************************
       table: my_student
create table: create table `my_student` (
  `id` int(11) not null auto_increment,
  `name` varchar(10) collate utf8mb4_general_ci default null,
  `class_id` int(11) default null,
  `age` int(11) default null,
  `gender` int(11) default null,
  primary key (`id`),
  key `fk_class_id` (`class_id`)
) engine=innodb auto_increment=10 default charset=utf8mb4 collate=utf8mb4_general_ci

外键不会删除普通索引,只会删除外键

删除普通索引:

alter table 表名 drop index 索引名字;

1.3、外键的基本要求

  • 外键字段需要与关联的主表的主关键字段类型完全一致
  • 基本属性也要相同
  • 如果是表后增加外键,对数据还有一定的要求(从表数据与主表的关联关系)
  • 外键只能使用innodb存储引擎,myisam不支持

2、外键约束

外键约束:通过建立外键关系后,对主表和从表都会有一定的数据约束效律

2.1、约束的基本概念

当外键产生时,外键所在的表(从表)会受制于主表数据的存在,从而导致数据不能进行某些不符合规范的操作
不能插入主表不存在的数据

如果一张表被其他表外键引入,那么该表的数据操作不能随意,必须保证从表数据的有效性,不能随意删除一个被从表引入的记录

mysql> select * from my_class;
+----+--------+
| id | name   |
+----+--------+
|  1 | 一班   |
|  3 | 三班   |
|  2 | 二班   |
+----+--------+

insert into my_foreign (name, class_id) values ('张飞', 1);
query ok, 1 row affected (0.01 sec)

-- 主表没有id=4的记录,从表不能插入该数据
insert into my_foreign (name, class_id) values ('张飞', 4);
error 1452 (23000): cannot add or update a child row: a foreign key constraint fails (`mydatabase`.`my_foreign`, constraint `my_foreign_ibfk_1` foreign key (`class_id`) references `my_class` (`id`))

mysql> select * from my_foreign;
+----+--------+----------+
| id | name   | class_id |
+----+--------+----------+
|  1 | 张飞   |        1 |
+----+--------+----------+
1 row in set (0.00 sec)

-- 从表中引用了id=1的记录,该数据不能被删除
delete from my_class where id = 1;
error 1451 (23000): cannot delete or update a parent row: a foreign key constraint fails (`mydatabase`.`my_foreign`, constraint `my_foreign_ibfk_1` foreign key (`class_id`) references `my_class` (`id`))

2.2、外键约束的概念

基本语法:

add foreign key (外键字段) references 主表(主键) on 约束模式;

约束模式有三种:

  • district 严格模式 默认,不允许操作
  • cascade 级联模式,一起操作,主表变化,从表的数据也跟着变化
  • set null 置空模式 主表变化(删除),从表对应记录设置,前提是从表中对应的外键字段允许为空

外键约束的主要对象是主表操作,从表就是不能插入主表不存在的数据

通常在进行约束的时候,需要制定操作,update 和 delete

常用的模式:

-- 更新级联, 删除置空, 空格隔开
on update cascade on delete set null;
-- 增加约束模式
alter table my_student
add foreign key (class_id) references my_class(id)
on update cascade on delete set null;
mysql> show create table my_student\g
*************************** 1. row ***************************
       table: my_student
create table: create table `my_student` (
  `id` int(11) not null auto_increment,
  `name` varchar(10) collate utf8mb4_general_ci default null,
  `class_id` int(11) default null,
  `age` int(11) default null,
  `gender` int(11) default null,
  primary key (`id`),
  key `class_id` (`class_id`),
  constraint `my_student_ibfk_1` foreign key (`class_id`) references `my_class` (`id`) on delete set null on update cascade
) engine=innodb auto_increment=10 default charset=utf8mb4 collate=utf8mb4_general_ci

mysql> select * from my_class;
+----+--------+
| id | name   |
+----+--------+
|  1 | 一班   |
|  3 | 三班   |
|  2 | 二班   |
+----+--------+
3 rows in set (0.00 sec)

mysql> select * from my_student;
+----+--------+----------+------+--------+
| id | name   | class_id | age  | gender |
+----+--------+----------+------+--------+
|  1 | 刘备   |        1 |   18 |      2 |
|  2 | 李四   |        1 |   19 |      1 |
|  3 | 王五   |        2 |   20 |      2 |
|  4 | 张飞   |        2 |   21 |      1 |
|  5 | 关羽   |     null |   22 |      2 |
|  6 | 曹操   |        1 |   20 |   null |
+----+--------+----------+------+--------+
6 rows in set (0.00 sec)

-- 更新主表
update my_class set id = 4 where id = 2;

-- 从表中所有class_id=2 的记录被修改为了 class_id=4
mysql> select * from my_student;
+----+--------+----------+------+--------+
| id | name   | class_id | age  | gender |
+----+--------+----------+------+--------+
|  1 | 刘备   |        1 |   18 |      2 |
|  2 | 李四   |        1 |   19 |      1 |
|  3 | 王五   |        4 |   20 |      2 |
|  4 | 张飞   |        4 |   21 |      1 |
|  5 | 关羽   |     null |   22 |      2 |
|  6 | 曹操   |        1 |   20 |   null |
+----+--------+----------+------+--------+

-- 删除主表数据
delete from  my_class where id = 4;

-- 从表中记录class_id=4被修改为class_id=null;
mysql> select * from my_student;
+----+--------+----------+------+--------+
| id | name   | class_id | age  | gender |
+----+--------+----------+------+--------+
|  1 | 刘备   |        1 |   18 |      2 |
|  2 | 李四   |        1 |   19 |      1 |
|  3 | 王五   |     null |   20 |      2 |
|  4 | 张飞   |     null |   21 |      1 |
|  5 | 关羽   |     null |   22 |      2 |
|  6 | 曹操   |        1 |   20 |   null |
+----+--------+----------+------+--------+
6 rows in set (0.00 sec)

2.3、约束的作用

保证数据的完整性,主表与从表的数据要一致,正是因为外键有非常强大的数据约束作用,而且可能导致数据再后台变化的不可控性,导致程序在设计开发逻辑的时候,没有办法很好的把握数据,所以外键很少使用

到此这篇关于mysql数据库外键 foreing key的文章就介绍到这了,更多相关mysql foreing key内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!