Is it OK not to use a Primary Key When I don't Need one

前端 未结 12 1433
南笙
南笙 2020-12-17 23:06

If I don\'t need a primary key should I not add one to the database?

12条回答
  •  粉色の甜心
    2020-12-17 23:26

    That depends very much on how sure you can be that you don't need one. If you have just the slightest bit of doubt, add one - you'll thank yourself later. An indicator being if the data you store could be related to other data in your DB at one point.

    One use case I can think of is a logging kind-of table, in which you simply dump one entry after the other (to properly process them later). You probably won't need a primary key there, if you're storing enough data to filter out the relevant messages (like a date). Of course, it's questionable to use a RDBMS for this.

提交回复
热议问题