额外的表或非特定的外键?

发布于 2024-07-04 20:08:32 字数 297 浏览 7 评论 0原文

系统中有多种类型的对象,每种对象在数据库中都有自己的表。 用户应该能够对其中任何一个进行评论。 您将如何设计评论表? 我可以想到几个选项:

  1. 一个注释表,每个对象类型都有一个 FK 列(ObjectAID、ObjectBID 等)
  2. 多个注释表,每个对象类型一个(ObjectAComments、ObjectBComments 等)
  3. 一个通用 FK (ParentObjectID),其中另一列指示类型(“ObjectA”)

您会选择哪一个? 还有我没有想到的更好的方法吗?

There are several types of objects in a system, and each has it's own table in the database. A user should be able to comment on any of them. How would you design the comments table(s)? I can think of a few options:

  1. One comments table, with a FK column for each object type (ObjectAID, ObjectBID, etc)
  2. Several comments tables, one for each object type (ObjectAComments, ObjectBComments, etc)
  3. One generic FK (ParentObjectID) with another column to indicate the type ("ObjectA")

Which would you choose? Is there a better method I'm not thinking of?

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

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

发布评论

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

评论(5

爱要勇敢去追 2024-07-11 20:08:32

我喜欢做的一件事是拥有一个单独的表,将通用/公用表链接到所有个性化表。

因此,对于对象 Foo 和 Bar,然后对 Foo 和 Bar 进行注释。 酒吧,你会得到这样的东西:

  • Foo
    • Foo ID(PK)
  • 酒吧
    • 酒吧 ID (PK)
  • 评论
    • 评论 ID (PK)
    • 评论文字
  • FooComment
    • Foo ID(PK FK)
    • 评论 ID (PK FK)
  • BarComment
    • 酒吧 ID (PK FK)
    • 评论 ID (PK FK)

这种结构:

  1. 让您拥有一个公共 Comments 表
  2. 不需要具有表继承的 DB
  3. 不会用 Comment 相关信息污染 Foo 和 Bar 表
  4. 让我们您将注释附加到多个对象(这可能是需要的)。
  5. 如果需要,您可以将其他属性附加到 Foo/Bar 和 Comment 的连接处。
  6. 仍然保留与标准(即:快速、简单、可靠)外键的关系

One of the things I like to do is have a separate tables that link the generic/common table to all of the individualized tables.

So, for objects Foo and Bar and then comments on Foo & Bar, you'd have something like this:

  • Foo
    • Foo ID (PK)
  • Bar
    • Bar ID (PK)
  • Comment
    • Comment ID (PK)
    • Comment Text
  • FooComment
    • Foo ID (PK FK)
    • Comment ID (PK FK)
  • BarComment
    • Bar ID (PK FK)
    • Comment ID (PK FK)

This structure:

  1. Lets you have a common Comments table
  2. Doesn't require a DB with table inheritance
  3. Doesn't pollute the Foo and Bar tables with Comment-related information
  4. Lets you attach a Comment to multiple objects (which can be desireable)
  5. Lets you attach other properties to the junction of Foo/Bar and Comment if so desired.
  6. Still preserves the relations with standard (ie: fast, simple, reliable) foreign keys
拔了角的鹿 2024-07-11 20:08:32

设计架构以使可注释(由于缺乏更好的词)表遵循标准继承建模模式之一是否可行? 如果是这样,您可以让注释表的 FK 指向公共父表。

Is it feasible to design the schema so that the commentable (for lack of a better word) tables follow one of the standard inheritance-modeling patterns? If so, you can have the comment table's FK point to the common parent table.

半世蒼涼 2024-07-11 20:08:32

@Hank Gay

所以类似:

  1. ObjectA
    • ObjectAID
    • 家长ID
  2. 对象B
    • ObjectBID
    • 家长ID
  3. 注释
    • 评论ID
    • 家长ID
  4. 父母
    • 家长ID

@Hank Gay

So something like:

  1. ObjectA
    • ObjectAID
    • ParentID
  2. ObjectB
    • ObjectBID
    • ParentID
  3. Comments
    • CommentID
    • ParentID
  4. Parents
    • ParentID
我的奇迹 2024-07-11 20:08:32

请小心不完全指向一张表的通用外键。 如果必须拆分类型上的 where 条件并指向多个不同的表,查询性能会受到极大影响。 如果您只有几种类型,并且类型的数量不会增长,那么可以为不同的表设置单独的可为空的外键,但如果您有更多类型,最好提出不同的数据模型(例如@palmsey 的建议)。

Be careful with generic foreign keys that don't point to exactly one table. Query performance suffers dramatically if you have to split the where condition on a type and point to several different tables. If you only have a few types, and the number of types will not grow, it's Ok to have separate nullable foreign keys to the different tables, but if you will have more types, it's better to come up with a different data model (like @palmsey's suggestion).

情栀口红 2024-07-11 20:08:32

@palmsey

差不多了,但是我最常看到的该模式的变化摆脱了 ObjectAID 等。 ParentID 成为Parents 的 PK 和 FK。 这会让你得到类似的东西:

  • Parents

    • ParentID
  • ObjectA

    • ParentID(FK 和 PK)
    • ColumnFromA NOT NULL
  • ObjectB

    • ParentID(FK 和 PK)
    • ColumnFromB NOT NULL

Comments 将保持不变。 然后,您只需要限制 ID 生成,这样您就不会意外地得到一个 ObjectA 行和一个 ObjectB 行,它们都指向相同的 Parents< /代码> 行; 最简单的方法是对 ObjectAObjectB 使用与 Parents 相同的序列(或其他序列)。

您还可以看到很多模式,例如:

  • Parents
    • ID
    • 子类鉴别器
    • ColumnFromA(可为空)
    • ColumnFromB(可为空)

注释 将保持不变。 但现在,如果不编写触发器或在不同的层执行此操作,则无法强制执行所有业务约束(子类的属性均可为空)。

@palmsey

Pretty much, but the variation on that pattern that I've seen most often gets rid of ObjectAID et al. ParentID becomes both the PK and the FK to Parents. That gets you something like:

  • Parents

    • ParentID
  • ObjectA

    • ParentID (FK and PK)
    • ColumnFromA NOT NULL
  • ObjectB

    • ParentID (FK and PK)
    • ColumnFromB NOT NULL

Comments would remain the same. Then you just need to constrain ID generation so that you don't accidentally wind up with an ObjectA row and an ObjectB row that both point to the same Parents row; the easiest way to do that is to use the same sequence (or whatever) that you're using for Parents for ObjectA and ObjectB.

You also see a lot of schemas with something like:

  • Parents
    • ID
    • SubclassDiscriminator
    • ColumnFromA (nullable)
    • ColumnFromB (nullable)

and Comments would remain unchanged. But now you can't enforce all of your business constraints (the subclasses' properties are all nullable) without writing triggers or doing it at a different layer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文