用于建模像Twitter这样的单向跟随系统的数据库结构

时间:2010-09-29 12:07:35

标签: sql database database-design

这是实现单向“跟随”关系的理想/最佳方式吗?

我有两张表如下

表1:User_Info_Table

UserID    Name   JoinDate

表2:User_Follower_Table

FollowerID     FollowingID    Status 

FollowerID和FollowingID是User_Info_Table UserID列的FK约束

发生以下逻辑:

•用户A跟随用户B

WHERE NOT EXISTS (SELECT 1 FROM User_Follower_Table WHERE FollowerID = A and FollowingID = B and Status = 0)
BEGIN

    INSERT INTO User_Follower_Table (FollowerID, FollowingID, Status)
    VALUES (A, B, 1)

END

ELSE

    UPDATE User_Follower_Table
    SET Status = 1
    WHERE FollowerID = A and FollowingID = B and Status = 0

•用户A取消关注用户B

UPDATE User_Follower_Table
SET Status = 0
WHERE FollowerID = A and FollowingID = B and Status = 1

•用户B决定关注用户A

WHERE NOT EXISTS (SELECT 1 FROM User_Follower_Table WHERE FollowerID = B and FollowingID = A and Status = 0)
BEGIN

    INSERT INTO User_Follower_Table (FollowerID, FollowingID, Status)
    VALUES (B, A, 1)

END

ELSE 

   UPDATE User_Follower_Table
   SET Status = 1
   WHERE FollowerID = B and FollowingID = A and Status = 0

•用户B取消关注用户A

UPDATE User_Follower_Table
SET Status = 0
WHERE FollowerID = B and FollowingID = A and Status = 1

1 个答案:

答案 0 :(得分:1)

请看一下 similar question/answer