2010-09-29 3 views
1

Serait-ce la manière idéale/optimale d'implémenter une relation «suivant» unidirectionnelle?Structure de la base de données pour la modélisation d'un système suiveur unidirectionnel comme Twitter

J'ai 2 tables comme suit

Tableau 1: User_Info_Table

UserID Name JoinDate 

Tableau 2: User_Follower_Table

FollowerID  FollowingID Status 

FollowerID et FollowingID sont FK contraintes à la colonne User_Info_Table UserID

Le la logique suivante a lieu:

• L'utilisateur A fait suite à l'utilisateur 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 

• L'utilisateur A unfollows utilisateur B

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

• L'utilisateur B décide de suivre l'utilisateur A dos

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 

• L'utilisateur B unfollows Utilisateur A

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

Répondre

Questions connexes