当前位置:网站首页>Why do we not use foreign keys now (2)?

Why do we not use foreign keys now (2)?

2022-06-22 23:49:00 Kunlunbase Kunlun database

Preface

MySQL Foreign keys (FOREIGNKEY) Is a special field of the table , Used to establish the relationship between the master table and the slave table , Establish a connection between the data of two tables , Constrains the consistency and integrity of data in two tables .

Previous first article related articles (「 Technical discussion 」 Why do you seldom use foreign keys ?), Summarizes some scenarios that do not use foreign keys and the advantages of using foreign keys .

This article will illustrate, based on examples, that foreign key constraints can guarantee the data relationship between tables “ Always complete and consistent ”, But in practice , Every time I do DELETE perhaps UPDATE Must consider foreign key constraints , It can lead to painful development .

Keep data consistent and complete , It is mainly reflected in the following two aspects :

Prevent execution

  • Insert new row from table , If the foreign key value is not the primary key value of the primary table, the insertion is prevented ;

  • Modify foreign key values from the table , If the new value is not the primary key value of the primary table, the modification is prevented ;

  • Main table delete row , Its primary key value cannot be deleted if it exists in the secondary table ( To delete , You must first delete the related rows from the table );

  • Modify the primary key value in the main table , Old values that exist from the table prevent modification ( To modify , You must first delete the related rows from the table ).

Cascade execution

  • Main table delete row , Delete the related rows of the table together ;

  • Modify the primary key value in the main table , Modify it together with the foreign key values of the related rows of the table .

Let's take an example to illustrate :

1. Create two table,“ The coach ” and “ student ”,table“ student ” With foreign key constraints .

CREATE TABLE jiaolian(
   `jiaolian_id` INT AUTO_INCREMENT,
   `jiaolian_name` VARCHAR(30),
   PRIMARY KEY (`jiaolian_id`));

CREATE TABLE xueyuan(
   `xueyuan_id` INT AUTO_INCREMENT,
   `jiaolian_id` INT,
   `xueyuan_name` VARCHAR(30),
   FOREIGN KEY (`jiaolian_id`) REFERENCES `jiaolian` (`jiaolian_id`),
   PRIMARY KEY (`xueyuan_id`));

2.  Add two “ The coach ”, Two “ student ”.


insert into jiaolian(jiaolian_name) values(" Driver Lao Li ");
insert into jiaolian(jiaolian_name) values(" Driver Lao Lin ");
insert into xueyuan(jiaolian_id,xueyuan_name) values(1," Disciple Zhang San ");
insert into xueyuan(jiaolian_id,xueyuan_name) values(2," Disciple Li Si ");

3. Add another “ student ”, There is no the jiaolian_id, Execution failed .

insert into xueyuan(jiaolian_id,xueyuan_name) values(3," Disciple Wang Wu ");  #error

4.  Delete one “ The coach ”,“ The coach ” Related “ student ”, Execution failure .

delete from jiaolian where jiaolian_name=" Driver Lao Li ";  #error

5.  Delete first “ The coach ” The associated “ student ”, And then delete “ The coach ”, Successful implementation

delete from xueyuan where xueyuan_name=" Disciple Zhang San ";
delete from jiaolian where jiaolian_name=" Driver Lao Li ";

Conclusion

Through this example , We can see , Foreign key constraints guarantee the relationship of data between tables “ Always complete and consistent ”.

Although the data consistency and integrity judgment is entrusted to the database , Reduces the amount of code in the program . But in practice , Every time I do DELETE perhaps UPDATE Must consider foreign key constraints , It can lead to painful development , The test data is extremely inconvenient .

In the case of using foreign keys , Every time you modify the data, you need to check the data in another table , Need to acquire additional locks .

In high concurrency and high traffic transaction scenarios , Using foreign keys can easily cause deadlock , And the bottleneck of database resources , Therefore, it is not recommended to use in general Internet industry with high frequency and concurrency .

Generally speaking , When implemented in business code , As long as the application layer follows the inherent correlation logic at the beginning of the design , To process the data , There is no need for foreign key constraints at the database level .

KunlunDB The project is open source

【GitHub:】
https://github.com/zettadb

【Gitee:】
https://gitee.com/zettadb

END

原网站

版权声明
本文为[Kunlunbase Kunlun database]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222122589450.html