Вопрос

salesman
========
uId

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

group
======
groupId

I have 3 table above that formed many to many relationship, and I would add a field 'performaces' (INT) so that each salesman can have a score in each group. And I believe it should be located at salesGroupLinked table. But since uId and groupId is FK, I can't insert / edit the data (I'm using phpmyadmin). I can't make the performacesScore field unique since they can be same value for example a salesman get 10 and another get the same.

I got this msg :

This table does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available.

describe salesGroupLinked enter image description here

Это было полезно?

Решение

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.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top