简体   繁体   English

论坛帖子和回复的理想数据库表结构是什么?

[英]What's the ideal database table structure for forum posts and replies?

I have been trying to create an online discussion forum as a fun project.我一直在尝试创建一个在线讨论论坛作为一个有趣的项目。 In this application, registered users can create a "topic".在这个应用程序中,注册用户可以创建一个“主题”。 Other users can reply to that topic (lets call them "posts").其他用户可以回复该主题(我们称它们为“帖子”)。 Also, people can "reply" to posts.此外,人们可以“回复”帖子。 However, reply to "replies" is not required at present.但是,目前不需要回复“回复”。

I thought of creating a DB schema like this我想像这样创建一个数据库模式在此处输入图像描述

But, the posts and replies table basically contain the same kind of fields.但是,帖子和回复表基本上包含相同类型的字段。 Only exception is that- the topic_id column in posts table will refer to topics table and post_id column in replies table will refer to the posts table唯一的例外是——posts 表中的 topic_id 列将引用 topics 表,而 replies 表中的 post_id 列将引用 posts 表

So, if I have to keep only one table for posts and replies, how do I manage the relationship between "topics and posts" and "posts and replies"?那么,如果我只需要为帖子和回复保留一张表,我该如何管理“主题和帖子”与“帖子和回复”之间的关系呢? What would be the ideal solution in this case?在这种情况下,理想的解决方案是什么?

Or或者

Should I just keep my existing schema?我应该只保留现有架构吗?

Thanks!谢谢!

You can keep one table only for posts and replies. 您只能为发帖和回复保留一张桌子。 For the relationship between post and reply add a new column names post_id . 对于发布和回复之间的关系,添加新的列名称post_id This column can be null for post, and in the case of reply insert id of the post. 对于帖子,此列可以为null,如果是回复,则插入帖子的id

Replies are not posts, so use a different table.回复不是帖子,因此请使用不同的表格。 Tables don't cost anything.桌子不花钱。 1 reply can have many posts, having both in the same table is very messy and will make for very ugly sql when querying the data. 1个回复可以有很多帖子,都在同一个表中非常混乱,并且在查询数据时会非常难看 sql 。 Replies can have replies, so add a parent id to the reply table so you can drill up.回复可以有回复,所以在回复表中添加一个parent id,这样就可以向上钻取了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM