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

后端 未结 7 650
死守一世寂寞
死守一世寂寞 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:23

    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.

    Use it as a unique identifier with a dataset where the PKey is not part of user managed data.

    Let's say I have a table containing data about the chemical elements. The atomic number of each element is unique and will never change. So rather than using an auto-incrementing integer for each column, it would probably make more sense to just use the atomic number, correct?

    Yes.

    Would the same be true if I had a table of books? Should I use the ISBN or an auto-incrementing integer for the primary key? Or a table of employees containing each person's SSN?

    ISBNs/SS#s are assigned by third-parties and because of their large storage size would be a highly inefficient way to uniquely identify a row. Remember, PKeys are useful when you join tables. Why use a large data format like an ISBN which would be numerous textual characters as the Unique identifier when a small and compact format like Integer is available?

提交回复
热议问题