我一直想知道Facebook是如何设计好友用户关系的。
我认为用户表是这样的:
user_email PK
user_id PK
password
我用用户的数据(性别,年龄等通过用户电子邮件连接我假设)计算表格。
它如何将所有朋友连接到该用户?
像这样的东西吗?
user_id
friend_id_1
friend_id_2
friend_id_3
friend_id_N
可能不会。因为用户数量未知并且会扩大。
保留一个好友表,其中保存了 UserID,然后保存了好友的 UserID(我们将其称为 FriendID)。这两列都将是返回 Users 表的外键。
有点有用的例子:
Table Name: User
Columns:
UserID PK
EmailAddress
Password
Gender
DOB
Location
TableName: Friends
Columns:
UserID PK FK
FriendID PK FK
(This table features a composite primary key made up of the two foreign
keys, both pointing back to the user table. One ID will point to the
logged in user, the other ID will point to the individual friend
of that user)
用法示例:
Table User
--------------
UserID EmailAddress Password Gender DOB Location
------------------------------------------------------
1 [email protected] bobbie M 1/1/2009 New York City
2 [email protected] jonathan M 2/2/2008 Los Angeles
3 [email protected] joseph M 1/2/2007 Pittsburgh
Table Friends
---------------
UserID FriendID
----------------
1 2
1 3
2 3
这将表明 Bob 是 Jon 和 Joe 的朋友,并且 Jon 也是 Joe 的朋友。在此示例中,我们假设友谊始终是两种方式,因此您不需要表中的行(例如 (2,1) 或 (3,2)),因为它们已经在另一个方向上表示。例如,友谊或其他关系不是明确的双向关系,您还需要这些行来指示双向关系。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)