MySQL好友表
我有一个 MySQL 数据库,其中存储有关每个用户的数据。
我想为每个用户添加一个朋友列表。我应该为数据库中的每个用户创建一个朋友表还是有更好的方法?
I have a MySQL DB in which I store data about each user.
I would like to add a list of friends for each user. Should I create a table of friends for each user in the DB or is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设您的所有朋友也在用户表中,您将需要一个朋友表,它定义了一个简单的一对多关系 - 将用户表链接回其自身。因此
,UserIDLink1 和 UserIDLink2 都是 Users 表上的外键。
例如,如果我有三个用户
,Joe 和 Jane 是朋友,那么 Friends 表将包含一行
上面隐式假设如果 A 是 B 的朋友,则 B 是 A 的朋友 - 如果情况并非如此您可能想将 UserIDLink1 和 UserIDLink2 重命名为 UserID 和 FriendID 或类似的名称 - 在这种情况下,您的记录也将增加一倍。
另外,对于双向配置(如果 B 是 A 的朋友,则 A 是 B 的朋友),您应该在 Friends 表上为 (UserIDLink1,UserIDLink2) 和 (UserIDLink2,UserIDLink1) 设置索引,以确保访问始终有效,如果我们正在搜索 joe 的朋友或 jane 的朋友(如果您没有设置第二个索引,那么第一个查询将是有效的索引查找,但第二个查询将需要全表扫描)。
如果您的链接不是双向的,则不需要找出 A 的朋友是谁,但您可能仍然最需要它,因为您可能还需要找出 B 的朋友是谁。
Assuming all your friends are also in the user table you will need a friends table which defines a simple one-to-many relationship - linking the users table back to itself. So
Where both UserIDLink1 and UserIDLink2 are foreign keys on the Users table.
So for instance if I have three users
and Joe and Jane are friends then the Friends table would contain a single row
The above implicitly assumes that if A is a friend of B then B is a friend of A - if this isn't the case you'd probably want to rename UserIDLink1 and UserIDLink2 to UserID and FriendID or similar - in which case you'd have up to double the records too.
Also for the bi-directional configuration (A is a friend of B if B is a friend of A) you should set up indexes on the Friends table for (UserIDLink1,UserIDLink2) and (UserIDLink2,UserIDLink1) to ensure access is always efficient if we were searching either for friends of joe or friends of jane (if you didn't set up the second index then the first query would be an efficient index lookup but the second would require a full table scan).
If your links were not bidirectional this wouldn't be necessary to find out who A's friends are, but you would still probably most require it as you'll likely also need to find out who B is a friend of.
假设您的
USER
表有一个名为id
或类似名称的主键,请使用下表:此设置支持 Peter 是 Mary 的朋友,但 Mary 不认为彼得就是这样。但数据的存在可以推断彼得是玛丽的熟人......
主键是两列也可以阻止重复。
Assuming your
USER
table has a primary key calledid
or something similar, use the following table:This setup supports that Peter is a friend of Mary, but Mary doesn't think of Peter like that. But the data exists to infer that Peter is an acquaintance for Mary...
The primary key being both columns also stops duplicates.
创建一个包含所有好友的表
表中的每一行将包含用户的 ID 及其好友的 ID
Create a table that contains all friends
Each row in the table will contain the ID of the user and the id of their friend
您正在寻找 M 对 N 或多对多连接表。
表 Users:
表 Friendships
USER_ID 和 FRIEND_ID 都是引用 Users 表 (Users.user_id) 的外键。
如果用户 123 是用户 921 的朋友。将行 (123, 921) 添加到 Friendships 表中。
You are looking for M-to-N or many-to-many join table.
Table Users:
Table Friendships
Both USER_ID and FRIEND_ID are foreign keys that reference Users table (Users.user_id).
If user 123 is friend of user 921. Add row (123, 921) to Friendships table.
为所有朋友创建一个表,并为每个朋友提供一个 UsersID,该 ID 等于他们各自的用户密钥
Create a single table for all the friends and give each friend a UsersID which is equal to their respective users key