Picking the best primary key + numbering system

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

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

    While natural keys may have great meaning to the business users, if you do not have the agreement that those keys are sacred and should not be altered, you will more than likely be pulling your hair out while maintaining a database where the "product codes have to be changed to accommodate the new product line the company acquired." You need to protect the RI of your data, and integers as primary keys with auto-increment are the best way to go. Performance is also better when indexing and traversing integers than char columns.

    While not appropriate as primary keys, natural keys are very appropriate for user consumption and you can enforce uniques via an index. They bring a context to the data that will make it easier for all parties to understand. Also, in the advent that you need to reload data, the natural keys can help verify that your lookups are still valid.

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

    I hope you will agree with me that every design element should have single purpose.

    Question is what do you think is purpose of PK? If it is to identify unique record in a table, then surrogate keys wins without much trouble. This is simple and straight.

    As far as new columns in option 3 are concerned, you should check if these can be calculated (best would be to do calculation in model layer so that they can be changed easily than if calculation done in RDBMS) without too much of performance penalty from other elements. For example, you can store segment number and road number in corresponding tables and then use them to generate "00000001.1". This will allow to change asset numbering on-the-fly.

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

    I suspect that you really should use option #3, as many here have already said. Surrogate PKs (either Integers or GUIDs) are good practice, even if there are adequate business keys. Surrogates will reduce maintenance headaches (as you yourself have already noted).

    That being said, something you may want to consider is whether or not your database is:

    1. focused on data maintenance and transactional processing (i.e. Create/Update/Delete operations)
    2. geared towards analysis and reporting (i.e. Queries)

    In other words, are the users concerned with maintaining active data or querying largely static data to find answers?

    If you are heavily focused on building an analysis and reporting DB (e.g. a data warehouse/mart) that is exposed to technical business users (e.g. report designers) who have a good grasp of the business vocabulary, then you might want to consider using natural keys based on meaningful business values. They help reduce query complexity by eliminating the need for complex joins and help the user focus on their task, not fighting the database structure.

    Otherwise you're probably focused on a full CRUD DB that has to cover all the bases to some degree - this is the vast majority of situations. In which case, go with your option #3. You can always optimize for queryability in the future but you'll be hard pressed to retrofit for maintainability.

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

    I'm also very strongly in the "don't use primary keys as meaningful data" camp. Every time I have contravened that policy it has ended in tears. Sooner or later the meaningful data needs to change and if that means you have to change a primary key it can get painful. The primary key will probably be used in foreign key constraints and you can spend ages trying to sort it all out just to make a simple data change.

    I always use GUIDs/UUIDs for my primary keys in every table I ever create but that's just personal preference serials or such are also good.

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

    This is really a discussion about surrogate (also called technical or synthetic) vs natural primary keys, a subject that has been extensively covered. I covered this in Database Development Mistakes Made by AppDevelopers.

    Natural keys are keys based on externally meaningful data that is (ostensibly) unique. Common examples are product codes, two-letter state codes (US), social security numbers and so on. Surrogate or technical primary keys are those that have absolutely no meaning outside the system. They are invented purely for identifying the entity and are typically auto-incrementing fields (SQL Server, MySQL, others) or sequences (most notably Oracle).

    In my opinion you should always use surrogate keys. This issue has come up in these questions:

    • How do you like your primary keys?
    • What’s the best practice for Primary Keys in tables?
    • Which format of primary key would you use in this situation.
    • Surrogate Vs. Natural/Business Keys
    • Should I have a dedicated primary key field?

    Auto number fields are the way to go. If your keys have meaning outside your database (like asset numbers) those will quite possibly change and changing keys is problematic. Just use indexes for those things into the relevant tables.

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