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

前端 未结 21 2225
别那么骄傲
别那么骄傲 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:03

    If you really want to read through all of the back and forth on this age-old debate, do a search for "natural key" on Stack Overflow. You should get back pages of results.

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

    We do a lot of joins and composite primary keys have just become a performance hog. A simple int or long takes care of many problems even though you are introducing a second candidate key, but it's a lot easier and more understandable to join on one field versus three.

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

    Natural verses artifical keys is a kind of religious debate among the database community - see this article and others it links to. I'm neither in favour of always having artifical keys, nor of never having them. I would decide on a case-by-case basis, for example:

    • US States: I'd go for state_code ('TX' for Texas etc.), rather than state_id=1 for Texas
    • Employees: I'd usually create an artifical employee_id, because it's hard to find anything else that works. SSN or equivalent may work, but there could be issues like a new joiner who hasn't supplied his/her SSN yet.
    • Employee Salary History: (employee_id, start_date). I would not create an artifical employee_salary_history_id. What point would it serve (other than "foolish consistency")

    Wherever artificial keys are used, you should always also declare unique constraints on the natural keys. For example, use state_id if you must, but then you'd better declare a unique constraint on state_code, otherwise you are sure to eventually end up with:

    state_id    state_code   state_name
    137         TX           Texas
    ...         ...          ...
    249         TX           Texas
    
    0 讨论(0)
  • 2020-11-22 14:08

    Besides all those good answers, I just want to share a good article I just read, The great primary-key debate.

    Just to quote a few points:

    The developer must apply a few rules when choosing a primary key for each table:

    • The primary key must uniquely identify each record.
    • A record’s primary-key value can’t be null.
    • The primary key-value must exist when the record is created.
    • The primary key must remain stable—you can’t change the primary-key field(s).
    • The primary key must be compact and contain the fewest possible attributes.
    • The primary-key value can’t be changed.

    Natural keys (tend to) break the rules. Surrogate keys comply with the rules. (You better read through that article, it is worth your time!)

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

    Natural versus artificial keys to me is a matter of how much of the business logic you want in your database. Social Security number (SSN) is a great example.

    "Each client in my database will, and must, have an SSN." Bam, done, make it the primary key and be done with it. Just remember when your business rule changes you're burned.

    I don't like natural keys myself, due to my experience with changing business rules. But if your sure it won't change, it might prevent a few critical joins.

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

    Tables should have a primary key all the time. When it doesn't it should have been an AutoIncrement fields.

    Sometime people omit primary key because they transfer a lot of data and it might slow down (depend of the database) the process. BUT, it should be added after it.

    Some one comment about link table, this is right, it's an exception BUT fields should be FK to keep the integrity, and is some case those fields can be primary keys too if duplicate in links is not authorized... but to keep in a simple form because exception is something often in programming, primary key should be present to keep the integrity of your data.

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