外鍵約束的三種方式


如果表A的主關鍵字是表B中的字段,則該字段稱為表B的外鍵,表A稱為主表,表B稱為從表。外鍵是用來實現參照完整性的,不同的外鍵約束方式將可以使兩張表緊密的結合起來,特別是修改或者刪除的級聯操作將使得日常的維護工作更加輕松。這里以MySQL為例,總結一下3種外鍵約束方式的區別和聯系。 

     這里以用戶表和用戶組表為例,這是一個典型的多對一關系,多個用戶對應於一個用戶組。 
     首先創建用戶組表: 

Sql代碼 
  1. create table t_group (   
  2.     id int not null,   
  3.     name varchar(30),   
  4.     primary key (id)   
  5. );  
create table t_group (id int not null,name varchar(30),primary key (id));



並插入兩條記錄: 

Sql代碼 
  1. insert into t_group values (1, 'Group1');   
  2. insert into t_group values (2, 'Group2');  
insert into t_group values (1, 'Group1');insert into t_group values (2, 'Group2');



下面創建用戶表,分別以不同的約束方式創建外鍵引用關系: 
1、級聯(cascade)方式 

Sql代碼 
  1. create table t_user (   
  2.     id int not null,   
  3.     name varchar(30),   
  4.     groupid int,   
  5.     primary key (id),   
  6.     foreign key (groupid) references t_group(id) on delete cascade on update cascade  
  7. );  
create table t_user (id int not null,name varchar(30),groupid int,primary key (id),foreign key (groupid) references t_group(id) on delete cascade on update cascade);



參照完整性測試 

Sql代碼 
  1. insert into t_user values (1, 'qianxin', 1); --可以插入   
  2. insert into t_user values (2, 'yiyu', 2);    --可以插入   
  3. insert into t_user values (3, 'dai', 3);    --錯誤,無法插入,用戶組3不存在,與參照完整性約束不符  
insert into t_user values (1, 'qianxin', 1); --可以插入insert into t_user values (2, 'yiyu', 2);    --可以插入insert into t_user values (3, 'dai', 3);    --錯誤,無法插入,用戶組3不存在,與參照完整性約束不符



約束方式測試 

Sql代碼 
  1. insert into t_user values (1, 'qianxin', 1);   
  2. insert into t_user values (2, 'yiyu', 2);   
  3. insert into t_user values (3, 'dai', 2);   
  4. delete from t_group where id=2;              --導致t_user中的2、3記錄級聯刪除   
  5. update t_group set id=2 where id=1;          --導致t_user中的1記錄的groupid級聯修改為2  
insert into t_user values (1, 'qianxin', 1);insert into t_user values (2, 'yiyu', 2);insert into t_user values (3, 'dai', 2);delete from t_group where id=2;              --導致t_user中的2、3記錄級聯刪除update t_group set id=2 where id=1;          --導致t_user中的1記錄的groupid級聯修改為2



2、置空(set null)方式 

Sql代碼 
  1. 置空方式   
  2. create table t_user (   
  3.     id int not null,   
  4.     name varchar(30),   
  5.     groupid int,   
  6.     primary key (id),   
  7.     foreign key (groupid) references t_group(id) on delete set null on update set null  
  8. );   
  9.   
  10. 參照完整性測試   
  11.   
  12. insert into t_user values (1, 'qianxin', 1); --可以插入   
  13. insert into t_user values (2, 'yiyu', 2);    --可以插入   
  14. insert into t_user values (3, 'dai', 3);     --錯誤,無法插入,用戶組3不存在,與參照完整性約束不符   
  15.   
  16. 約束方式測試   
  17. insert into t_user values (1, 'qianxin', 1);   
  18. insert into t_user values (2, 'yiyu', 2);   
  19. insert into t_user values (3, 'dai', 2);   
  20. delete from t_group where id=2;              --導致t_user中的2、3記錄的groupid被設置為NULL   
  21. update t_group set id=2 where id=1;          --導致t_user中的1記錄的groupid被設置為NULL  
置空方式create table t_user (id int not null,name varchar(30),groupid int,primary key (id),foreign key (groupid) references t_group(id) on delete set null on update set null);參照完整性測試insert into t_user values (1, 'qianxin', 1); --可以插入insert into t_user values (2, 'yiyu', 2);    --可以插入insert into t_user values (3, 'dai', 3);     --錯誤,無法插入,用戶組3不存在,與參照完整性約束不符約束方式測試insert into t_user values (1, 'qianxin', 1);insert into t_user values (2, 'yiyu', 2);insert into t_user values (3, 'dai', 2);delete from t_group where id=2;              --導致t_user中的2、3記錄的groupid被設置為NULLupdate t_group set id=2 where id=1;          --導致t_user中的1記錄的groupid被設置為NULL




3、禁止(no action / restrict)方式 

Sql代碼 
    1. 禁止方式   
    2. create table t_user (   
    3.     id int not null,   
    4.     name varchar(30),   
    5.     groupid int,   
    6.     primary key (id),   
    7.     foreign key (groupid) references t_group(id) on delete no action on update no action  
    8. );   
    9.   
    10. 參照完整性測試   
    11. insert into t_user values (1, 'qianxin', 1); --可以插入   
    12. insert into t_user values (2, 'yiyu', 2);    --可以插入   
    13. insert into t_user values (3, 'dai', 3);     --錯誤,無法插入,用戶組3不存在,與參照完整性約束不符   
    14.   
    15. 約束方式測試   
    16. insert into t_user values (1, 'qianxin', 1);   
    17. insert into t_user values (2, 'yiyu', 2);   
    18. insert into t_user values (3, 'dai', 2);   
    19. delete from t_group where id=2;              --錯誤,從表中有相關引用,因此主表中無法刪除   
    20. update t_group set id=2 where id=1;          --錯誤,從表中有相關引用,因此主表中無法修改  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM