如何处理具有多个父项的表
我在对我正在处理的特定数据库结构进行建模时遇到问题。简而言之,考虑以下因素:
- 一个网页上可以有一个或多个话题
- 一个话题由一条或多条评论组成
- 评论可以针对它提出一项或多项投诉
- 还可以针对整个话题提出投诉
- 投诉也可以根据页面提交
我不太清楚如何在数据库级别对此进行建模。前三个很简单:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将把投诉创建为一个实体,然后在它可以关联的所有不同事物之间建立链接表。
我有以下表格...
这是瓦利德解决方案的一个略有不同的变体。与所有类似的问题一样,有很多方法可以解决它:)
这种方法的优点是您可以使用外键来维护数据完整性。缺点是,每当您需要抱怨新的“事物”时,您将需要一个新的链接表,但我想您无论如何都必须创建一个新的“事物”表。
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 ...
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.
我想到的一个解决方案是拥有一张表:
那么您的投诉表可以如下所示:
当然,这可能会在稍后查询投诉表并与其他表连接时添加额外的工作,但这完全取决于您的内容想查询。
One solution off the top of my head is to have a table:
Then your complaint table can be as follows:
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.
另一种方法是创建一个新的实体表,该表与 3 个表(网页、话题、评论)具有超类型/子类型关系:
这样,创建一个新网页(或话题或评论)或删除一个会稍微复杂一些(在两个表中插入或删除新行比在一个表中插入或删除新行。)
Another approach is to have a new
entity
table that has a supertype/subtype relationship with the 3 tables (webpage, thread, comment):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.)