本文介绍了Facebook数据库设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我一直想知道Facebook如何设计朋友的用户关系。我认为用户表是这样的: user_email PK user_id PK 密码 我使用用户的数据(性别,年龄等)通过我将承担的用户电子邮件连接的表格。 如何将所有的朋友连接到此用户? 这样的东西? user_id friend_id_1 friend_id_2 friend_id_3 friend_id_N 可能不是。因为用户数量是未知的并且会扩展。解决方案保留一个拥有UserID的朋友表,然后保留UserID朋友(我们称之为FriendID)。两个列都将是外键返回到Users表。 有用的示例: code>表名称:用户列:用户ID PK EmailAddress 密码性别 DOB 位置 TableName:朋友列:用户ID PK FK FriendID PK FK (此表具有由两个外部键组成的复合主键都指向用户表,一个ID将指向登录用户,另一个ID将指向该用户的个人朋友 示例用法: 表用户 -------------- UserID EmailAddress密码性别DOB位置 -------------------- ---------------------------------- 1 bob@bob.com bobbie M 1/1 / 2009纽约市 2 jon@jon.com jonathan M 2/2/2008洛杉矶 3 joe@joe.com约瑟夫M 1/2/2007匹兹堡 表朋友 --------------- UserID FriendID ---------------- 1 2 1 3 2 3 这将显示Bob是朋友乔恩和乔,乔恩也是乔的朋友。在这个例子中,我们假设友谊总是有两种方式,所以你不需要像表(2,1)或(3,2)这样的一行,因为它们已经在另一个方向表示了。例如,如果友谊或其他关系不是明确的两种方式,您还需要有这些行来表示双向关系。 I have always wondered how Facebook designed the friend <-> user relation.I figure the user table is something like this:user_email PKuser_id PKpasswordI figure the table with user's data (sex, age etc connected via user email I would assume).How does it connect all the friends to this user?Something like this?user_idfriend_id_1friend_id_2friend_id_3friend_id_NProbably not. Because the number of users is unknown and will expand. 解决方案 Keep a friend table that holds the UserID and then the UserID of the friend (we will call it FriendID). Both columns would be foreign keys back to the Users table.Somewhat useful example:Table Name: UserColumns: UserID PK EmailAddress Password Gender DOB LocationTableName: FriendsColumns: 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)Example Usage:Table User--------------UserID EmailAddress Password Gender DOB Location------------------------------------------------------1 bob@bob.com bobbie M 1/1/2009 New York City2 jon@jon.com jonathan M 2/2/2008 Los Angeles3 joe@joe.com joseph M 1/2/2007 PittsburghTable Friends---------------UserID FriendID----------------1 21 32 3This will show that Bob is friends with both Jon and Joe and that Jon is also friends with Joe. In this example we will assume that friendship is always two ways, so you would not need a row in the table such as (2,1) or (3,2) because they are already represented in the other direction. For examples where friendship or other relations aren't explicitly two way, you would need to also have those rows to indicate the two-way relationship. 这篇关于Facebook数据库设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-10 19:59