Picking the best primary key + numbering system

前端 未结 13 1642
盖世英雄少女心
盖世英雄少女心 2020-12-02 14:50

We are trying to come up with a numbering system for the asset system that we are creating, there has been a few heated discussions on this topic in the office so I decided

13条回答
  •  日久生厌
    2020-12-02 15:07

    I think the important thing to remember here is that each table in your database/design might have multiple keys. These are the Candidate Keys. See wikipedia entry for Candidate Keys

    By definition, all Candidate Keys are created equal. They are each unique identifiers for the table in question.

    Your job then is to select the best candidate from the pool of Candidate Keys to serve as the Primary Key. The Primary Key will be used by other tables to establish the relational constraints, but you are free to continue using Candidate Keys to query the table.

    Because Primary Keys are referenced by other structures, and therefore used in join operations, the criteria for Primary Key selection boils down to the following for me (in order of importance):

    • Immutable/Stable - Primary Key values should not change. If they do, you run the risk of introducing update anomolies
    • Not Null - most DBMS platforms require that the Primary Key attribute(s) are not null
    • Simple - simple datatypes and values for physical storage and performance. Integer values work well here, and this is the datatype of choice for most surrogate/auto-gen keys

    Once you've identified the Candidate Keys, the criteria above can be used to select the Primary Key. If there is not a "Natural" Candidate Key meets the criteria, then a Surrogate Key that does meet the criteria can be created and used as mentioned in other answers.

提交回复
热议问题