简体   繁体   English

在MySQL中将点赞表与帖子,评论和回复表相关联的有效方法是什么?

[英]What is an efficient way of relating a likes table to a posts, comments and replies table in MySQL?

I am building a simple social networking website (a personal project of mine to help me understand back-end programming more) and as of the moment I am stuck on how I should tackle the above problem. 我正在建立一个简单的社交网站(我的一个个人项目,可以帮助我更多地了解后端编程),并且到目前为止,我一直坚持如何解决上述问题。

Right now I have a table for users, posts, comments, replies, post_likes, comment_likes and reply_likes. 现在,我有一个用于用户,帖子,评论,回复,post_likes,comment_likes和reply_likes的表。

As of the moment my system works as follows: 到目前为止,我的系统工作如下:

  1. A user creates a post which will then be inserted to the posts table along with that user's id 用户创建一个帖子,然后将其与该用户的ID一起插入到posts表中
  2. Whenever a user comments on said post, a row is inserted into the comments table along with the user's id and the post's id 每当用户对帖子发表评论时,都会在评论表中插入一行以及用户ID和帖子ID
  3. Whenever a user replies to a comment, it is inserted into the replies table together with that user's id as well as the comment's id in which the reply was made 每当用户回复评论时,评论就会与该用户的ID以及评论所在的评论ID一起插入回复表中

Enter my likes tables which is structured as so... 输入结构如下的我的点赞表...

post_likes
  post_id
  user_id
  like_state

comment_likes
  comment_id
  user_id
  like_state

reply_likes
  reply_id
  user_id
  like_state

You can probably already tell where I am going with this, but each time a user likes a certain post, comment or reply it gets inserted into its respective like table along with that user's id and a like_state to prevent them from liking again. 您可能已经知道我要去哪里了,但是每次用户喜欢某个帖子,评论或回复时,它都会与该用户的ID和like_state一起插入到各自的like表中,以防止他们再次喜欢。

This all works fine but I am clearly repeating myself which I know is taboo in the programming world. 一切都很好,但是我很清楚地重复我自己,我知道这是编程世界中的忌讳。 Which leads us to my question, what exactly can I do to remedy this? 这就引出了我的问题,我该怎么做才能解决这个问题? Although I came up with an idea, I just can't quite figure out how I can structure my question well enough to be able to get any good results from Google (I am not a native English speaker) 尽管我想出了一个主意,但我只是想不出如何很好地解决问题,以便能够从Google获得任何好的结果(我不是英语为母语的人)

PS the solution I came up with is simply creating just one likes table and each row could either have just a post_id (if the user liked a post), a comment_id (if the user liked a comment) or a reply_id (if the user liked a reply), is that possible? PS我想出的解决方案只是创建一个Likes表,每行可以只有一个post_id(如果用户喜欢一个帖子),comment_id(如果用户喜欢一个评论)或reply_id(如果用户喜欢)回复),有可能吗?

I think your current tables are good. 我认为您目前的餐桌很好。 I think your post, comments and replies has a one to many relationship with the likes. 我认为您的帖子,评论和回复与喜欢的人有一对多的关系。 The like should go in separate tables. 像应该放在单独的表中。 And you are exactly doing that. 而您正是这样做的。 If you want to combine the likes into one table, then you will need an extra column to track what is belongs to what. 如果要将“喜欢”组合到一个表中,则将需要一个额外的列来跟踪什么属于什么。 And you will also not able to set the foreign key constrain on that table. 而且,您也将无法在该表上设置外键约束。 So, IMO, you are good at this point. 因此,IMO,您在这一点上很出色。

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

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