数据库用户和组模型
我有 2 个实体:用户和组。最简单的模型是 user(id,name,etc...), group(id,name), user_group_rel(user_id,group_id)。但我需要将组纳入其他组。一个用户可以属于多个组,并且组可以包含具有自己的用户和子组的用户和子组! 需要数据库模型方面的帮助!
I have 2 entities: users and groups. Easiest model is user(id,name,etc...), group(id,name), user_group_rel(user_id,group_id). But I need to include groups into other groups. One user can be in many groups and groups can include users and subgroups with own users and subgroups!
Need help with database model!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
虽然我同意 Ken Down 的命名法,但我不一定同意用户和角色是同一实体。
基础实体:
关联实体:
While I agree with Ken Down's nomenclature, I don't necessarily agree that users and roles are the same entity.
Base Entities:
Associative entities:
如今,这是通过将用户和组折叠为“角色”来完成的。您拥有的只是角色以及角色与其他角色的链接。某些角色的“登录”标志设置为 true,这使得它们实际上是用户,而没有设置“登录”标志的角色更像是组。
主要思想是这让一切变得更简单。您不必维护两个单独的概念或实体(用户和组),而只需维护一个概念:角色。然后您为用户使用“登录”标志。
编辑:对于表结构,请查看@XIVSolutions提供的第一个选项,他详细说明了我上面提到的内容。它回答了您有关将一个角色放入任意数量其他角色的问题。第二个表(交叉引用)列出了角色及其父角色。如果一个角色有多个父级,这意味着该表中有多个条目,那么这就像在多个组中拥有一个用户。
#DIT:同样回复:@XIVSolutions 表设计,第三个表是将用户(登录名)作为单独管理实体的完全合法的方式。
These days this is done by collapsing both users and groups into "Roles". All you have are roles, and a linking of roles into other roles. Some roles have a "login" flag set to true which makes them in effect users, while roles that do not have the "login" flag set are more like groups.
The main idea is this makes everything simpler. You don't have to maintain two separate concepts or entities (user and group), but only one concept: role. Then you use that "login" flag for users.
EDIT: For table structure look at the first option provided by @XIVSolutions, he spells out what I referred to above. It answers your question about putting one role into any number of other roles. The second table, the cross-reference, lists a role and its parent. If a role has multiple parents, meaning multiple entries in that table, then this is like having one user in many groups.
#DIT: Also re:@XIVSolutions table design, that third table is a perfectly legitimate way to have users (logins) as separately managed entities.
回复:Ken Downs 解决方案:
我玩过这个的两个版本(并且很想听听这是否达到目标......):
选项 1:
或者选项 2:
re: Ken Downs solution:
I've played with two versions of this (and would LOVE to hear if this is on-target or not . . .):
Option 1:
Or, option 2:
建模的主要症结在于如何将用户和组视为组的成员。肯·唐斯的方法将解决这个问题。另一种方法是将用户和组视为公共基本类型的派生类型:
此方法的一个优点是它可以防止用户或组意外成为用户的成员 - 此限制是在架构级别强制执行的。
The main sticking point for modelling this is how to treat both users and groups as members of groups. Ken Downs's approach will solve this problem. Another approach is to treat users and and groups as a derived type of a common base type:
One advantage of this approach is that it prevents a user or group from accidentally becoming a member of a user - this restriction is enforced at the schema level.
Paul Keister 的方案是什么样的。这是 JetBrains DataGrip IDE 自动生成的图表。
What Paul Keister's scheme looks like. This is an auto generated diagram from JetBrains DataGrip IDE.