What is the difference between using a foreign key and not using a foreign key in a database

Source: Internet
Author: User

Today listened to a corporate technical director of the preaching, the results heard in his development system in the process, did not use the foreign key, which surprised me, hurriedly search the Internet for some information to see, and finally understand the reasons for the use of foreign keys.
This is a discussion about the use of foreign keys, which makes sense:
For the primary/foreign key/index, in some development teams is considered to be a tool to deal with database relations, but also by some development team is considered to deal with some specific business of the devil, your point of view? What kind of approach do you take in real-world applications?
Common point of view: The primary key and index is not few, not only can optimize the speed of data retrieval, developers also save no other work,
Conflicting focus: Whether the database design requires a foreign key. Here are two questions: one is how to ensure the integrity and consistency of database data, and the second is the effect of the first on performance.

Affirmative view:

1, by the database itself to ensure data consistency, integrity, more reliable, because the program is difficult to 100% to ensure the integrity of data, and foreign keys even when the database server or other problems, the data can be guaranteed to the maximum consistency and integrity. Eg: database and application is a one-to-many relationship, a application will maintain the integrity of his part of the data, when the system becomes larger, the B application is added, A and b two applications may be different development team to do. How do they coordinate the integrity of the data, and if the C application is added a year later?

2, the database design with the primary foreign key can increase the readability of the ER graph, which is very important in database design.

3, the foreign key to a certain extent the business logic, will make the design thoughtful and concrete comprehensive.

Opposing views:

1, you can use a trigger or application to ensure data integrity

2, excessive emphasis or use of the primary key/foreign key will add to the development difficulty, resulting in too many problems such as the table

3, without foreign key data management is simple, easy to operate, high performance (import and export operations, in the INSERT, UPDATE, delete data faster) eg: in a huge database to think about the foreign key, imagine, a program to insert millions of records per day, When there are foreign key constraints, every time to scan this record is qualified, generally more than one field has a foreign key, so the number of scans is a series of growth! One of my program storage in 3 hours to finish, if you add foreign keys, it takes 28 hours!

Conclusion:

1, in large-scale systems (performance requirements are not high, high security requirements), the use of foreign keys, in large-scale systems (high performance requirements, security control), no foreign key; small system, the best use of foreign keys.

2, use the foreign key to be appropriate, can not be excessively pursued

3, without foreign keys to control data consistency and integrity, you should write a layer to ensure that then each application through this layer to access the database.

What is the difference between using a foreign key and not using a foreign key in a database

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.