在你的数据库设计中有循环关系总是不好的做法吗

Is it always a bad practice to have circular relationships in your database design

我不知道如何解决这个问题 我有以下关系

用户有 -> 很多帖子

一个帖子有 -> 很多评论

一个评论有->一个用户(所有者)

我一直在阅读数据库设计人员的这个论点,认为这是不应该这样做的,但我从未阅读过这些问题的任何解决方案,例如:

This month, instead of starting with an idea and creating a model to support the concept, I'll analyze a poor design construct: a situation I call the circular reference. A circular reference is a recursive condition in which one table references a second table, which in turn references the first table.

在这种情况下循环引用可以吗?

如果两个"users"值相同,我认为你违反了"no redundant info"原则。但是,评论的"owner"不一定是post的"owner"。因此,您不会冗余地存储 相同的 用户。

关系不必采用树的形式。如果有一些规则规定关系只能是 DAG(有向无环图),那么这个问题就有意义了。

让我们换个角度看数据。您有实体:用户、帖子、评论。你有你提到的关系。那么,谁在乎"circular"?

你们甚至可以 "users" "married" 彼此。没有比这更循环的了!

但是,那么,我们不应该做你的功课,尤其是晚了一年多。

我同意@DavidAldridge 的评论,我编辑了问题中的图片以帮助解释评论的含义。

就数据库中的循环引用而言,我的经验是您可以构建它们并且简单的查询和更新将起作用,但是一旦您需要更复杂的连接,您最终会创建笛卡尔连接的变体并快速消耗大量内存和查询结果可能永远不会回来。

请注意,循环引用与 self-referencing 关系不同。 Self-referencing 表在数据库设计中很常见。您可以在 Self-Referencing Tables or a full tutorial on all Reference Types including Self-Reference.

上找到一个简单示例