Are there any good reasons to have a database table without an integer primary key?

前端 未结 11 1819
感动是毒
感动是毒 2021-02-10 00:30

Although I\'m guilty of this crime, it seems to me there can\'t be any good reason for a table to not have an identity field primary key.

Pros: - whether you want to o

相关标签:
11条回答
  • 2021-02-10 01:00

    I don't think every table needs a primary key. Sometimes you only want to "connect" the contents of two tables - via their primary key.

    So you have a table like users and one table like groups (each with primary keys) and you have a third table called users_groups with only two colums (user and group) where users and groups are connected with each other.

    For example a row with user = 3 and group = 6 would link the user with primary key 3 to the group with primary key 6.

    0 讨论(0)
  • 2021-02-10 01:05

    Every table should have a primary key. But it doesn't need to be a single field identifier. Take for example in a finance system, you may have the primary key on a journal table being the Journal ID and Line No. This will produce a unique combination for each row (and the Journal ID will be a primary key in its own table)

    Your primary key needs to be defined on how you are going to link the table to other tables.

    0 讨论(0)
  • 2021-02-10 01:11

    One reason not to have primary key defined as identity is having primary key defined as GUIDs or populated with externally generated values.

    In general, every table that is semantically meaningful by itself should have primary key and such key should have no semantic meaning. A join table that realizes many-to-many relationship is not meaningful by itself and so it doesn't need such primary key (it already has one via its values).

    0 讨论(0)
  • 2021-02-10 01:11

    Sure, an example in a single-database solution is if you have a table of countries, it probably makes more sense to use the ISO 3166-1-alpha-2 country code as the primary key as this is an international standard, and makes queries much more readable (e.g. CountryCode = 'GB' as opposed to CountryCode = 28). A similar argument could be applied to ISO 4217 currency codes.

    In a SQL Server database solution using replication, a UNIQUEIDENTIFIER key would make more sense as GUIDs are required for some types of replication (and also make it much easier to avoid key conflicts if there are multiple source databases!).

    0 讨论(0)
  • 2021-02-10 01:16

    To be a properly normalised table, each row should only have a single identifiable key. Many tables will already have natural keys, such a unique invoice number. I agree, especially with storage being so cheap, there is little overhead in having an autonumber/identity key on all tables, but in this instance which is the real key.

    Another area where I personally don't use this approach if for reference data, where typically we have a Description and a Value

    Code, Description
    'L', 'Live'
    'O', 'Old'
    'P', 'Pending'
    

    In this situation making code a primary key ensures no duplicates, and is more human readable.

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