任意顺序的唯一密钥
我对一张表使用 MYSQL 的 UNIQUE KEY。但是,我有一个包含“user1,user2”的表。我创建了一个唯一密钥,但问题是我希望唯一密钥起作用,无论是“user1==user1 && user2==user2”还是“user1==user2 && user2==user1”
user1 user2
----------------
user1 user2
还是
user1 user2
----------------
user2 user1
如何实现mySQL 中的唯一键?
谢谢!
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 user2
----------------
user2 user1
How to achieve that UNIQUE KEY in mySQL?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只有当您还强制执行另一条规则时,您才能做到这一点:
那么您就知道 UNIQUE 约束将满足您的要求。 (或者,相反,
user1 > user2
;只要你是自洽的,这并不重要。)否则,正如评论中指出的,排列 '(b,a) ' 与 '(a,b)' 不同,唯一约束不会强制执行您想要的唯一性。
You can only do it if you also enforce another rule:
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.