Three ways to constrain foreign keys

Source: Internet
Author: User

If Table A's primary key is a field in table B, the field is called the Foreign key of Table B, table A is called the primary table, and table B is called from the table. Foreign keys are used to achieve referential integrity, and different foreign key constraints will allow the two tables to be tightly combined, especially if the modified or deleted cascade operation will make routine maintenance easier. Here we take MySQL as an example to summarize the differences and connections between the 3 foreign key constraints.

Here, for example, the user and User groups table, this is a typical many-to-one relationship where multiple users correspond to a single user group.
First create the User Group table:

SQL code
    1. Create table T_group (
    2. ID int not null,
    3. name varchar (+),
    4. primary key (ID)
    5. );
CREATE TABLE T_group (ID int not null,name varchar (), primary key (ID));



and insert two records:

SQL code
    1. Insert INTO t_group values (1, ' Group1 ');
    2. Insert INTO t_group values (2, ' Group2 ');
INSERT into T_group values (1, ' Group1 ') and insert into T_group values (2, ' Group2 ');



Create a user table below to create a foreign key reference relationship with different constraints:
1. Cascade (CASCADE) mode

SQL code
  1. Create table T_user (
  2. ID int not null,
  3. name varchar (+),
  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 (), GroupID int,primary key (ID), foreign key (GroupID) references T_grou P (ID) on the DELETE cascade on UPDATE cascade);



Referential integrity Testing

SQL code
    1. insert into t_user  values  (1,  ' qianxin ',  1);  --can be inserted    
    2. insert into t_user values   (2,  ' Yiyu ',  2);     --can be inserted     
    3. insert  into t_user values  (3, --error, unable to insert, user Group 3 does not exist, inconsistent with referential integrity constraints   
INSERT into T_user values (1, ' qianxin ', 1); --INSERT into t_user values (2, ' Yiyu ', 2) can be inserted;    --INSERT into t_user values (3, ' Dai ', 3) can be inserted;    --Error, unable to insert, user Group 3 does not exist, does not match referential integrity constraint



Constraint mode test

SQL code
  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; --Causes 2, 3 record cascade deletions in T_user
  5. Update T_group set id=2 where id=1; --GroupID cascade modification of 1 records in T_user to 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);d elete from T_group where id=2;              --Results in T_user 2, 3 record cascade Delete Update T_group set id=2 where id=1;          --GroupID cascade modification of 1 records in T_user to 2



2. Empty (set NULL) mode

SQL code
  1. Empty mode
  2. Create table T_user (
  3. ID int not null,
  4. name varchar (+),
  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. Referential integrity Testing
  10. Insert INTO t_user values (1, ' qianxin ', 1); --can be inserted
  11. Insert INTO t_user values (2, ' Yiyu ', 2); --can be inserted
  12. Insert INTO t_user values (3, ' Dai ', 3); --Error, unable to insert, user Group 3 does not exist, does not match referential integrity constraint
  13. Constraint mode test
  14. Insert INTO t_user values (1, ' qianxin ', 1);
  15. Insert INTO t_user values (2, ' Yiyu ', 2);
  16. Insert INTO t_user values (3, ' Dai ', 2);
  17. Delete from t_group where id=2; --Causes the GroupID of 2, 3 records in T_user to be set to null
  18. Update T_group set id=2 where id=1; --The groupid that caused the 1 record in T_user is set to null
Empty mode CREATE TABLE T_user (ID int not null,name varchar (), GroupID int,primary key (ID), foreign key (GroupID) references T_ Group (ID) on delete set NULL on update set NULL); Referential integrity test INSERT INTO T_user values (1, ' qianxin ', 1); --INSERT into t_user values (2, ' Yiyu ', 2) can be inserted;    --INSERT into t_user values (3, ' Dai ', 3) can be inserted;     --Error, unable to insert, user Group 3 does not exist, inconsistent with referential integrity constraints test INSERT INTO T_user values (1, ' qianxin ', 1); INSERT into t_user values (2, ' Yiyu ', 2); ins ert into T_user values (3, ' Dai ', 2);d elete from T_group where id=2;              --Causes the GroupID of 2, 3 records in T_user to be set to Nullupdate T_group set id=2 where id=1;          --the GroupID that caused the 1 record in T_user is set to null




3. Prohibit (no action/restrict) mode

SQL code
    1. Prohibited mode
    2. Create table T_user (
    3. ID int not null,
    4. name varchar (+),
    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. Referential integrity Testing
    10. Insert INTO t_user values (1, ' qianxin ', 1); --can be inserted
    11. Insert INTO t_user values (2, ' Yiyu ', 2); --can be inserted
    12. Insert INTO t_user values (3, ' Dai ', 3); --Error, unable to insert, user Group 3 does not exist, does not match referential integrity constraint
    13. Constraint mode test
    14. Insert INTO t_user values (1, ' qianxin ', 1);
    15. Insert INTO t_user values (2, ' Yiyu ', 2);
    16. Insert INTO t_user values (3, ' Dai ', 2);
    17. Delete from t_group where id=2; --Error, there is a correlation reference from the table, so the primary table cannot be deleted
    18. Update T_group set id=2 where id=1; --error, related reference from table, so cannot be modified in main table

Three ways to constrain foreign keys

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.