关于多表数据库设计问题的评论

发布于 2024-11-02 08:52:03 字数 243 浏览 1 评论 0原文

我有表格:

Articles{...}
Recipes{...}
Notifications{...}
Photos{...}

我需要实现“用户评论”功能(如facebook)。 我应该以 1:n 关系制作表格:ArticleComments、RecipesComments 等吗? 或者为所有人创建一个 Comments 表(但我不知道如何设计它)?

I have tables:

Articles{...}
Recipes{...}
Notifications{...}
Photos{...}

And I need to implement 'user comments' feature (like facebook).
Should I make tables: ArticleComments, RecipesComments etc. with 1:n relationship?
Or create one Comments table for all (but I have no idea how to design this)?

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

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

发布评论

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

评论(5

雨后彩虹 2024-11-09 08:52:04

这取决于您的应用程序将如何使用注释。

我的猜测是,您经常想要提取用户创建的所有评论,无论他们评论的实体是什么。也就是说,我假设您经常需要一个查询,该查询返回指示用户 JohnDoe 对第 1 条、照片 12、食谱 171 发表评论的行。如果是这种情况,那么使用单个 Comments 表的结构类似于 Steve Mayne 建议的 CommentableEntity 表。

另一方面,如果您只想访问特定项目的评论(即第 1 条的所有评论),则单独的 ArticleCommentsPhotoComments 表可能更合适。这使得在实体表和注释表之间使用外键变得更容易,并且可能会更高效,因为它是一个穷人的分区。当然,一旦您开始必须组合来自多个评论表的数据,这种效率就会消失,因此您需要对用例有相当的信心。

That depends on how your application will be using comments.

My guess is that you'll frequently want to pull up all the comments a user has created regardless of the entity that they are commenting on. That is, I assume you'll frequently want a query that returns rows indicating that user JohnDoe commented on Article 1, then Photo 12, then Recipe 171. If that's the case, then it would make far more sense to have a single Comments table with a structure similar to what Steve Mayne has suggested with the CommentableEntity table.

On the other hand, if you would only be accessing the comments for a particular item (i.e. all comments for Article 1), separate ArticleComments and PhotoComments tables may be more appropriate. That makes it easier to have foreign keys between the entity table and the comment table and is potentially a bit more efficient since it's a poor man's partitioning. Of course, as soon as you start having to combine data from multiple comment tables, this efficiency goes away so you'd need to be reasonably confident about the use cases.

尛丟丟 2024-11-09 08:52:04

最简单的方法是拥有一个“多态”注释表,其中包含它所引用的对象的 id 和类型的列。

您可以执行以下操作:

SELECT * FROM Comments where type = "Articles" and type_id = 1;
SELECT * FROM Comments where type IN ("Recipes", "Photos")

在 (type, id) 上放置唯一的复合索引也会提高查找的性能。

The easiest way would to have a 'polymorphic' comments table that would have columns for both the id and the type of the object that it refers to.

The you could do the following:

SELECT * FROM Comments where type = "Articles" and type_id = 1;
SELECT * FROM Comments where type IN ("Recipes", "Photos")

Putting a unique compound index on (type, id) would also improve the performance of the look ups.

阳光①夏 2024-11-09 08:52:04
SELECT TOP 1000 [Comments_Id]
      ,[Comments_Text]
      ,[Comments_IsApproved]
      ,[Comments_IsVisible]
      ,[Comments_DateStamp]
      ,[Type_Id]
      ,[Entity_Id] -- From Entity Table, listing Articles, Recipes etc. 
      ,[EntityItem_Id] -- One of the PK from table of Articles, Recipes etc.
      ,[User_Id]
  FROM [tbl_Comments]
SELECT TOP 1000 [Comments_Id]
      ,[Comments_Text]
      ,[Comments_IsApproved]
      ,[Comments_IsVisible]
      ,[Comments_DateStamp]
      ,[Type_Id]
      ,[Entity_Id] -- From Entity Table, listing Articles, Recipes etc. 
      ,[EntityItem_Id] -- One of the PK from table of Articles, Recipes etc.
      ,[User_Id]
  FROM [tbl_Comments]
春庭雪 2024-11-09 08:52:04

要了解如何为所有对象创建单个 Comments 表,您可以查看 django comment model ( http://docs.djangoproject.com/en/dev/ref/contrib/comments/models/< /a> )

To have an idea on how to create a single Comments table for all objects, you can take a look at django comment model ( http://docs.djangoproject.com/en/dev/ref/contrib/comments/models/ )

徒留西风 2024-11-09 08:52:03

您可以创建另一个表CommentableEntity(尽管称其为更好的名称)。表中的每一行(ArticlesRecipes 等)都将引用该表中的唯一行。实体表可能有一个type字段来指示实体的类型(以帮助反向连接)。

然后,您可以拥有一个以通用方式引用 CommentableEntityComment 表。

例如,您最终会得到以下表格:

Articles
-----------------
Article_id
CommentableEntity_id (fk, unique)
Content
....

Recipes
-----------------
Recipe_id
CommentableEntity_id (fk, unique)
Content
....

CommentableEntity
-----------------
CommentableEntity_id (pk)
EntityType (e.g. 'Recipe', 'Article')

Comment
-------
Comment_id (pk)
CommentableEntity_id (fk)
User_id (fk)
DateAdded
Comment 
...etc...

每次添加文章/食谱等时,您都可以添加 CommentableEntity 记录。您所有的评论处理代码必须知道的是 CommentableEntity_id - 它不关心什么类型事情就是这样。

You could create another table CommentableEntity (although call it something better). Each of the rows in your tables (Articles, Recipes etc.) would have a reference to a unique row in this table. The entity table might have a type field to indicate the type of entity (to aid reverse joining).

You can then have a Comment table that references CommentableEntity, in a generic fashion.

So for example you'll end up with the following tables:

Articles
-----------------
Article_id
CommentableEntity_id (fk, unique)
Content
....

Recipes
-----------------
Recipe_id
CommentableEntity_id (fk, unique)
Content
....

CommentableEntity
-----------------
CommentableEntity_id (pk)
EntityType (e.g. 'Recipe', 'Article')

Comment
-------
Comment_id (pk)
CommentableEntity_id (fk)
User_id (fk)
DateAdded
Comment 
...etc...

You can add the CommentableEntity record every time you add an Article/Recipe etc. All your comment-handling code has to know is the CommentableEntity_id - it doesn't care what type of thing it is.

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