社交网络数据库设计 - 朋友/块关系

时间:2011-09-28 21:18:42

标签: sql-server database-design social-networking

我正在开发一个社交网站,需要用户能够互相交流和/或互相阻止。我看到它的方式,2个用户可以是朋友待定阻止 NULL 。我希望有一个单独的视图,显示每个已确认关系的单行。我的观点正确地显示了这种关系,但我必须做一个解决方法,只显示1行/关系而不将表与自身联合并交换订单或请求者和请求者。

有人对如何清理它有任何想法吗?

谢谢,    - 格雷格

关系表:

  

Requestor(int)|请求者(int)| ApprovedTimestamp(smalldatetime)| IsBlock(位)

vwRelationship查看:

SELECT DISTINCT 
                      CASE WHEN f.Requestor < f.Requestee THEN f.Requestor ELSE f.Requestee END AS UserA, 
                      CASE WHEN f.Requestor < f.Requestee THEN f.Requestee ELSE f.Requestor END AS UserB, CASE WHEN b.Requestor IS NULL AND b.Requestee IS NULL 
                      THEN CASE WHEN f.AcceptedTimestamp IS NULL THEN 'Pending' ELSE 'Friend' END ELSE 'Block' END AS Type
FROM         dbo.Relationship AS f LEFT OUTER JOIN
                          (SELECT     Requestor, Requestee
                            FROM          dbo.Relationship
                            WHERE      (IsBlock = 1)) AS b ON f.Requestor = b.Requestor AND f.Requestee = b.Requestee OR f.Requestor = b.Requestee AND f.Requestee = b.Requestor

示例查询:

  

从vwRelationship中选择Type(UserA = 1 AND UserB = 2)OR(UserA = 2 AND UserB = 1)

情景:

  1. 用户1和用户2彼此不了解关系类型= NULL
  2. 用户1朋友用户2 |关系类型= 待定
  3. 用户2接受|关系类型= 朋友
  4. 一个月后用户2阻止用户1 |关系类型= 阻止

1 个答案:

答案 0 :(得分:3)

以下是我最终使用的内容:

Table - Relationship
RelationshipID, RelationshipTypeID, CreatedByUserID, CreatedTimestamp

Table - RelationshipType
RelationshipTypeID, RelationshipTypeName

Table - UserRelationship
UserID, RelationshipID, IsPending

有人想到更好的事情吗?