MySQL foreign keys - always a good idea?
Are MySQL foreign keys always a good idea? I mean if the field I'm using is required for every row and it references another table, should I 100% of the time use a foreign key? It seems to me like I would, and it seems like I'd always want to setup ON DELETE CASCADE开发者_JS百科
as well to maintain data integrity.
If you have a relationship then it's a good idea to setup your foreign keys. Foreign keys are sort of a key component of relational databases. However, you don't necessarily need cascading deletes. Simply by implementing your foreign keys you'll maintain data integrity. Any delete will fail when conflicting with your foreign key constraints.
Foreign keys are the relations of a Relational Database. You should use them whenever you can in an OLTP system.
精彩评论