I have the following tables: Users
, Regions
and Teams
. Every tables are in many-to-many connection with eachother and I need every connection from every direction. In other words:
Users
can be in multipleRegions
andTeams
.Regions
can have multipleUsers
andTeams
.Teams
can be in multipleRegions
and have multipleUsers
.
How should I implement the pivot table between them?
Create multiple small pivot tables between each tables
Create one big pivot table between all table
Which one should I use? Is there actually a perfect solution or are there pros and contras? Would the answer different if I have to connect 4 or more tables the same way, where there would be 6 - or (n(n-1))/2 for any n - pivot table?
In certain cases depending on many factors in your business logic, use cases, defined by the exact roles of the entities, etc. you might find the second option useful, but I would definitely go for the first one. In professional database design, every many-to-many relation is normalised using a join table (not pivot). So you would have for e.g.
User
,Region
andUser_Region
and so on for each many-to-many relation.I personally find it very efficient to 1) have a composite primary key consisting of both columns in each join table and 2) let the primary key of the parent tables be a foreign key referencing the join table.
A simple example of the above for one relation on MySQL command line: