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

后端 未结 7 884
半阙折子戏
半阙折子戏 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: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.

提交回复
热议问题