Picking the best primary key + numbering system

前端 未结 13 1608
盖世英雄少女心
盖世英雄少女心 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:18

    I would go with the surrogate key, but you may want to have a computed column that "formats" the surrogate key into a more "readable" value if that improves your reporting. The computed colum could produce example 2 from the surrogate key for instance for display purposes.

    I think the surrogate key route is the way to go and the only exceptions that I make for it are join tables, where the primary key could be composed of the foreign key references. Even in these cases I'm finding that having a surrogate primary key is more useful than not.

    0 讨论(0)
  • 2020-12-02 15:21

    As mentioned before, keep your internal primary keys as just keys, whatever the most optimal datatype is on your platform.

    However you do need to let the numbering system argument be fought out, as this is actually a business requirement, and perhaps let's call it an identification system for the asset.

    If there is only going to be one identifier, then add it as a column to the main table. If there are likely to be many identification systems (and assets usually have many), you'll need two more tables

        Identifier-type table             Identifier-cross-ref table
          type-id             ------------> type-id              (unique
          type-name                         identifier-string     key)
                                            internal-id
    
    
    

    That way different people who need to access the asset can identify in their own way. For example the server team will identify a server differently from the network team and different again from project management, accounts, etc.

    Plus, you get to go to all the meetings where everyone argues with each other.

    0 讨论(0)
  • 2020-12-02 15:28

    I would personally say keep it simple and stay with an autoincremented primary key. If you need something more "Readable" in terms of display in the program, then possibly one of your other ideas, but I think that is just adding unneeded complexity to the primary key field.

    0 讨论(0)
  • 2020-12-02 15:32

    Follow the Don't Use policy.

    Some problems you can run into:

    You need to generate keys from more than one host.

    Someone will want to reserve contiguous numbers to use together.

    How meaningful will people want it to be? Wars are fought over this, and you're in the first skirmish of one already. "It's already meaningful, and if we just add two more digits we can ..." i.e. you're establishing a design style that will (should) be extensible.

    If you are concatenating the two, you're doing typecasts which can mess up your query Optimizer.

    You'll need to reclassify roads, and redefine their boundaries (i.e. move the roads), which implies changing the primary key and maybe losing links.

    There are workarounds for all this, but this is the kind of issue where workarounds proliferate and get out of control. And it doesn't take more than a couple to get beyond "Simple".

    0 讨论(0)
  • 2020-12-02 15:32

    First off, option 2 is the absolute worst option. As an Index, it's a string, and that makes it slow. And it's generated based on business rules - which can change and cause a rather large headache.

    Personally, I always use a separate primary key column; and I always use a GUID. Some developers prefer a simple INT over a GUID for reasons of hard-drive space. However, if the situation arises where you need to merge two databases, GUIDs will almost never collide (whereas INTs are guaranteed to collide).

    Primary Keys should NEVER be seen by the user. Making it readable to the user should not be a concern. Primary Keys SHOULD be used to link with Foreign Keys. This is their purpose. The value should be machine readable and, once created, never changed.

    0 讨论(0)
  • 2020-12-02 15:33

    Don't put meaning into your PK fields unless...

    • It is 100% completely impossible that the value will never change and that

    • No two people would ever reasonably
      argue about which value should be
      used for a particular row.

    Go with option one and format the value in the app to look like option two or three when it is displayed.

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