what are the advantages of defining a foreign key

前端 未结 5 1372

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:36

    It gives you data integrity that's enforced at the database level. This helps guard against possibly error in application logic that might cause invalid data.

    If any data manipulation is ever done directly in SQL that bypasses your application logic, it also guards against bad data that breaks those constraints.

    An additional side-benefit is that it allows tools to automatically generating database diagrams with relationships inferred from the schema itself. Now in theory all the diagramming should be done before the database is created, but as the database evolves beyond its initial incarnation these diagrams often aren't kept up to date, and the ability to generate a diagram from an existing database is helpful both for reviewing, as well as for explaining the structure to new developers joining a project.

    It might be a helpful to disable FKs while the database structure is still in flux, but they're good safeguard to have when the schema is more stabilized.

提交回复
热议问题