时间:2021-07-01 10:21:17 帮助过:3人阅读
主键和外键就像粘合剂,能够将多个表联系起来。
创建外键的条件:
1)存储引擎是innodb
2)相关联字段数据类型要一致
3)最好在外键列上建索引(目的就是为了减小扫描范围,不创建也可以,只是影响性能)
例子:
dept:部门表
emp :员工表
mysql> create table dept (dno int,dname char(10),primary key (dno));
mysql> create table emp (eno int,e_dno int,ename char(15),index(e_dno),foreign key (e_dno) references dept(dno));
向父表中插入数据
mysql> insert into dept values(1,‘sa‘),(2,‘dba‘),(3,‘manager‘);
向子表中插入数据
mysql> select * from dept;
+-----+---------+
| dno | dname |
+-----+---------+
| 1 | sa |
| 2 | dba |
| 3 | manager |
+-----+---------+
3 rows in set (0.00 sec)
mysql> insert into emp values(100,3,‘Tom‘);
mysql> select * from emp;
+------+-------+-------+
| eno | e_dno | ename |
+------+-------+-------+
| 100 | 3 | Tom |
+------+-------+-------+
1 row in set (0.00 sec)
mysql> insert into emp values(101,4,‘Mary‘); //反例:插入父表中不存在的部门号
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`up1`.`emp`, CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`e_dno`) REFERENCES `dept` (`dno`))
mysql> delete from dept where dno=2;
mysql> delete from dept where dno=3;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`up1`.`emp`, CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`e_dno`) REFERENCES `dept` (`dno`))
小结:
1)子表中的数据依赖于父表,不能向子表中插入父表中不存在值
2)不能删除父表中被子表所依赖的记录
删除父表中被依赖的行的方法:
1)删除外键约束
2)指定级联操作的选项
on delete cascade:级联删除
on update cascade:级联更新
mysql> drop table emp;
mysql> create table emp (eno int,e_dno int,ename char(15),index(e_dno),foreign key (e_dno) references dept(dno) on delete cascade on update cascade); //完整的外键创建
mysql> insert into emp values(100,1,‘Tom‘),(101,3,‘Mary‘),(103,1,‘Jack‘);
mysql> select * from dept;
+-----+---------+
| dno | dname |
+-----+---------+
| 1 | sa |
| 3 | manager |
+-----+---------+
2 rows in set (0.00 sec)
mysql> select * from emp;
+------+-------+-------+
| eno | e_dno | ename |
+------+-------+-------+
| 100 | 1 | Tom |
| 101 | 3 | Mary |
| 103 | 1 | Jack |
+------+-------+-------+
3 rows in set (0.00 sec)
mysql> delete from dept where dno=1;
Query OK, 1 row affected (0.02 sec)
mysql> select * from emp;
+------+-------+-------+
| eno | e_dno | ename |
+------+-------+-------+
| 101 | 3 | Mary |
+------+-------+-------+
1 row in set (0.00 sec)
mysql> update dept set dno=100 where dno=3;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from emp;
+------+-------+-------+
| eno | e_dno | ename |
+------+-------+-------+
| 101 | 100 | Mary |
+------+-------+-------+
1 row in set (0.00 sec)
有了级联删除和级联修改选项,父表中的数据发生删除或者更新时,子表中相关数据也会发生相应的变化。
删除外键
alter table 表名 drop foreign key 外键的名字
mysql> show create table emp \G //红色字体为外键的名字
*************************** 1. row ***************************
Table: emp
Create Table: CREATE TABLE `emp` (
`eno` int(11) DEFAULT NULL,
`e_dno` int(11) DEFAULT NULL,
`ename` char(15) DEFAULT NULL,
KEY `e_dno` (`e_dno`),
CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`e_dno`) REFERENCES `dept` (`dno`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql> alter table emp drop foreign key emp_ibfk_1;
mysql> show create table emp \G
*************************** 1. row ***************************
Table: emp
Create Table: CREATE TABLE `emp` (
`eno` int(11) DEFAULT NULL,
`e_dno` int(11) DEFAULT NULL,
`ename` char(15) DEFAULT NULL,
KEY `e_dno` (`e_dno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
MySQL约束
标签:mysql