评论系统数据库

发布于 2024-09-27 15:24:57 字数 511 浏览 1 评论 0原文

我想创建一个 2 级状态消息系统。创建表的最佳方法是什么?

范围:

  1. 用户设置状态消息
  2. 用户回复状态消息

这是显示它的图片

alt text

表格 i已创建

用户(id、名称...) status_messages(id、消息、时间、user_id) status_message_replies (id, message, time, status_message_id, user_d)

有人建议这可以用单表格式来完成

status_messages (id, pid, message, time, user_id)

where pid = selfId 或 ParentId of状态。

我想知道创建系统的最佳方法是什么?

i wanna create a 2 level status message system. Which is the best way to create a tables ?

Scope:

  1. User sets a Status Message
  2. Users Reply to the status message

this is a picture showing it

alt text

Tables i have created

users (id, name .... )
status_messages (id, message, time, user_id)
status_message_replies (id, message, time, status_message_id, user_d)

Some one suggested this can be done in a single table format

status_messages (id, pid, message, time, user_id)

where pid = selfId or ParentId of the status.

I wanna know which is the best method to create the system ?

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

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

发布评论

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

评论(2

相对绾红妆 2024-10-04 15:24:57

只要原始消息和响应具有相同的结构(属性集或列),那么您就可以使用单表方法。它的优点是您可以通过单个查询搜索原始消息和响应。

可以在 pid = selfid 处找到原始消息集,并且可以在 pid <> 处找到响应。自我。如果能够分别查看原始消息和响应消息(无需了解存储机制)很重要,您可以将上述条件封装在两个视图中:OriginalMessages 和 Responses。

如果原件和响应具有不同的属性(例如,如果您希望原件允许链接到 URL、照片等),您可以考虑使用两个单独的表。但即使在那里,我也可能会主张采用一个表结构,并使用一个单独的扩展表来存储附加属性。这意味着您不必为那些不使用扩展属性的原始消息存储经常为空的列,并且您稍后也可以轻松地将扩展属性添加到响应消息中(如果需要)。

As long as the original messages and the responses have the same structure (set of attributes, or columns) then you can use the single table approach. It has the advantage that you can search over original messages and responses with a single query.

The set of original messages can be found where pid = selfid and the responses where pid <> selfid. If it's important to be able to see the original and response messages separately (without knowledge of the storage mechanism) you can encapsulate the above conditions in two VIEWs: OriginalMessages and Responses.

If the originals and responses have different attributes (for instance, if you want the original to allow links to URLs, photos, etc) you might consider using two separate tables. But even there, I'd probably argue for the one table structure with a separate, extender table for the additional attributes. That means you don't have to store often-empty columns for those original messages that don't use the extended attributes, and you can later easily add the extended attributes to the response messages as well (if desired).

甜宝宝 2024-10-04 15:24:57

经典的 IS-A 关系:每个回复都是一条带有额外属性的消息(它是回复的消息)。
这可能不是建模的最佳方法。您将面临必须对这两个表编写大量 UNION 查询的风险。

替代方案:

  • 只有一张表:status_messages (id, message, time, status_message_id, user_id),并允许 status_message_id 为 NULL,
  • 使用 HAS-A:一张表 status_messages (id, message, time, user_id) 和一张表 replies (reply_id, replies_to_id)

前者的缺点是在 SQL 中使用 NULL 很棘手。
当您想要专门查询回复时,后者将需要联接。

顺便说一句,根据列所代表的关系而不是它们所引用的表来命名列要清楚得多(IMO)。

A classical IS-A relationship: every reply is a message with an extra attribute (the message it is a reply to).
This is probably not the best way to model it. You'll be running the risk of having to write a lot of UNION queries over those two tables.

Alternatives:

  • just one table: status_messages (id, message, time, status_message_id, user_id), and allowing status_message_id to be NULL
  • use a HAS-A: one table status_messages (id, message, time, user_id) and one table replies (reply_id, replies_to_id

The former has the disadvantage that working with NULL is tricky in SQL.
The latter will necessitate joins when you want to query replies specifically.

BTW it's much clearer (IMO) to name columns after the relationship they stand for, not the table they refer to.

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