Reputation: 5717
I have the following SQL Server db with one table so far.
----------------
|Users |
----------------
| UserId PK |
| |
| Other fields |
----------------
I need to add few more tables to it which is not a problem but this is the goal:
---------------- ---------------- ---------------- ----------------
|Users | |Roles | | Teams | | Groups |
---------------- ---------------- ---------------- ----------------
| UserId PK | | RoleId PK | | TeamId PK | | GroupId PK |
| | | | | | | |
| Other fields | | Other fields | | Other fields | | Other fields |
---------------- ---------------- ---------------- ----------------
What I need to achieve is the following:
I have X amount of users
I have 3 roles only for all users to use in all teams and groups
One user can create X amount of teams
One user can create X amount of groups
Groups and Teams can have users assigned to them with different roles (Admin, Member, Visitor)
One user can belong to one or many team or groups
One user can belong to one or many roles
I have some hard time understanding the relation between those tables.
Here is what I managed to achieve based on the answer from @Robertas Valeika.
Upvotes: 2
Views: 6750
Reputation: 126
You need 3 more tables. UsersRoles UsersRolesGroups UsersRolesTeams.
Relationships:
UsersRoles - UsersRolesGroups, Groups - UsersRolesGroups
UsersRoles - UsersRolesTeams, Teams - UsersRolesTeams
Users - UsersRoles, Roles - UsersRoles.
And add FK to users in groups and teams tables to identify creator of group and team.
Upvotes: 4