Should each and every table have a primary key?

后端 未结 15 727
甜味超标
甜味超标 2020-11-22 07:00

I\'m creating a database table and I don\'t have a logical primary key assigned to it. So, I\'m thinking about leaving it without a primary key, but I\'m feeling a bit guilt

相关标签:
15条回答
  • 2020-11-22 07:35

    Late to the party but I wanted to add my two cents:

    Should each and every table have a primary key?

    • If you are talking about "Relational Albegra", the answer is Yes. Modelling data this way requires the entities and tables to have a primary key. The problem with relational algebra (apart from the fact there are like 20 different, mismatching flavors of it), is that it only exists in paper. You can't build real world applications using relational algebra.

    • Now, if you are talking about databases from real world apps, they partially/mostly adhere to the relational algebra, by taking the best of it and discarding the rest. Also, database engines offer massive non-relational functionality nowadays (it's 2020 now). So in this case the answer is No. In any case, 99.9% of my real world tables have a primary key, but there are justifiable exceptions. Case in point: event/log tables (multiple indexes, but not a single key in sight).

    Bottom line, in transactional applications that follow the entity/relationship model it makes a lot of sense to have primary keys for almost (if not) all of the tables. If you ever decide to skip the primary key of a table, make sure you have a good reason for it, and you are prepared to defend your decision.

    0 讨论(0)
  • 2020-11-22 07:38

    Short answer: yes.

    Long answer:

    • You need your table to be joinable on something
    • If you want your table to be clustered, you need some kind of a primary key.
    • If your table design does not need a primary key, rethink your design: most probably, you are missing something. Why keep identical records?

    In MySQL, the InnoDB storage engine always creates a primary key if you didn't specify it explicitly, thus making an extra column you don't have access to.

    Note that a primary key can be composite.

    If you have a many-to-many link table, you create the primary key on all fields involved in the link. Thus you ensure that you don't have two or more records describing one link.

    Besides the logical consistency issues, most RDBMS engines will benefit from including these fields in a unique index.

    And since any primary key involves creating a unique index, you should declare it and get both logical consistency and performance.

    See this article in my blog for why you should always create a unique index on unique data:

    • Making an index UNIQUE

    P.S. There are some very, very special cases where you don't need a primary key.

    Mostly they include log tables which don't have any indexes for performance reasons.

    0 讨论(0)
  • 2020-11-22 07:38

    Just add it, you will be sorry later when you didn't (selecting, deleting. linking, etc)

    0 讨论(0)
  • 2020-11-22 07:41

    Will you ever need to join this table to other tables? Do you need a way to uniquely identify a record? If the answer is yes, you need a primary key. Assume your data is something like a customer table that has the names of the people who are customers. There may be no natural key because you need the addresses, emails, phone numbers, etc. to determine if this Sally Smith is different from that Sally Smith and you will be storing that information in related tables as the person can have mulitple phones, addesses, emails, etc. Suppose Sally Smith marries John Jones and becomes Sally Jones. If you don't have an artifical key onthe table, when you update the name, you just changed 7 Sally Smiths to Sally Jones even though only one of them got married and changed her name. And of course in this case withouth an artificial key how do you know which Sally Smith lives in Chicago and which one lives in LA?

    You say you have no natural key, therefore you don't have any combinations of field to make unique either, this makes the artficial key critical.

    I have found anytime I don't have a natural key, an artifical key is an absolute must for maintaining data integrity. If you do have a natural key, you can use that as the key field instead. But personally unless the natural key is one field, I still prefer an artifical key and unique index on the natural key. You will regret it later if you don't put one in.

    0 讨论(0)
  • 2020-11-22 07:41

    I know that in order to use certain features of the gridview in .NET, you need a primary key in order for the gridview to know which row needs updating/deleting. General practice should be to have a primary key or primary key cluster. I personally prefer the former.

    0 讨论(0)
  • 2020-11-22 07:42

    Pretty much any time I've created a table without a primary key, thinking I wouldn't need one, I've ended up going back and adding one. I now create even my join tables with an auto-generated identity field that I use as the primary key.

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