跨多列排列的唯一约束 [英] Unique constraint for permutations across multiple columns

查看:101
本文介绍了跨多列排列的唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出Postgres数据库中的以下三列:第一,第二,第三;

Given the following three columns in a Postgres database: first, second, third; how can I create a constraint such that permutations are unique?

例如,如何创建一个排列唯一的约束?如果数据库中存在('foo','bar','shiz'),则('bar','shiz','foo' )将被视为非唯一。

E.g. If ('foo', 'bar', 'shiz') exist in the db, ('bar', 'shiz', 'foo') would be excluded as non-unique.

推荐答案

您可以使用hstore创建唯一索引:

You could use hstore to create the unique index:

CREATE UNIQUE INDEX hidx ON test USING BTREE (hstore(ARRAY[a,b,c], ARRAY[a,b,c]));

提琴

实际上

CREATE UNIQUE INDEX hidx ON test USING BTREE (hstore(ARRAY[a,b,c], ARRAY[null,null,null]));

可能是一个更好的主意,因为它的工作原理相同,但占用的空间较小(小提琴)。

might be a better idea since it will work the same but should take less space (fiddle).

这篇关于跨多列排列的唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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