what are the advantages of defining a foreign key

前端 未结 5 1378

What is the advantage of defining a foreign key when working with an MVC framework that handles the relation?

I\'m using a relational database with a framework that

5条回答
  •  余生分开走
    2020-12-15 16:18

    The main benefits are data integrity and cascading deletes. You can also get a performance gain when they're defined and those fields are properly indexed. For example, you wouldn't be able to create a phone number that didn't belong to a contact, or when you delete the contact you can set it to automatically delete all of their phone numbers. Yes, you can make those connections in your UI or middle tier, but you'll still end up with orphans if someone runs an update directly against the server using SQL rather than your UI. The "hassle" part is just forcing you to consider those connections before you make a bulk change. FKs have saved my bacon many times.

提交回复
热议问题