If one post has many comments, and the comments are essentially the same as posts (e.g. they have a title, pictures and audio etc.) should I create two tables or just one?
如果一个帖子有很多评论,并且评论基本上和帖子一样(例如,他们有一个标题,图片和音频等等),我应该创建两个表格还是一个表格?
For example, if I only use one table I can have a parent_id column, so If it's not a reply to anything it would be null, otherwise, it would have the id of the parent post. On the other hand I can create a post table and a comments table. Comments can also reply back to other comment so this could get confusing quick.
例如,如果我只使用一个表,我可以有一个parent_id列,因此如果它不是任何东西的应答,它将是null,否则,它将具有父post的id。另一方面,我可以创建一个post表和一个comments表。评论也可以回复其他评论,因此这可能会很快让人感到困惑。
*Post*
id
title
content
image
audio
parent_id
or,
或者,
*Post* *Comments*
id id
title title
content content
image author_id
audio post_id
author_id image
audio
What the second option would allow is creating indexes. Infact I won't even have to add author_id or post_id If I use indexes from the start will I?
第二个选项允许的是创建索引。事实上,如果我从一开始就使用索引,我甚至不需要添加author_id或post_id,对吗?
What are you thoughts on this SO? Which would be more efficient? I thinking of using redbeanphp for this.
你对此有什么看法?哪种更有效?我想用redbeanphp来做这个。
2 个解决方案
#1
2
The second option would be better. When displaying a message board, you don't care about comments and looking them up by an indexed parent post id column is fast. Posts and comments will likely have different fields, so keeping them separate is correct. The parent id index for the first option would work fine, but conceptually, it's messy and you're basically creating an index to use on half or however many comments there are relative to posts.
第二个选择会更好。当显示留言板时,您不需要关心注释,通过索引的父post id列查找注释是很快的。文章和评论可能有不同的字段,所以保持它们的独立是正确的。第一个选项的父id索引可以工作得很好,但是从概念上来说,它是混乱的,您基本上是创建一个索引来使用一半的内容,或者不管有多少与文章相关的评论。
#2
0
As a rule in database-design: Tables are called entities, so each entity in your application should be separated and demonstrated by table. Here, although you regarded posts and comments each has the same kind of data but finally each of them is a separate entity so they should be separated in two tables. This behavior is not a personal opinion. It is basic rule that leads to more smooth application development.
作为数据库设计中的一条规则:表被称为实体,因此应用程序中的每个实体都应该通过表进行分离和演示。这里,虽然你认为每个帖子和评论都有相同类型的数据,但最后它们都是一个单独的实体,所以它们应该在两个表中分开。这种行为不是个人观点。这是使应用程序开发更加顺利的基本规则。
#1
2
The second option would be better. When displaying a message board, you don't care about comments and looking them up by an indexed parent post id column is fast. Posts and comments will likely have different fields, so keeping them separate is correct. The parent id index for the first option would work fine, but conceptually, it's messy and you're basically creating an index to use on half or however many comments there are relative to posts.
第二个选择会更好。当显示留言板时,您不需要关心注释,通过索引的父post id列查找注释是很快的。文章和评论可能有不同的字段,所以保持它们的独立是正确的。第一个选项的父id索引可以工作得很好,但是从概念上来说,它是混乱的,您基本上是创建一个索引来使用一半的内容,或者不管有多少与文章相关的评论。
#2
0
As a rule in database-design: Tables are called entities, so each entity in your application should be separated and demonstrated by table. Here, although you regarded posts and comments each has the same kind of data but finally each of them is a separate entity so they should be separated in two tables. This behavior is not a personal opinion. It is basic rule that leads to more smooth application development.
作为数据库设计中的一条规则:表被称为实体,因此应用程序中的每个实体都应该通过表进行分离和演示。这里,虽然你认为每个帖子和评论都有相同类型的数据,但最后它们都是一个单独的实体,所以它们应该在两个表中分开。这种行为不是个人观点。这是使应用程序开发更加顺利的基本规则。