开发者

UNIQUE KEY in any order

I am using UNIQUE KEY for MYSQL for one table. However, I have a table which has "user1,user2". I created a UNIQUE KEY but the problem is I want the UNIQUE KEY to act whether "user1==user1 && user2==user2" OR "user1==user2 && user2==user1"

user1     user2
----------------
user1     user2

OR

user1     u开发者_C百科ser2
----------------
user2     user1

How to achieve that UNIQUE KEY in mySQL?

Thanks!


You can only do it if you also enforce another rule:

user1 < user2

Then you know that the UNIQUE constraint will meet your requirements. (Or, conversely, user1 > user2; it doesn't matter which as long as you are self-consistent.)

Otherwise, as pointed out in the comments, the permutation '(b,a)' is different from '(a,b)' and the unique constraint will not enforce the uniqueness you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜