MySQL好友表

发布于 2024-09-05 04:28:20 字数 90 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凯凯我们等你回来 2024-09-12 04:28:20

假设您的所有朋友也在用户表中,您将需要一个朋友表,它定义了一个简单的一对多关系 - 将用户表链接回其自身。因此

User Table
UserID int identity not null
[other attribute fields]

Friends Table
UserIDLink1 int
UserIDLink2 int 
[other attribute field]

,UserIDLink1 和 UserIDLink2 都是 Users 表上的外键。

例如,如果我有三个用户

1 Joe
2 Bill
3 Jane

,Joe 和 Jane 是朋友,那么 Friends 表将包含一行

1 3

上面隐式假设如果 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

User Table
UserID int identity not null
[other attribute fields]

Friends Table
UserIDLink1 int
UserIDLink2 int 
[other attribute field]

Where both UserIDLink1 and UserIDLink2 are foreign keys on the Users table.

So for instance if I have three users

1 Joe
2 Bill
3 Jane

and Joe and Jane are friends then the Friends table would contain a single row

1 3

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.

不必你懂 2024-09-12 04:28:20

假设您的 USER 表有一个名为 id 或类似名称的主键,请使用下表:

DROP TABLE IF EXISTS `friends`;
CREATE TABLE `friends` (
  `user_id` int(10) unsigned NOT NULL,
  `friend_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`user_id`,`friend_id`),
  KEY `FK_FRIENDS_2` (`friend_id`),
  CONSTRAINT `FK_FRIENDS_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
  CONSTRAINT `FK_FRIENDS_2` FOREIGN KEY (`friend_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

此设置支持 Peter 是 Mary 的朋友,但 Mary 不认为彼得就是这样。但数据的存在可以推断彼得是玛丽的熟人......

主键是两列也可以阻止重复。

Assuming your USER table has a primary key called id or something similar, use the following table:

DROP TABLE IF EXISTS `friends`;
CREATE TABLE `friends` (
  `user_id` int(10) unsigned NOT NULL,
  `friend_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`user_id`,`friend_id`),
  KEY `FK_FRIENDS_2` (`friend_id`),
  CONSTRAINT `FK_FRIENDS_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
  CONSTRAINT `FK_FRIENDS_2` FOREIGN KEY (`friend_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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.

盛夏尉蓝 2024-09-12 04:28:20

创建一个包含所有好友的表
表中的每一行将包含用户的 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

萝莉病 2024-09-12 04:28:20

您正在寻找 M 对 N 或多对多连接表。

表 Users:

USER_ID  integer primary key,
NAME     varchar

表 Friendships

USER_ID    integer not null,
FRIEND_ID  integer not null,

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:

USER_ID  integer primary key,
NAME     varchar

Table Friendships

USER_ID    integer not null,
FRIEND_ID  integer not null,

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.

夜空下最亮的亮点 2024-09-12 04:28:20

为所有朋友创建一个表,并为每个朋友提供一个 UsersID,该 ID 等于他们各自的用户密钥

Create a single table for all the friends and give each friend a UsersID which is equal to their respective users key

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文