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.
精彩评论