MySQL 外來鍵約束

2021-07-01 23:35:56 字數 3629 閱讀 8040

-- 建立測試主表. id 是主鍵.

create table test_main (

id      int,

value   varchar(10),

primary key(id)

);-- 建立測試子表.

create table test_sub (

id      int,

main_id int,

value   varchar(10),

primary key(id)

);-- 插入測試主表資料.

insert into test_main(id, value) values (1, 'one');

insert into test_main(id, value) values (2, 'two');

-- 插入測試子表資料.

insert into test_sub(id, main_id, value) values (1, 1, 'oneone');

insert into test_sub(id, main_id, value) values (2, 2, 'twotwo');

缺省外鍵約束方式

mysql> alter table test_sub

->   add constraint main_id_cons

->   foreign key (main_id)

->   references  test_main(id);

-> //

query ok, 2 rows affected (0.17 sec)

records: 2  duplicates: 0  warnings: 0

mysql> delete from

->   test_main

-> where

->   id = 1;

-> //

error 1451 (23000): cannot delete or update a parent row: a foreign key constrai

nt fails (`test`.`test_sub`, constraint `main_id_cons` foreign key (`main_id`) r

eferences `test_main` (`id`))

mysql使用下面這個語句刪除外來鍵約束

alter table test_sub drop foreign key  main_id_cons;

delete cascade 方式

mysql> alter table test_sub

->   add constraint main_id_cons

->   foreign key (main_id)

->   references  test_main(id)

->   on delete cascade//

query ok, 2 rows affected (0.16 sec)

records: 2  duplicates: 0  warnings: 0

mysql> delete from

->   test_main

-> where

->   id = 1;

-> //

query ok, 1 row affected (0.02 sec)

mysql> select

->   *

-> from

->   test_sub;

-> //

+----+---------+--------+

| id | main_id | value  |

+----+---------+--------+

|  2 |       2 | twotwo |

+----+---------+--------+

1 row in set (0.00 sec)

mysql使用下面這個語句刪除外來鍵約束

alter table test_sub drop foreign key  main_id_cons;

update cascade方式

mysql> alter table test_sub

->   add constraint main_id_cons

->   foreign key (main_id)

->   references  test_main(id)

->   on update cascade;

-> //

query ok, 1 row affected (0.14 sec)

records: 1  duplicates: 0  warnings: 0

mysql> update test_main set id = 5 where id = 2

-> //

query ok, 1 row affected (0.01 sec)

rows matched: 1  changed: 1  warnings: 0

mysql> select * from test_sub//

+----+---------+--------+

| id | main_id | value  |

+----+---------+--------+

|  2 |       5 | twotwo |

+----+---------+--------+

1 row in set (0.00 sec)

mysql使用下面這個語句刪除外來鍵約束

alter table test_sub drop foreign key  main_id_cons;

set null方式

mysql> alter table test_sub

->   add constraint main_id_cons

->   foreign key (main_id)

->   references  test_main(id)

->   on delete set null;

-> //

query ok, 1 row affected (0.13 sec)

records: 1  duplicates: 0  warnings: 0

mysql> delete from

->   test_main

-> where

->   id = 5;

-> //

query ok, 1 row affected (0.05 sec)

mysql>

mysql> select

->   *

-> from

->   test_sub;

-> //

+----+---------+--------+

| id | main_id | value  |

+----+---------+--------+

|  2 |    null | twotwo |

+----+---------+--------+

1 row in set (0.00 sec)

mysql約束與外來鍵 MySQL 外來鍵與約束

外來鍵的建立 建表如下 create table parent id int not null,primary key id type innodb create table child id int,parent id int,foreign key parent id references pa...

MySQL外來鍵約束

innodb型別表有乙個其他儲存引擎不支援的特性 外來鍵約束。當b表的外來鍵關聯到a表的主鍵時 表b是父表a表的子表 如果刪除了a表,那麼b中的外來鍵則仍然關聯著乙個不存在的表的主鍵。而外鍵約束則設定了當約束被破壞時應該應用的的規則,包括防止約束破壞。建立乙個外來鍵約束的語法是 foreign ke...

My SQL外來鍵約束

外來鍵約束對子表的含義 如果在父表中找不到對應的候選鍵,則不能對子表進行insert update操作 外來鍵約束對父表的含義 在父表上進行update delete以更新或刪除在子表中有一條或多條對應匹配行的候選鍵時,父表的行為取決於 在定義子表的外來鍵時指定的on update on delet...