【问题标题】:Mysql : table strucutre design for post 'like' and 'unlike'Mysql:post 'like' 和 'unlike' 的表结构设计
【发布时间】:2016-10-20 16:02:42
【问题描述】:

我有一个安卓应用,用户可以"like/unlike"发帖,

我已经创建了一个表:

| id | post_id | user_id |
|  1 |     105 |      67 |
|  2 |      76 |      56 |
|  3 |      36 |     102 |

id 自增,这个表结构正确吗?

由于一个人可以多次喜欢/不喜欢,您认为删除行(不喜欢)+创建新行(喜欢)是一种好习惯吗?

如果有很多用户,自动增量行会增加并达到很大的数字,这很糟糕吗?因为自增不能重用被删除的行。

如果我添加了一个额外的boolean 列,则like 的值为10

并考虑表中没有现有行的用户 + 具有现有行但列 like = 0 的用户不同

这样会更好吗?

【问题讨论】:

  • 您是否使用自动递增列进行任何操作?为什么不在 post_id 和 user_id 之间使用复合主键?
  • 这真的取决于这个表的用途。你想监控历史吗?

标签: php android mysql database database-design


【解决方案1】:

你认为删除行(不喜欢)+创建新行(喜欢)是好的做法吗?

这个设计有缺陷。假设,用户不喜欢。但是表中没有为他对应的行。所以没有什么可删除的。

您将无法存储关于不同点的信息。

如果有很多用户,自动增量行会增加并达到很大的数字,这很糟糕吗?

这里不需要自动递增列。使用复合主键:

post_id - PK, FK to posts table
user_id - PK, FK to users table
like - boolean (0 - dislike, 1 -like)

在这种情况下,您将能够存储所有信息。并且您可以免受自动递增列到期的影响。

更新

如果您不需要为帖子保留不同的价值,并且用户在喜欢该帖子之前无法对其进行点赞。最简单的解决方案是创建一个简单的映射表:

post_id - PK, FK to posts table
user_id - PK, FK to users table

当用户喜欢帖子时,添加一行。如果他不喜欢,请删除该行。

【讨论】:

  • so 如果用户“不喜欢”,我应该保留该列吗?或者只是添加另一个值为 1 或 0 的“喜欢”列是否更容易?因此,当用户第一次“喜欢”时,它会创建一个新行,以后如果他“不喜欢”,将保留该列,但将“喜欢”列的值设置为 0。我不必跟踪“不喜欢”,所以“不喜欢”将是不存在的行或“喜欢”行的列 = 0
【解决方案2】:

简单喜欢

 Create table like( 
    user_id int,
    post_id int,
    Constraint pk_like primary key (user_id, post_id),
    Constraint fk_user foreign key (user_id) references UserTable(id),
    Constraint fk_post foreign key (post_id) references PostTable(id)
 );

如果用户喜欢你添加一行,

Insert into like values (@userId,@postId);

如果他不喜欢他的喜欢你删除

Delete from like where user_id = @userId and post_id = @post_id;

如果您的应用中存在“不喜欢”,则应添加布尔列 1 表示喜欢,0 表示不喜欢,如果您有更多喜欢和不喜欢的内容,例如:hate、love、ungry。强>

#Call in query to like(1), unlike(2), hate(3)
call sp_Like(@userID,@postId,1); 
#To dont like,unlike,hate,etc anymore you send (-1) in likeType.
call sp_Like(@userID,@postId,-1);
    #This is your query
    call sp_Like(@userID,@postId,likeType);

    Create table yourDB.like( 
    user_id int,
    post_id int,
    likeType int,
    Constraint pk_like primary key (user_id, post_id),
    Constraint fk_user foreign key (user_id) references UserTable(id),
    Constraint fk_post foreign key (post_id) references PostTable(id)
    );


    DELIMITER $$
    USE yourDB$
    DROP PROCEDURE IF EXISTS yourDB.sp_Like$$
    CREATE PROCEDURE yourDB.sp_Like
    (
      IN ruser_id int,
      IN rpost_id int,
      IN rlikeType int
    )
    BEGIN
    DECLARE exit handler for sqlexception
          BEGIN
          ROLLBACK;
          SHOW ERRORS LIMIT 1;
          select "-1" as "response";
    END;
    START TRANSACTION;

    #check if he done like before
    If exists(select post_id from like where  user_id = ruser_id and post_id = rpost_id)
    Then
    #if user doesnt like or unlike, love, hate, anymore
    If rlikeType = -1 then delete from like where 
    user_id = ruser_id and post_id = rpost_id; 
    else
    #if user change like type
    Update like set likeType = rlikeType where  user_id = ruser_id and post_id = rpost_id;
    end if;

    Else
    #if he doesnt like yet
    Insert into like values (ruser_id,rpost_id,rlikeType);
    End if;

    select "1" as "response";
    COMMIT;

    END$$
    DELIMITER ;

【讨论】:

    猜你喜欢
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2017-04-10
    相关资源
    最近更新 更多