我应该对 2 个不同的父表使用 2 个相同的注释表,使用什么数据库结构
对于我正在开发的应用程序来说,这是一个棘手的设计问题。我的应用程序中有 2 个不同的项目,它们都将使用注释。但我无法决定如何设计我的数据库。
这里有两种可能性。第一个是每个需要评论的表都有一个不同的评论表(规范化方式):
movies -> movie_comments
articles -> article_comments
我想到的第二种方法是使用通用评论表,然后为评论和电影|文章关系建立多对多关系。例如,
comments
comments_movies (movie_id, comment_id)
comments_articles (article_id, comment_id)
您认为最好的方法是什么?您能否给出一个充分的理由,以便我做出决定。
This is a tough design question for a application I'm working on. I have 2 different items in my app that both will use comments. What but I can't decide how to design my database.
There are 2 possibilities here. The first is a different comment table for every table that needs comments (normalized way):
movies -> movie_comments
articles -> article_comments
The second way I was thinking of was the use of a generic comments table and then have a many 2 many relationship for the comment and movie|article relations. Eg
comments
comments_movies (movie_id, comment_id)
comments_articles (article_id, comment_id)
What is your opinion on that the best method would be and can you give a good reason so I can decide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我个人选择第二个解决方案
,仅在表模型上维护逻辑评论模型要简单得多,例如,当您不想向评论添加某些功能时,您只需执行一次,或者当您不想计算特定用户的评论时更容易,因为在一个表中
当然其他人可以写出将其保留在多个表中的优点,但是您征求了意见,所以这是我的:)
i personally opt for 2nd solution
it is much more simple to maintain only on table model for logical Comment model e.g. when You wan't to add some feature to comments You just do it once or when You wan't count comments for specific user is much more easier because there are in one table
of course someone else could write his advantages of keeping that in multiple tables but You asked for opinions so here is mine :)
将它们分开的好处是支持变革,而不影响其他实体(电影与文章)的评论。假设针对文章和电影的评论的属性存在差异。否则......
我想可能需要显示一篇文章和一部电影的评论。但如果您想将来为其他实体提供评论功能,合并也将支持。
答案取决于您当前需要什么,以及对您将来想要做什么的最佳猜测。更多细节可以帮助我们知道该提出什么建议。
Keeping them separate has the benefit of supporting change without impacting the comments for the other entity (movie vs articles). Assuming there are differences in attributes for a comment against an article vs. a movie. Otherwise...
I suppose there could be a need for displaying a comment with an article and a movie. But the consolidation would also support if you want to provide comment functionality for other entities in the future.
The answer depends on what you need currently, and a best guess of what you want to do in the future. More details help us to know what to suggest.
不存在“最佳”方法,因为这是一个直接的标准化问题:提案要么正确标准化,要么没有正确标准化。
实际上,第一个选项是not Normalized,标准化不完整。您在两个表中具有相同的重复列组,但尚未识别这些列并将其分组到单个表中。
第二个选项是标准化。您已经确定了这一点,并将它们放在一个表中。
在逻辑层面上,Movie 和 Comment 之间以及 Article 和 Comment 之间存在多对多关系(不是表格)。逻辑层面的故事结束。
在物理级别,其中 n::n 关系作为关联表实现,您有 CommentMovie 和 CommentArticle。
随着 Db 的扩展和增长,生活变得简单,因为:
任何与 Movie.PK 为 1::1 的新列都会放置在 Movie
任何与 Article.PK 为 1::1 的新列都会放置在 Article 中
任何与 Comment.PK 为 1::1 的新列都会放置在 Comment 中
任何与 CommentArticle.PK 为 1::1 的新列(关系;PK 如图所示 (ArticleId, CommentId) )都将放置在 CommentArticle 中。这(向 n::n 关系添加属性)现在将导致表显示在逻辑模型上。
任何与 CommentMovie.PK 为 1::1 的新列(关系;PK 如图所示 (MovieId, CommentId) )都将放置在 CommentMovie 中。这(向 n::n 关系添加属性)现在将导致表显示在逻辑模型上。
There is no "best" method, because it is a straight-forward Normalisation question: the proposal is either correctly Normalised or it is not.
Actually, the first option is not Normalised, the Normalisation is not complete. You have identical repeating groups of columns in two tables which have not been identified and grouped into a single table.
The second option is Normalised. You have identified that, and placed them in a single table.
at the logical level then, you have a many-to-many relation (not a table) between Movie and Comment, and between Article and Comment. End of story at the logical level.
at the physical level, where n::n relations are implemented as Associative tables, you have CommentMovie and CommentArticle.
as the Db expands and grows, life is simple, because:
any new column that is 1::1 with Movie.PK is placed in Movie
any new column that is 1::1 with Article.PK is placed in Article
any new column that is 1::1 with Comment.PK is placed in Comment
any new column that is 1::1 with CommentArticle.PK (the relation; PK is as shown (ArticleId, CommentId) ) is placed in CommentArticle. This (adding attributes to an n::n relation) will now cause the table to show up on the Logical model.
any new column that is 1::1 with CommentMovie.PK (the relation; PK is as shown (MovieId, CommentId) ) is placed in CommentMovie. This (adding attributes to an n::n relation) will now cause the table to show up on the Logical model.
我建议您的第二选择:
一个评论表,两个数据透视表(多对多)。
这会将所有相同的数据保留在一个表中,并且只是松散地链接它们。如果您可以逃脱连接,我通常建议对于不需要扩展的事情这样做,因为连接可能会影响性能,并且在某些情况下会是一场噩梦。但这最适合您的情况。
I would suggest your second choice:
One comments table, two pivot tables(many to many).
This will keep all the same data in one table and just loosely linking them. If you can get away with joins I usually recommend that for things that don't need to scale because joining can be a performance hit and a nightmare in cases. But this would be best for your case.
object_id 指的是诸如电影、i 文章等对象。
输入等于1:对电影进行评论,
输入等于2:对文章进行评论
您可以像这样设计表格。
object_id refers to object such as movie ,i articles and so on.
type equals 1: comment was done to movie ,
type equals 2: comment was done to article
You can design your tables like this.