在定义外键可以对外键进行update 和delete 时对字表进行的操作

mysql> create table parent(-> id int primary key);Query OK, 0 rows affected (0.01 sec)mysql> create table child (id int,parent_id int,foreign key(parent_id) references parent(id) on delete set null);Query OK, 0 rows affected (0.02 sec)mysql> insert into parent values (1),(2),(3);Query OK, 3 rows affected (0.00 sec)Records: 3 Duplicates: 0 Warnings: 0mysql> insert into child values(1,1),(2,2),(3,3);Query OK, 3 rows affected (0.01 sec)Records: 3 Duplicates: 0 Warnings: 0mysql> delete from parent where id=1;Query OK, 1 row affected (0.01 sec)mysql> select * from child;+------+-----------+| id | parent_id |+------+-----------+| 1 | NULL || 2 | 2 || 3 | 3 |+------+-----------+3 rows in set (0.00 sec)mysql>
