如何创建不考虑列顺序的唯一索引(设置?) [英] How to create unique index where column order is not taken into account (set?)

查看:79
本文介绍了如何创建不考虑列顺序的唯一索引(设置?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表中有两列: Object1 Object2
我想以不允许的方式使对( Object1 Object2 )唯一像这样插入两行:

I have two columns in a table: Object1 and Object2. I would like to make pair (Object1, Object2) unique in a way that it wont allow to insert two rows like this:

Object1 = 1, Object2 = 2
Object1 = 2, Object2 = 1


推荐答案

您可以在表达式上创建索引,在这种情况下 least() greatest()

You can create an index on an expression, in this case least() and greatest():

create unique index idx_obj1_obj2 on table(least(Object1, Object2), greatest(Object1, Object2));

注意:如果列允许 NULL 值。在那种情况下,相同的值将只允许一次,无论它位于哪个列中。如果确实存在问题,可以使用更复杂的表达式来解决。

Note: there is one slight weirdness if the columns allow NULL values. In that case, the same value would only be allowed once, regardless of the column it is in. This can be fixed with a more complicated expression, if it is actually a problem.

这篇关于如何创建不考虑列顺序的唯一索引(设置?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆