关系“OR”在关系数据库中

发布于 2024-12-07 18:46:23 字数 293 浏览 1 评论 0原文

我想这个问题很微不足道。但尽管如此, 例如,我有实体:用户(id,名称),组(id,名称),user_group(user_id,group_id)和画廊(id,名称,owner_id)。 图库的所有者可以是用户或组

关系数据库中最好的解决方案是什么?

谢谢!

PS 如果有人知道关系代数和模式优化。它会是什么样子?

我正在考虑 Owner (id, user_id, group_id),但我不知道如何用关系代数来表示“OR”关系。

The question is pretty trivial I guess. But nevertheless,
E.g., I have Entities: user (id, name), group (id, name), user_group (user_id, group_id) and gallery (id, name, owner_id).
Owner of the gallery could be user OR group.

What's the best solution for this in relational databases?

Thanks!

PS If anybody knows relational algebra and schema optimization. How will it look like?

I was thinking about Owner (id, user_id, group_id), but I don't have any idea how to show "OR" relation in terms of relational algebra.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荭秂 2024-12-14 18:46:23

最简单的解决方案是一个关系 Owner(id, user_id, group_id),其中可以设置 user_idgroup_id ——用一个适当的约束。

GroupUser 折叠到一张表中会将多个一致性检查从数据库拖到应用程序逻辑中:

  • 一个组不仅可以拥有用户,还可以拥有其他组(递归和/或无限循环)前面)。
  • 如果在更多地方使用UserGroup,则可能需要进行更多调整。
  • 数据库无法再强制执行数据一致性。

The simplest solution would be a relation Owner(id, user_id, group_id) where either user_id or group_id can be set -- guard that with an appropriate constraint.

Collapsing Group and User into one table drags several consistency checks from the database into the application logic:

  • A group could have not only users but other groups (recursion and/or infinit cylces ahead).
  • If User and Group are used in some more places more adaptions might be necessary.
  • Data consistence cannot be enforced by the database any more.
他夏了夏天 2024-12-14 18:46:23

将所有者和组合并到一张表中。所有者和组的区别仅在于它们在此表中具有的属性,或者将新表与其自身连接在一起的关系表中存在/不存在行(“属于……的成员”)。

Combine owner and group into one table. Owner and group should only differ by the attributes they have in this table, or the presence / absence of rows in a relationtable joining this new table with itself ("is member of" ).

你又不是我 2024-12-14 18:46:23

用户和组之间没有技术上的区别(只是概念上的区别)。

将它们放入同一个表 (user) 中,并在第二个字段中标记行的类型(组或用户)。

使用应用程序逻辑确保在 user_group 表中只有 group 类型的行可以“有子级”。

There is no technical difference between a user and a group (only a conceptual one).

Put them both into the same table (user) and flag a row's type (group or user) in a second field.

Use application logic to make sure that only a row of type group may "have children" in the user_group table.

鹊巢 2024-12-14 18:46:23

引入一个新实体 OWNER 并使其成为 GROUP 的所有者。然后将 USER 和 GROUP 类别设为 OWNER(即“继承”它们)。

您的 ER 模型将如下所示(仅显示 PK 字段):

在此处输入图像描述

理论上,有 3 种主要方法在物理数据库中实现一个类别。它们都有优点和缺点,但对于您的模型来说,解决方案 1 可能是最合适的:

  1. 为 OWNER、USER 和 GROUP 使用单独的表,并通过 FOREIGN KEY 连接它们。
    • 在这种情况下,您可能会也可能不会在 OWNER 中使用鉴别符(即类型标识符)。
  2. 将 USER 和 GROUP 放在单独的表中,并且 OWNER 的字段都存在于两个表中。
  3. 将 OWNER、USER 和 GROUP 放在同一个表中。

Introduce a new entity OWNER and make it owner of the GROUP. Then make USER and GROUP categories of (i.e. "inherit" them from) OWNER.

Your ER model would look like this (only PK fields shown):

enter image description here

Theoretically, there are 3 major ways to implement a category in the physical database. All of them have pros and cons, but for your model, the solution 1 is probably the most appropriate:

  1. Use separate tables for OWNER, USER and GROUP and connect them via FOREIGN KEYs.
    • In this scenario, you may or may not use the discriminator (i.e. type identifier) in OWNER.
  2. Put USER and GROUP in the separate tables, with OWNER's fields present in both.
  3. Put OWNER, USER and GROUP in the same table.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文