Are foreign keys really necessary in a database design?

前端 未结 24 2169
执笔经年
执笔经年 2020-11-28 18:08

As far as I know, foreign keys (FK) are used to aid the programmer to manipulate data in the correct way. Suppose a programmer is actually doing this in the right manner alr

相关标签:
24条回答
  • 2020-11-28 18:21

    They are not strictly necessary, in the way that seatbelts are not strictly necessary. But they can really save you from doing something stupid that messes up your database.

    It's so much nicer to debug a FK constraint error than have to reconstruct a delete that broke your application.

    0 讨论(0)
  • 2020-11-28 18:23

    FKs are very important and should always exist in your schema, unless you are eBay.

    0 讨论(0)
  • 2020-11-28 18:24

    Suppose a programmer is actually doing this in the right manner already

    Making such a supposition seems to me to be an extremely bad idea; in general software is phenomenally buggy.

    And that's the point, really. Developers can't get things right, so ensuring the database can't be filled with bad data is a Good Thing.

    Although in an ideal world, natural joins would use relationships (i.e. FK constraints) rather than matching column names. This would make FKs even more useful.

    0 讨论(0)
  • 2020-11-28 18:26

    I can't imagine designing a database without foreign keys. Without them, eventually you are bound to make a mistake and corrupt the integrity of your data.

    They are not required, strictly speaking, but the benefits are huge.

    I'm fairly certain that FogBugz does not have foreign key constraints in the database. I would be interested to hear how the Fog Creek Software team structures their code to guarantee that they will never introduce an inconsistency.

    0 讨论(0)
  • 2020-11-28 18:26

    If you plan on generating your data access code, ie, Entity Framework or any other ORM you entirely lose the ability to generate a hierarchical model without Foreign Keys

    0 讨论(0)
  • 2020-11-28 18:27

    Foreign keys help enforce referential integrity at the data level. They also improve performance because they're normally indexed by default.

    0 讨论(0)
提交回复
热议问题