Are there any good reasons to have a database table without an integer primary key?

前端 未结 11 1830
感动是毒
感动是毒 2021-02-10 00:30

Although I\'m guilty of this crime, it seems to me there can\'t be any good reason for a table to not have an identity field primary key.

Pros: - whether you want to o

相关标签:
11条回答
  • 2021-02-10 00:49

    Every table should have a primary key. It doesn't matter if it's an integer, GUID, or the "setting name" column. The type depends on the requirements of the application. Ideally, if you are going to join the table to another, it would be best to use a GUID or integer as your primary key.

    0 讨论(0)
  • 2021-02-10 00:52

    The key difference (sorry) between a natural primary key and a surrogate primary key is that the value of the natural key contains information whereas the value of a surrogate key doesn't.

    Why is this important? Well a natural primary key is by definition guaranteed to be unique, but its value is not usually guaranteed to stay the same. When it changes, you have to update it in multiple places.

    A surrogate key's value has no real meaning and simply serves to identify that row, so it never needs to be changed. It is a feature of the model rather than the domain itself.

    So the only place I would say a surrogate key isn't appropriate is in an association table which only contains columns referring to rows in other tables (most many-to-many relations). The only information this table carries is the association between two (or more) rows, and it already consists solely of surrogate key values. In this case I would choose a composite primary key.

    If such a table had bag semantics, or carried additional information about the association, I would add a surrogate key.

    0 讨论(0)
  • 2021-02-10 00:52

    The most clear example of a table that doesn't need a surrogate key is a many-to-many relation:

    CREATE TABLE Authorship (
      author_id INT NOT NULL,
      book_id   INT NOT NULL,
      PRIMARY KEY (author_id, book_id),
      FOREIGN KEY (author_id) REFERENCES Authors (author_id),
      FOREIGN KEY (book_id) REFERENCES Books (book_id)
    );
    

    I also prefer a natural key when I design a tagging system:

    CREATE TABLE Tags (
      tag VARCHAR(20) PRIMARY KEY
    );
    
    CREATE TABLE ArticlesTagged (
      article_id INT NOT NULL,
      tag        VARCHAR(20) NOT NULL,
      PRIMARY KEY (article_id, tag),
      FOREIGN KEY (article_id) REFERENCES Articles (article_id),
      FOREIGN KEY (tag) REFERENCES Tags (tag)
    );
    

    This has some advantages over using a surrogate "tag_id" key:

    • You can ensure tags are unique, without adding a superfluous UNIQUE constraint.
    • You prevent two distinct tags from having the exact same spelling.
    • Dependent tables that reference the tag already have the tag text; they don't need to join to Tags to get the text.
    0 讨论(0)
  • 2021-02-10 00:53

    A primary key is ALWAYS a good idea. It allows for very fast and easy joining of tables. It aides external tools that can read system tables to make join allowing less skilled people to create their own queries by drag-and-drop. It also makes the implementation of referential integrity a breeze and that is a good idea from the get go.

    0 讨论(0)
  • 2021-02-10 00:54

    I know for sure that some very smart people working for web giants do this. While I don't know why their own reasons, I know 2 cases where PK-less tables make sense:

    • Importing data. The table is temporary. Insertions and whole table scans need to be as fast as possible. Also, we need to accept duplicate records. Later we will clean the data, but the import process needs to work.
    • Analytics in a DBMS. Identifying a row is not useful - if we need to do it, it is not analytics. We just need a non-relational, redundant, horrible blob that looks like a table. We will build summary tables or materialized views by writing proper SQL queries.

    Note that these cases have good reasons to be non-relational. But normally your tables should be relational, so... yes, they need a primary key.

    0 讨论(0)
  • 2021-02-10 00:57

    Yes, there are good reasons. You can have semantically meaningful true keys, rather than articificial identity keys. Also, it is not a good idea to have a seperate autoincrementing primary key for a Many-Many table. There are some reasons you might want to choose a GUID.

    That being said, I typically use autoincrementing 64bit integers for primary keys.

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