开发者

UNIQUE KEY in any order

开发者 https://www.devze.com 2023-03-09 07:29 出处:网络
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 &&am

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

精彩评论

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