Should each and every table have a primary key?

后端 未结 15 726
甜味超标
甜味超标 2020-11-22 07:00

I\'m creating a database table and I don\'t have a logical primary key assigned to it. So, I\'m thinking about leaving it without a primary key, but I\'m feeling a bit guilt

相关标签:
15条回答
  • 2020-11-22 07:23

    I am in the role of maintaining application created by offshore development team. Now I am having all kinds of issues in the application because original database schema did not contain PRIMARY KEYS on some tables. So please dont let other people suffer because of your poor design. It is always good idea to have primary keys on tables.

    0 讨论(0)
  • 2020-11-22 07:25

    Except for a few very rare cases (possibly a many-to-many relationship table, or a table you temporarily use for bulk-loading huge amounts of data), I would go with the saying:

    If it doesn't have a primary key, it's not a table!

    Marc

    0 讨论(0)
  • 2020-11-22 07:25

    In short, no. However, you need to keep in mind that certain client access CRUD operations require it. For future proofing, I tend to always utilize primary keys.

    0 讨论(0)
  • 2020-11-22 07:25

    If you are using Hibernate its not possible to create an Entity without a primary key. This issues can create problem if you are working with an existing database which was created with plain sql/ddl scripts, and no primary key was added

    0 讨论(0)
  • 2020-11-22 07:26

    Always best to have a primary key. This way it meets first normal form and allows you to continue along the database normalization path.

    As stated by others, there are some reasons not to have a primary key, but most will not be harmed if there is a primary key

    0 讨论(0)
  • 2020-11-22 07:28

    I always have a primary key, even if in the beginning I don't have a purpose in mind yet for it. There have been a few times when I eventually need a PK in a table that doesn't have one and it's always more trouble to put it in later. I think there is more of an upside to always including one.

    0 讨论(0)
提交回复
热议问题