Recently work used to PowerDesigner, the early need through the PowerDesigner to generate table structure, and later because of the staff is not, many table structure add not synchronized to PowerDesigner, a checklist structure is really troublesome, then thought to reverse generation model, However, there is no foreign key relationship between the database and the resulting model does not have a foreign key. Curiosity about not using foreign keys in the project then asked the relevant personnel reasons, and a simple understanding of the characteristics of the foreign key and the use of the scene. In order to further understand, found on the Internet related articles below for the reproduced original link:
Http://www.cnblogs.com/pengxl/archive/2010/06/11/1756346.html
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 whether to use foreign keys, it makes sense:
for primary/foreign key/index, in some development teams is considered to be a tool to deal with database relations, 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 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: database design requires foreign keys. 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, the database itself guarantees data consistency, integrity, and more reliable, because the program is difficult to 100% guarantee the integrity of the data, The foreign key can ensure the consistency and completeness of data even when the database server or other problems occur. 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 important in database design.
3, the foreign key to a certain extent the business logic, will make the design thoughtful and comprehensive.
反方观点:
1,Data integrity can be guaranteed with a trigger or application
2,Too much emphasis or use of the primary key/foreign key will add to the difficulty of development, resulting in too many problems such as table
3,No foreign key when the 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 do not want to think about foreign keys, 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!
结论: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, with the foreign key to be appropriate, can not over-pursue 3, without foreign keys and program control data consistency and integrity, you should write a layer to ensure, and then each application through this layer to access the database.
Database in the end with no foreign key