mysql如何處理外碼約束

2022-03-02 19:21:41 字數 3629 閱讀 1540

-- 建立測試主表. 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如何處理外碼約束

建立測試主表.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 k...

MySQL如何處理死鎖

官方定義如下 兩個事務都持有對方需要的鎖,並且在等待對方釋放,並且雙方都不會釋放自己的鎖。這個就好比你有乙個人質,對方有乙個人質,你們倆去談判說換人。你讓對面放人,對面讓你放人。看到這裡,也許你會有這樣的疑問,事務和談判不一樣,為什麼事務不能使用完鎖之後立馬釋放呢?居然還要操作完了之後一直持有鎖?這...

mysql取消外碼約束 MySQL刪除外來鍵約束問題

當我們在乙個表中新增字段約束的時候 alter table product add constraint product fk foreign key category id references category id 會認為我的外來鍵約束名稱為product fk,然後當我們想刪除時 alter...