Do you absolutely need foreign keys in a database?

后端 未结 11 986
遥遥无期
遥遥无期 2021-02-12 20:31

I was wondering how useful foreign keys really are in a database. Essentially, if the developers know what keys the different tables depend on, they can write the queries just

11条回答
  •  生来不讨喜
    2021-02-12 21:07

    Foreign keys are invaluable as a means of ensuring integrity, and even if you trust your developers to never (!) make errors the cost of having them is usually well worth it.

    Foreign keys also serve as documentation, in that you can see what relates to what. This information is typically also used by tools, such as for generating reports, creating data sets from table definitions, object-relational mappers, etc. Even if you do not use any of these today, having FKs will make it easier to tread that path later.

    Foreign keys also allow you to define cascade rules, which e.g. can be used to to delete associated records in related tables when a row in one table is deleted.

    Only if you have ridiculously high loads should you consider bypassing FKs.

    Edit: updated answer to include points from other answers (reports, cascades).

提交回复
热议问题