Should all database tables have a primary key?

前端 未结 7 1539
旧巷少年郎
旧巷少年郎 2021-01-04 22:35

Is it good practice to give every database table a primary key? It seems to me that if the primary key is not explicitly needed, then it would just be extra clutter in my d

7条回答
  •  轮回少年
    2021-01-04 23:16

    When you probably WOULD:

    In an OLTP database you'd almost always (in my case always) have a primary key of some sort. Sometimes Guid, sometimes autonumber/identity fields, sometimes set by the application or the customer. Sometimes even a combination of more than one field. This is because you'll typically want to uniquely identify any given row from the table.

    Also, a primary key is a constraint used by the query optimiser that should improve performance for lookups and joins.

    When you probably WOULDN'T:

    The only time you wouldn't have a primary key is in a "reporting" table, probably in a denormalised data warehouse.

提交回复
热议问题