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

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

    ArtistFans
        ArtistID (PK)
        UserID (PK)
    

    The use of an auto incremental PK has no advantages here, even if the parent tables have them.

    I'd also create a "reverse PK" index automatically on (UserID, ArtistID) too: you will need it because you'll query the table by both columns.

    Autonumber/ID columns have their place. You'd choose them to improve certain things after the normalisation process based on the physical platform. But not for link tables: if your braindead ORM insists, then change ORMs...

    Edit, Oct 2012

    It's important to note that you'd still need unique (UserID, ArtistID) and (ArtistID, UserID) indexes. Adding an auto increments just uses more space (in memory, not just on disk) that shouldn't be used

提交回复
热议问题