简体   繁体   中英

Avoiding trouble with foreign key relationships in MySQL

I met Rasmus Lerdorf at a conference a few months ago. During his talk, one suggestion he made was that we rarely use foreign keys, because if something goes wrong with a foreign key relationship it can lock up your whole database.

Here's what foreign keys enforce that Rasmus warned against: "When one table has a foreign key to another table, the concept of referential integrity states that you may not add a record to the table that contains the foreign key unless there is a corresponding record in the linked table."

On the flip side, foreign keys are great for documentation, staying organized and establishing relationships that save coding leg work.

What can be done to mitigate the risks of foreign keys, while leveraging the benefits?

This question / answer does a great job of laying out both the benefits and risks of foreign keys, but I'm looking for more specific insight on how to avoid the pitfalls yet take advantage of the good.

The database wont lock up, its the badly written application on top of it.

Ofcourse you can take some shortcuts when building application but later on you are more likely than not pull the hair on that inconsistent data you have because of not using FKs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM