in general, should every table in a database have an identity field to use as a PK?

前端 未结 10 1894
旧时难觅i
旧时难觅i 2020-12-07 15:36

This seems like a duplicate even as I ask it, but I searched and didn\'t find it. It seems like a good question for SO -- even though I\'m sure I can find it on many blogs e

10条回答
  •  抹茶落季
    2020-12-07 16:07

    Yes, for the vast majority of cases.

    Edge cases or exceptions might be things like:

    • two-way join tables to model m:n relationships
    • temporary tables used for bulk-inserting huge amounts of data

    But other than that, I think there is no good reason against having a primary key to uniquely identify each row in a table, and in my opinion, using an IDENTITY field is one of the best choices (I prefer surrogate keys over natural keys - they're more reliable, stable, never changing etc.).

    Marc

提交回复
热议问题