在关系数据库中存储1:1用户关系的最佳方式 [英] best way to store 1:1 user relationships in relational database

查看:147
本文介绍了在关系数据库中存储1:1用户关系的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存储用户关系的最佳方式是什么?友谊,必须是双向的(你是我的朋友,因此我是你的朋友)在rel。数据库。 MYSql?

What is the best way to store user relationships, e.g. friendships, that must be bidirectional (you're my friend, thus I'm your friend) in a rel. database, e.g. MYSql?

我可以想出两种方法:


  1. ,我将向数据库添加两行,行A包含创始用户的用户ID,后面是接受用户在下一列中的UID。

  2. 您只需添加一行,UID(启动用户),然后是UID(接受用户);

当然有更好的东西?

推荐答案

我会有一个朋友的链接表,或者什么,2列都是PK,用户表。

I would have a link table for friends, or whatever, with 2 columns both being PK's, and both being FK's to the User table.

两列都是UID,每个朋友关系(A,B和B,A)有两行。只要两列都是PK,它应该仍然是正常的格式(虽然其他人可以自由更正我的)

Both columns would be the UID, and you would have two rows per friend relationship (A,B and B,A). As long as both columns are PK's, it should still be in normal format (although others are free to correct me on this)

它的一个更复杂的查询,但是没有什么东西不能被存储过程或一些业务逻辑抽象出来,而且在正常格式中,这通常是很好的。

Its a little more complex of a query, but nothing that can't be abstracted away by a stored procedure or some business logic, and its in Normal Format, which is usually nice to have.

这篇关于在关系数据库中存储1:1用户关系的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆