Should all database tables have a primary key?

前端 未结 7 1533
旧巷少年郎
旧巷少年郎 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:32

    The purpose of keys in relational database design is to prevent duplicate data and thereby help ensure data integrity. If you permit duplicate rows then you create ambiguity, potential update anomalies and very possibly incorrect results. Therefore in general every table ought to have at least one key (and maybe more than one if required). It's not usually the case that data integrity is "not explicitly needed"!

    A primary key is just any one of the keys of a table. Designating one key as a primary key can be useful but is not particularly important - the important thing is that each table have at least one candidate key.

    The reasons why duplicate rows ought to be avoided are covered pretty extensively in database literature. See:

    http://www.dbdebunk.com/page/page/627052.htm

    http://www.dbdebunk.com/page/page/638922.htm

    http://dl.acm.org/citation.cfm?id=77708

    http://www.amazon.com/Practical-Issues-Database-Management-Practitioner/dp/0201485559

提交回复
热议问题