如何处理具有多个父项的表

发布于 2024-11-26 07:50:46 字数 537 浏览 0 评论 0原文

我在对我正在处理的特定数据库结构进行建模时遇到问题。简而言之,考虑以下因素:

  • 一个网页上可以有一个或多个话题
  • 一个话题由一条或多条评论组成
  • 评论可以针对它提出一项或多项投诉
  • 还可以针对整个话题提出投诉
  • 投诉也可以根据页面提交

我不太清楚如何在数据库级别对此进行建模。前三个很简单:

webpage
----------
id
name

thread
---------
id
page_id
name

comment
--------
id
thread_id
name

但如果我想要一个投诉表,该如何建模呢?我认为您不想这样做:

complaint
----------
id
page_id
thread_id
comment_id

如果您添加了新的对象类型(例如图片),则必须在投诉中添加更多列。有没有更好的方法来做到这一点,或者是最好的方法?

提前致谢, - 安东尼

I'm having trouble modeling a particular database structure I'm working on. To be short, considering the following:

  • A webpage can have one or more threads on it
  • A thread consists of one or more comments
  • Comments can have one or more complaints filed against it
  • Complaints can also be filed against the thread as a whole
  • Complaints can also be filed against the page

I can't quite figure out how to model this at the DB level. The first three are easy:

webpage
----------
id
name

thread
---------
id
page_id
name

comment
--------
id
thread_id
name

But if I wanted a single table of complaints, how would one model that? I don't think you would want to do:

complaint
----------
id
page_id
thread_id
comment_id

If you ever added a new object type, like picture, you'd have to add more columns to the complaint. Is there a better way to do this, or is at as good as it gets?

Thanks in advance,
- Anthony

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

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

发布评论

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

评论(3

笑咖 2024-12-03 07:50:46

我将把投诉创建为一个实体,然后在它可以关联的所有不同事物之间建立链接表。

我有以下表格...

  • 投诉
  • complaint_comment_linkcomplaint_thread_linkcomplaint_page_link
  • 因此

这是瓦利德解决方案的一个略有不同的变体。与所有类似的问题一样,有很多方法可以解决它:)

这种方法的优点是您可以使用外键来维护数据完整性。缺点是,每当您需要抱怨新的“事物”时,您将需要一个新的链接表,但我想您无论如何都必须创建一个新的“事物”表。

I would create the complaint as an entity in it's own right, then have link table between all the different things it can be associated with.

So, I'd have the following tables ...

  • complaint
  • compliant_comment_link
  • complaint_thread_link
  • complaint_page_link

This is a slightly different variation on Waleed's solution. As with all things like this, there are many ways to solve it :)

The advantage of this approach is that you can have foreign keys to maintain data integrity. The disadvantage is that whenever you need to have complaint against a new "thing" you will need a new link table, but I suppose you'd have to create a new "thing" table anyway.

相思故 2024-12-03 07:50:46

我想到的一个解决方案是拥有一张表:

ObjectType

-------------------
|  id  |   name   |
-------------------
|   1  | Webpage  |
|   2  | Thread   |
|   3  | Comment  |
-------------------

那么您的投诉表可以如下所示:

----------------------------------------
|  id  |   object_type_id   | objectid |
----------------------------------------
|   1  |         1          |    1     |
|   2  |         1          |    2     |
|   3  |         2          |    1     |
---------------------------------------|

当然,这可能会在稍后查询投诉表并与其他表连接时添加额外的工作,但这完全取决于您的内容想查询。

One solution off the top of my head is to have a table:

ObjectType

-------------------
|  id  |   name   |
-------------------
|   1  | Webpage  |
|   2  | Thread   |
|   3  | Comment  |
-------------------

Then your complaint table can be as follows:

----------------------------------------
|  id  |   object_type_id   | objectid |
----------------------------------------
|   1  |         1          |    1     |
|   2  |         1          |    2     |
|   3  |         2          |    1     |
---------------------------------------|

Of course this could add additional work later on when querying the complaint table and joining with the others, but that all depends on what you want to query.

迷路的信 2024-12-03 07:50:46

另一种方法是创建一个新的实体表,该表与 3 个表(网页、话题、评论)具有超类型/子类型关系:

entity
----------
id (PK)

webpage
----------
id (PK)
name
FOREIGN KEY id REFERENCES entity(id)

thread
---------
id (PK)
page_id
name
FOREIGN KEY id REFERENCES entity(id)

comment
--------
id (PK)
thread_id
name
FOREIGN KEY id REFERENCES entity(id)

complaint
----------
id (PK)
entity_id 
FOREIGN KEY entity_id REFERENCES entity(id)

这样,创建一个新网页(或话题或评论)或删除一个会稍微复杂一些(在两个表中插入或删除新行比在一个表中插入或删除新行。)

Another approach is to have a new entity table that has a supertype/subtype relationship with the 3 tables (webpage, thread, comment):

entity
----------
id (PK)

webpage
----------
id (PK)
name
FOREIGN KEY id REFERENCES entity(id)

thread
---------
id (PK)
page_id
name
FOREIGN KEY id REFERENCES entity(id)

comment
--------
id (PK)
thread_id
name
FOREIGN KEY id REFERENCES entity(id)

complaint
----------
id (PK)
entity_id 
FOREIGN KEY entity_id REFERENCES entity(id)

This way, creating of a new webpage (or thread or comment) or deleting one will be slightly more complicated (inserting or deleting a new row in two tables than one.)

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