When to use an auto-incremented primary key and when not to?

后端 未结 7 654
死守一世寂寞
死守一世寂寞 2020-12-13 01:39

I\'m trying to figure out the \"best practices\" for deciding whether or not to add an auto-incrementing integer as the primary key to a table.

Let\'s say I have a t

7条回答
  •  心在旅途
    2020-12-13 02:16

    You've got the idea right there.

    Auto-increment should be used as a unique key when no unique key already exists about the items you are modelling. So for Elements you could use the Atomic Number or Books the ISBN number.

    But if people are posting messages on a message board then these need a unique ID, but don't contain one naturally so we assign the next number from a list.

    It make sense to use natural keys where possible, just remember to make the field as the primary key and ensure that it is indexed for performance

提交回复
热议问题