5

这个月我在两份不同的工作中遇到了同样的问题:

Version 1: User 1 & User 2 are friends
Version 2: Axis 1 & Axis 2 when graphed should have the quadrants colored...

问题是,我没有看到一种优雅的方式,使用 RDBMS 来存储和查询这些信息。

有两种明显的方法:

方法一:

store the information twice (i.e. two db rows rows per relationship):
u1, u2, true 
u2, u1, true
u..n, u..i, true
u..i, u..n, true

have rules to always look for the inverse on updates: 
on read, no management needed
on create, create inverse
on delete, delete inverse
on update, update inverse

Advantage:    management logic is always the same.
Disadvantage: possibility of race conditions, extra storage (which is admittedly cheap, but feels wrong)

方法二:

store the information once (i.e. one db row per relationship)
u1, u2, true
u..n, u..i, true

have rules to check for corollaries:
on read, if u1, u2 fails, check for u2, u1 
on create u1, u2: check for u2, u1, if it doesn't exist, create u1, u2
on delete, no management needed
on update, optionally redo same check as create

Advantage: Only store once
Disadvantage: Management requires different set of cleanup depending on the operation

我想知道是否有第三种方法符合“使用 f(x,y) 的键,其中 f(x,y) 对于每个 x,y 组合都是唯一的,而 f(x,y) === f(y,x)"

我的直觉告诉我,应该有一些按位运算的组合可以满足这些要求。类似于两列的东西:

键1 = x && y 键2 = x + y

我希望那些在数学系花费更多时间而在社会学系花费较少时间的人已经看到了这种可能性或不可能性的证据,并且可以提供一个快速的“[你白痴]它很容易证明(im)可能,请参阅此链接“(名称调用可选)

任何其他优雅的方法也将受到欢迎。

谢谢

4

5 回答 5

7

还有一种方法可以通过添加额外的约束来使用第二种方法。检查u1 < u2

CREATE TABLE User
( Name VARCHAR(10) NOT NULL
, PRIMARY KEY (Name)
) ;

CREATE TABLE MutualFriendship
( u1 VARCHAR(10) NOT NULL
, u2 VARCHAR(10) NOT NULL
, PRIMARY KEY (u1, u2)
, FOREIGN KEY (u1) 
    REFERENCES User(Name)
, FOREIGN KEY (u2) 
    REFERENCES User(Name)
, CHECK (u1 < u2) 
) ;

读取、创建、插入或更新的规则必须使用(LEAST(u1,u2), GREATEST(u1,u2)).

于 2012-01-10T20:31:48.997 回答
2

在 SQL 中,很容易实现约束以支持您的第一种方法:

CREATE TABLE MutualFriendship
(u1 VARCHAR(10) NOT NULL,
 u2 VARCHAR(10) NOT NULL,
 PRIMARY KEY (u1,u2),
 FOREIGN KEY (u2,u1) REFERENCES MutualFriendship (u1,u2));

INSERT INTO MutualFriendship VALUES
('Alice','Bob'),
('Bob','Alice');
于 2012-01-10T20:09:06.930 回答
1

对于任何感兴趣的人,我尝试了一些按位运算,发现以下似乎满足 f(x,y) 的标准:

#Python, returns 3 tuple
def get_hash(x, y):
  return (x & y, x | y, x * y)

不过,我无法证明。

于 2012-01-10T21:17:11.013 回答
0

“x 是 y 的朋友”。

定义 (x,y) 对的表并强制执行规范形式,例如 x<y。这将确保您的数据库中不能同时拥有 (p,q) 和 (q,p),因此它将确保“存储一次”。

创建一个视图为 SELECT x,y FROM FRIENDS UNION SELECT x as y, y as x FROM FRIENDS。

对基表进行更新(缺点:更新者必须知道强制的规范形式),对视图进行查询。

于 2012-01-10T23:57:16.717 回答
-2

您似乎将朋友的数量限制为 1。如果是这种情况,那么我会使用类似 u1,u2 u2,u1 u3,null u4,u5 u5,u4

u3没有朋友。

于 2012-01-10T19:20:25.990 回答