这是实现单向“跟随”关系的理想/最佳方式吗?
我有两张表如下
表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