What's the best practice for primary keys in tables?

前端 未结 21 2227
别那么骄傲
别那么骄傲 2020-11-22 14:02

When designing tables, I\'ve developed a habit of having one column that is unique and that I make the primary key. This is achieved in three ways depending on requirements

相关标签:
21条回答
  • 2020-11-22 14:18

    A natural key, if available, is usually best. So, if datetime/char uniquely identifies the row and both parts are meaningful to the row, that's great.

    If just the datetime is meaningful, and the char is just tacked on to make it unique, then you might as well just go with an identify field.

    0 讨论(0)
  • 2020-11-22 14:19

    You should use a 'composite' or 'compound' primary key that comprises of multiple fields.

    This is a perfectly acceptable solution, go here for more info :)

    0 讨论(0)
  • 2020-11-22 14:22

    Just an extra comment on something that is often overlooked. Sometimes not using a surrogate key has benefits in the child tables. Let's say we have a design that allows you to run multiple companies within the one database (maybe it's a hosted solution, or whatever).

    Let's say we have these tables and columns:

    Company:
      CompanyId   (primary key)
    
    CostCenter:
      CompanyId   (primary key, foreign key to Company)
      CostCentre  (primary key)
    
    CostElement
      CompanyId   (primary key, foreign key to Company)
      CostElement (primary key)
    
    Invoice:
      InvoiceId    (primary key)
      CompanyId    (primary key, in foreign key to CostCentre, in foreign key to CostElement)
      CostCentre   (in foreign key to CostCentre)
      CostElement  (in foreign key to CostElement)
    

    In case that last bit doesn't make sense, Invoice.CompanyId is part of two foreign keys, one to the CostCentre table and one to the CostElement table. The primary key is (InvoiceId, CompanyId).

    In this model, it's not possible to screw-up and reference a CostElement from one company and a CostCentre from another company. If a surrogate key was used on the CostElement and CostCentre tables, it would be.

    The fewer chances to screw up, the better.

    0 讨论(0)
  • 2020-11-22 14:24

    I always use an autonumber or identity field.

    I worked for a client who had used SSN as a primary key and then because of HIPAA regulations was forced to change to a "MemberID" and it caused a ton of problems when updating the foreign keys in related tables. Sticking to a consistent standard of an identity column has helped me avoid a similar problem in all of my projects.

    0 讨论(0)
  • 2020-11-22 14:26

    I suspect Steven A. Lowe's rolled up newspaper therapy is required for the designer of the original data structure.

    As an aside, GUIDs as a primary key can be a performance hog. I wouldn't recommend it.

    0 讨论(0)
  • 2020-11-22 14:29

    There´s no problem in making your primary key from various fields, that's a Natural Key.

    You can use a Identity column (associated with a unique index on the candidate fields) to make a Surrogate Key.

    That´s an old discussion. I prefer surrogate keys in most situations.

    But there´s no excuse for the lack of a key.

    RE: EDIT

    Yeah, there´s a lot of controversy about that :D

    I don´t see any obvious advantage on natural keys, besides the fact that they are the natural choice. You will always think in Name, SocialNumber - or something like that - instead of idPerson.

    Surrogate keys are the answer to some of the problems that natural keys have (propagating changes for example).

    As you get used to surrogates, it seems more clean, and manageable.

    But in the end, you´ll find out that it's just a matter of taste - or mindset -. People "think better" with natural keys, and others don´t.

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