From Previous Question I got some idea about having composite primary key in n-m relationships. My condition is very similary yet a little different.
[Person]
PERSON_ID: int, primary key
CLIENT_ID: int, primary key
Name: nvarchar(xx)
primary_key( PERSON_ID, CLIENT_ID);
[Group]
GROUP_ID: int
CLIENT_ID: int
Name: nvarchar(xx)
primary_key( GROUP_ID, CLIENT_ID);
Now when I use mysql-workbench tool to generate a relationship table what it does is it creates a table:
option 1:
[PersonHasGroup]
PERSON_ID: int
CLIENT_ID: int
GROUP_ID: int
CLIENT1_ID: int
primary_key( PERSON_ID, CLIENT_ID, GROUP_ID, CLIENT1_ID);
In my case both client_id would normally have the same value so I edited the table to look like
option 2:
[PersonHasGroup]
PERSON_ID: int
CLIENT_ID: int
GROUP_ID: int
primary_key( PERSON_ID, CLIENT_ID, GROUP_ID);
Are those a good practices? What my other colleagues prefer is a little different. they use:
Option 3:
[PersonHasGroup]
PERSON_HAS_GROUP_ID: int, auto-increment
CLIENT_ID: int
PERSON_ID: int, foreign key
GROUP_ID: int, foreign key
primary_key( PERSON_HAS_GROUP_ID, CLIENT_ID);
Which of the practise would be suitable when my relationship would be many-to-many explanation with an example would be a lot helpful 🙂
I prefer to have a single id for easy referencing, and another unique key for the unique pair, never used workbench, so here is it first in plain sql:
and proberly somthing like this in “workbench”: