[Posts] database, only with the foreign key constraint sucker!

Database, only sucker with a foreign key constraint!

Author: Lonely smoke

From: Daza ZRJ

introduction

In fact, this topic is commonplace, many people do not use foreign keys in their work. Including this one also has the following specifications in Ali's JAVA

[] Shall not be used to force a foreign key with a cascade, all foreign key concepts that must be addressed in the application layer. 

But then, ask them why, most of them answered in this

Every time DELETE, or UPDATE must be considered a foreign key constraint, it will lead to the development of very painful time, the test data is extremely inconvenient.

Frankly, say is right. But it is not comprehensive enough, so open a paper to detail.

text

First, we make it clear that the foreign key constraint is a constraint, the existence of this constraint, will ensure that the relationship between the table data "is always full." Thus, foreign key constraints, not entirely without merit.
Such as foreign keys, can be

  • Ensure the integrity and consistency of the data

  • Cascading easy to operate

  • Analyzing the integrity of the data entrusted to the database is complete, the code size is reduced

However, fish and bear's paw can not have both. A foreign key is to ensure the integrity of the data, but the system will bring a lot of flaws. Because of these shortcomings, only lead us to recommend the use of foreign keys, as follows

Performance issues

Assume a table called user_tb. Then this table has two foreign key fields, pointing to two tables. So, each time to user_tb table insert data, it is necessary to two foreign key table corresponding to the query whether there are corresponding data. If handed over control of the program, such a query process can be controlled in our hands, you can omit some unnecessary query process. But if controlled by a database, you must go to these two judges table.

Concurrency issues

In the case of using the foreign key, each modification of data needs to go to another check table data, need to acquire additional locks. If the high concurrent high-volume transaction scenarios, using foreign keys more likely to cause a deadlock.

Scalability issues

This is mainly divided into two

  • Make convenient platform migration, such as you from Mysqlmoving to Oracle, like triggers, foreign keys this kind of thing, you can take advantage of features of the framework itself to achieve, rather than rely on the characteristics of the database itself, make the migration easier.

  • Convenient sub-library sub-table, in the case of a horizontal split and sub-libraries, the foreign key is not in force. Will maintain the relationship between the data and put it into the application, eliminating a lot of trouble for the future of sub-library sub-table.

technical problem

Foreign key, in fact, the determination logic of the application to be executed is transferred to the database. Then this means that the performance overhead of a database becomes larger, then the DBA which requires even higher. Many small and medium sized companies because of funding problems, and did not hire a professional DBA, so they will choose to do foreign key, reduce the consumption of the database.
Conversely, if the constraint logic in the application, the application server performance not found, the machine can be added, to make the horizontal extension. If you are on the database server, the database server can be a performance bottleneck, do horizontal expansion more difficult.

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11764458.html