What's the best practice for primary keys in tables?

前端 未结 21 2408
别那么骄傲
别那么骄傲 2020-11-22 14:02

When designing tables, I\'ve developed a habit of having one column that is unique and that I make the primary key. This is achieved in three ways depending on requirements

21条回答
  •  没有蜡笔的小新
    2020-11-22 14:08

    Besides all those good answers, I just want to share a good article I just read, The great primary-key debate.

    Just to quote a few points:

    The developer must apply a few rules when choosing a primary key for each table:

    • The primary key must uniquely identify each record.
    • A record’s primary-key value can’t be null.
    • The primary key-value must exist when the record is created.
    • The primary key must remain stable—you can’t change the primary-key field(s).
    • The primary key must be compact and contain the fewest possible attributes.
    • The primary-key value can’t be changed.

    Natural keys (tend to) break the rules. Surrogate keys comply with the rules. (You better read through that article, it is worth your time!)

提交回复
热议问题