SQL: Do you need an auto-incremental primary key for Many-Many tables?

后端 未结 7 886
半阙折子戏
半阙折子戏 2020-12-06 00:48

Say you have a Many-Many table between Artists and Fans. When it comes to designing the table, do you design the table like such:

ArtistFans
    ArtistFanID         


        
相关标签:
7条回答
  • 2020-12-06 01:24

    In my opinion, in pure SQL id column is not necessary and should not be used. But for ORM frameworks such as Hibernate, managing many-to-many relations is not simple with compound keys etc., especially if join table have extra columns.

    So if I am going to use a ORM framework on the db, I prefer putting an auto-increment id column to that table and a unique constraint to the referencing columns together. And of course, not-null constraint if it is required.

    Then I treat the table just like any other table in my project.

    0 讨论(0)
  • 2020-12-06 01:27

    Assuming that you're already a devotee of the surrogate key (you're in good company), there's a case to be made for going all the way.

    A key point that is sometimes forgotten is that relationships themselves can have properties. Often it's not enough to state that two things are related; you might have to describe the nature of that relationship. In other words, there's nothing special about a relationship table that says it can only have two columns.

    If there's nothing special about these tables, why not treat it like every other table and use a surrogate key? If you do end up having to add properties to the table, you'll thank your lucky presentation layers that you don't have to pass around a compound key just to modify those properties.

    I wouldn't even call this a rule of thumb, more of a something-to-consider. In my experience, some slim majority of relationships end up carrying around additional data, essentially becoming entities in themselves, worthy of a surrogate key.

    The rub is that adding these keys after the fact can be a pain. Whether the cost of the additional column and index is worth the value of preempting this headache, that really depends on the project.

    As for me, once bitten, twice shy – I go for the surrogate key out of the gate.

    0 讨论(0)
  • 2020-12-06 01:27

    Funny how all answers favor variant 2, so I have to dissent and argue for variant 1 ;)

    To answer the question in the title: no, you don't need it. But...

    Having an auto-incremental or identity column in every table simplifies your data model so that you know that each of your tables always has a single PK column.

    As a consequence, every relation (foreign key) from one table to another always consists of a single column for each table.

    Further, if you happen to write some application framework for forms, lists, reports, logging etc you only have to deal with tables with a single PK column, which simplifies the complexity of your framework.

    Also, an additional id PK column does not cost you very much in terms of disk space (except for billion-record-plus tables).

    Of course, I need to mention one downside: in a grandparent-parent-child relation, child will lose its grandparent information and require a JOIN to retrieve it.

    0 讨论(0)
  • 2020-12-06 01:37

    The standard way is to use the composite primary key. Adding in a separate autoincrement key is just creating a substitute that is already there using what you have. Proper database normalization patterns would look down on using the autoincrement.

    0 讨论(0)
  • 2020-12-06 01:38

    Even if you create an identity column, it doesn't have to be the primary key.

    ArtistFans
        ArtistFanId
        ArtistId (PK)
        UserId (PK)
    

    Identity columns can be useful to relate this relation to other relations. For example, if there was a creator table which specified the person who created the artist-user relation, it could have a foreign key on ArtistFanId, instead of the composite ArtistId+UserId primary key.

    Also, identity columns are required (or greatly improve the operation of) certain ORM packages.

    0 讨论(0)
  • 2020-12-06 01:39

    I cannot think of any reason to use the first form you list. The compound primary key is fine, and having a separate, artificial primary key (along with the unique contraint you need on the foreign keys) will just take more time to compute and space to store.

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