Unique constraint for permutations across multiple columns

前端 未结 4 1509
抹茶落季
抹茶落季 2021-01-17 18:33

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

E.g. If (\'foo

4条回答
  •  庸人自扰
    2021-01-17 19:21

    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]));
    

    Fiddle

    UPDATE

    Actually

    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).

提交回复
热议问题