many to many relationship bridge table dilemma

后端 未结 1 1321
暗喜
暗喜 2021-01-17 07:45
salesman
========
uId

salesGroupLinked
================
uId
groupId
//add performacesScore field here

group
======
groupId

I have 3 table above t

相关标签:
1条回答
  • 2021-01-17 08:40

    The tool is simply telling you that there can be several entries for a uId-groupId combination. Example:

    uId  groupId  performacesScore 
    1    1        10
    1    2        20
    2    1        30
    2    1        30
    2    1        40
    2    2        20
    

    Now imagine this data is shown to you and you make the first 2/1/30 a 2/1/50. What update statement could the tool sent to the dbms?

    update salesGroupLinked set performacesScore = 50
    where uId = 2 and groupId = 1;
    

    This would update three records instead of one.

    update salesGroupLinked set performacesScore = 50
    where uId = 2 and groupId = 1 and performacesScore = 30;
    

    This would still update two records instead of one.

    So in order to properly update and delete, you must tell the dbms what makes the records unique. There are four possibilities:

    • If you never want to update or delete single records, leave it as is.
    • If you want to be able to update and there can only be one entry for a uId-groupId combination, then tell the dbms so and make uId plus groupId the primary key of your table.
    • If you want to be able to update and there can be duplicates for a a uId-groupId combination, but a uId-groupId-performacesScore combination will always be unique, then make these three the table's primary key.
    • If you want to be able to update and there can be duplicates for any combination, then give the table another column for a technical id and make this the primary key.
    0 讨论(0)
提交回复
热议问题