SQL Server database design users, groups, roles, memebers

和自甴很熟 提交于 2019-12-08 06:13:22

问题


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

    • User1
    • User2
    • UserX
  • I have 3 roles only for all users to use in all teams and groups

    • Admin
    • Member
    • Visitor
  • One user can create X amount of teams

    • Team1
    • Team2
    • TeamX
  • One user can create X amount of groups

    • Group1
    • Group2
    • GroupX
  • 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.


回答1:


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.



来源:https://stackoverflow.com/questions/26145734/sql-server-database-design-users-groups-roles-memebers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!