2012-03-08 43 views
1

我有3个表:(MySQL的)主键W/2相同的外键

Users: _id_, username 
Movies: _id_, title 
Friends: _#id1, #id2_, accepted 

我与InnoDB的,一切都是那样工作。但是,即使朋友主键(users.id,users.id),2行像被MySQL接受:

1) (1, 2) 
2) (2, 1) 

有没有考虑这些2行作为一个重复键的方式MySQL?
感谢您的帮助。

回答

1

我想不出有什么办法让MySQL独自解决这个问题。我会用任何插入/更新GREATEST(http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_greatest)和LEAST(http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_least)本表:

INSERT INTO friends (LEAST(_#id1, _#id2), GREATEST(_#id1, _#id2)); 

所以,你将永远不会得到(2,1)和总是(1,2)。

+1

谢谢,这很好。 – Max13 2012-03-09 10:43:19