Do link tables need a meaningless primary key field?

前端 未结 12 1474
你的背包
你的背包 2020-12-08 14:05

I am working on a couple of link tables and I got to thinking (Danger Will Robinson, Danger) what are the possible structures of a link table and what are their pro\'s and c

相关标签:
12条回答
  • 2020-12-08 14:29

    For true link tables, they typically do not exist as object entities in my object models. Thus the surrogate key is not ever used. The removable of an item from a collection results in a removal of an item from a link relationship where both foreign keys are known (Person.Siblings.Remove(Sibling) or Person.RemoveSibling(Sibling) which is appropriately translated at the data access layer as usp_Person_RemoveSibling(PersonID, SiblingID)).

    As Mike mentioned, if it does become an actual entity in your object model, then it may merit an ID. However, even with addition of temporal factors like effective start and end dates of the relationship and things like that, it's not always clear. For instance, the collection may have an effective date associated at the aggregate level, so the relationship itself may still not become an entity with any exposed properties.

    I'd like to add that you might very well need the table indexed both ways on the two foreign key columns.

    0 讨论(0)
  • 2020-12-08 14:29

    There is something called identifying and non-identifying relationship. With identifying relationships the FK is a part of the PK in the many-to-many table. For example, say we have tables Person, Company and a many-to-many table Employment. In an identifying relationship both fk PersonID and CompanyID are part of the pk, so we can not repeat PersonID, CompanyID combination.

    TABLE Employment(PersonID int (PK,FK), CompanyID int (PK,FK))
    

    Now, suppose we want to capture history of employment, so a person can leave a company, work somewhere else and return to the same company later. The relationship is non-identifying here, combination of PersonID, CompanyID can now repeat, so the table would look something like:

    TABLE Employment(EmploymentID int (PK), PersonID int (FK), CompanyID int (FK), 
                         FromDate datetime, ToDate datetime)
    
    0 讨论(0)
  • 2020-12-08 14:34

    Having a single column pk can help out alot in disaster recovery situation. So though while correct in theory that you only need the 2 foreign keys. In practice when the shit hits the fan you may want the single column key. I have never been in a situation where i was screwed because I had a single column identifier but I have been in ones where I was screwed because I didn't.

    0 讨论(0)
  • 2020-12-08 14:35

    The correct answer is:

    • Primary key is ('table1fk' , 'table2fk')
    • Another index on ('table2fk' , 'table1fk')

    Because:

    • You don't need an index on table1fk or table2fk alone: the optimiser will use the PK
    • You'll most likely use the table "both" ways
    • Adding a surrogate key is only needed because of braindead ORMs
    0 讨论(0)
  • 2020-12-08 14:35

    I (almost) always use the additional single-column primary key. This generally makes it easier to build user interfaces, because when a user selects that particular linking entity I can identify with a single integer value rather than having to create and then parse compound identifiers.

    0 讨论(0)
  • 2020-12-08 14:37

    Composite PK and turn off clustering.

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