Should every MySQL table have an auto-incremented primary key?

后端 未结 3 1891
半阙折子戏
半阙折子戏 2021-02-15 15:35
  • I understand the value of primary keys.
  • I understand the value of indexes.

Should every MySQL table have an auto-incremented prim

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-15 16:21

    Yes, with one exception:

    A table which implements a n:m relationship between two other tables, a pure link table, does not need such a field if it has two fields only, referencing the two primary keys of the linked tables. The primary key of the link table then consists of the two fields.

    As soon as the link table has extra information, it needs a simple single-field primary key.

    Having said that, there may be more exceptions; database design is a very broad field...

    EDIT: Added more in the previous sentence.

提交回复
热议问题