当前位置:网站首页>Why do we seldom use foreign keys?

Why do we seldom use foreign keys?

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

background

At present, many companies do not consider adding foreign keys , Many people don't use foreign keys much at work , After all, 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 my opinion , In the corresponding scenario , If you use a feature that does more harm than good , You may not choose . So whether to use foreign keys depends on the corresponding scenario , You can't abandon foreign keys completely .

Why do you rarely use foreign keys ?

There are some inconveniences in using foreign keys at present , That's why most people don't use it . I specifically summarized the following reasons :

1、 Increase database pressure

A foreign key is the implementation of a consistent transaction of data , All to the database server to complete , And you have foreign keys , As some additions involving foreign key fields 、 Delete 、 After update operation , You need to trigger related operations to check , Resources have to be consumed .

2、 The deadlock problem

If it is a high concurrency and high traffic transaction scenario , Using foreign keys can also cause deadlock .

3、 Development is not convenient

When there are foreign keys , Both development and maintenance , When data needs to be maintained manually , It's not very convenient , Consider cascading factors .

4、 Data brush Library

In the actual development process , It is inevitable to brush the library frequently , Import or delete data , The existence of a foreign key will cause the table deletion or insertion to fail ( Table data change has sequence requirements ).

5、 Foreign key maintenance

As the project iterates , The relationship between tables also changes , For example, a foreign key needs to be deleted ; Or to add a foreign key , However, due to the inaccuracy of the existing data , The foreign key cannot be added , in the course of time ,ER Graphs also do not completely show table relationships , this ER Picture or not ?

Are foreign keys really useless ?

Existence is reason , Every product , Since every function exists , There are also advantages in some places . The author summarizes several advantages of foreign keys :

1、 Database consistency

Data consistency is guaranteed by the database itself 、 Integrity will be more reliable , The program is difficult 100% Ensure data consistency 、 integrity , Using foreign keys, even when the database server crashes or other problems occur , It can also ensure the consistency and integrity of data to the greatest extent .

2、ER Figure reliability

Database design with main foreign key can increase ER Readability of graphs ( There are also special company products that pay special attention to this aspect ).

summary

1、 If it is a single machine with low concurrency , No performance tuning is required , Or the consistency of the data cannot be guaranteed by the program , integrity , You can use foreign keys , As in school , Ensure data consistency and integrity , The base number is not big .

2、 For high concurrency , Distributed , Make the system performance better , And better maintenance , Do not use foreign keys .

原网站

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