数据库设计问题

发布于 2024-11-09 09:13:02 字数 740 浏览 0 评论 0原文

我正在为一个应用程序设计一个数据库,用户可以在其中对图片和内容发表评论。视频。

我是否应该为评论保留单独的表格,例如(图片评论和视频评论)
或者
我应该保留单个表用于评论和其他映射表,例如(图片_评论_映射和视频_评论_映射)。

哪一种方法更好?为什么?

详细信息
方法 1:

user
     id
     name

Picture
     id
     link
video
     id
     link

comment_video
     id
     user_id
     video_id
     comment
comment_picture
     id
     user_id
     picture_id
     comment

方法 2:

user
     id
     name

picture
     id
     link
video
     id
     link

comment
     id
     user_id
     comment

comment_picture_mapping
     id
     comment_id
     picture_id
comment_video_mapping
     id
     comment_id
     video_id

哪种方法更好?为什么?

I am designing a database for an application where a user can comment on pictures & videos.

Should I keep separate tables for comments like (Picture_comments & videos_comments)

OR
should I keep single table for comment and other mapping tables like (picture_comment_mapping & video_comment_mapping).

Which one will be better approach and why?

Detail
Approach 1:

user
     id
     name

Picture
     id
     link
video
     id
     link

comment_video
     id
     user_id
     video_id
     comment
comment_picture
     id
     user_id
     picture_id
     comment

Approach 2:

user
     id
     name

picture
     id
     link
video
     id
     link

comment
     id
     user_id
     comment

comment_picture_mapping
     id
     comment_id
     picture_id
comment_video_mapping
     id
     comment_id
     video_id

Which one is better approach and why?

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

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

发布评论

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

评论(4

临风闻羌笛 2024-11-16 09:13:02

我会使用你的方法 2,因为它更容易做“搜索所有评论”之类的事情

I would use your approach number 2 because its much easier to then do things like "Search all comments"

别靠近我心 2024-11-16 09:13:02

我将创建一个名为“Comment”的实体,并在该评论实体上添加与用户的关系映射。评论实体还将在一列上包含枚举值,说明其是针对图片还是针对视频。比如 -

enum MimeType {
    PICTURE, VIDEO;
}

class Comment {
    @ManyToONe
    private User user;
    @Enumerated
    private MimeType mimeType;
}

为什么我喜欢这种方法?因为它更干净,并且生成的实体会更少,而且我不必编写不同的查询来搜索不同类型的 mime 类型评论。由于注释位于差异表上,因此我可以延迟加载它们。

I would make an entity with name "Comment", and add relation mapping on on that comment entity with user. The comment entity will also contain enumerated value on one column saying if its for picture or its for video. Something like -

enum MimeType {
    PICTURE, VIDEO;
}

class Comment {
    @ManyToONe
    private User user;
    @Enumerated
    private MimeType mimeType;
}

Why I like this approach? Because its cleaner, and resulting entities will be less, and I do not have to write different queries to search for different types of mime type comments. And since comments are on diff table, i can load them lazily.

柠檬色的秋千 2024-11-16 09:13:02

您可以只有一个表注释,其中一个字段将指定注释的类型。这基本上是没有映射的选项 2,单个字段足以知道它是什么类型的评论。至于视频或图片 ID,您可以将它们视为通用对象,消费者可以根据类型或评论或对象 MIME 类型决定如何处理它们。这应该允许您将来添加更多类型的评论。

Comment
 id
 user_id
 comment_text
 comment_type
 linked_object_id

CommentObect
  id
  type
  object

You can have just one table comments where one field will specific the type of the comment. This is basically Option 2 with no mapping , a single field is enough to know what type of comment it is. As for the video or picture ID you can consider them as generic objects and the consumer can decide what to do with them based on the type or comment or object mime type. This should allow you to add more types of comments in the future.

Comment
 id
 user_id
 comment_text
 comment_type
 linked_object_id

CommentObect
  id
  type
  object
倾`听者〃 2024-11-16 09:13:02

您只需要大约三个实体(如果不包括用户实体则需要两个实体)。

您的方法 2 意味着特定评论可以涉及多个图片或视频。这就是你想要的吗?

用户 {one-to-many} 评论 {many-to-one} 媒体(媒体类型)

You only need about three entities (or two if you don't include the User entity).

Your approach 2 implies that a particular comment can be about more than one picture or video. Is that what you want?

User {one-to-many} Comment {many-to-one} media (media type)

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