配列内のすべての値の一意性に関する制約を作成する方法は次のとおりです。
CREATE TABLE mytable
(
interface integer[2],
CONSTRAINT link_check UNIQUE (sort(interface))
)
私のソート機能
create or replace function sort(anyarray)
returns anyarray as $$
select array(select $1[i] from generate_series(array_lower($1,1),
array_upper($1,1)) g(i) order by 1)
$$ language sql strict immutable;
値{10, 22}と{22, 10}が同じであるとみなされ、UNIQUE CONSTRAINTの下でチェックされる必要があります。
ベストアンサー1
関数をユニーク制約しかし、個性的索引次のようなソート関数があるとします。
create function sort_array(anyarray) returns anyarray as $$
select array_agg(distinct n order by n) from unnest($1) as t(n);
$$ language sql immutable;
次に、次のようにします。
create table mytable (
interface integer[2]
);
create unique index mytable_uniq on mytable (sort_array(interface));
すると次のことが起こります:
=> insert into mytable (interface) values (array[11,23]);
INSERT 0 1
=> insert into mytable (interface) values (array[11,23]);
ERROR: duplicate key value violates unique constraint "mytable_uniq"
DETAIL: Key (sort_array(interface))=({11,23}) already exists.
=> insert into mytable (interface) values (array[23,11]);
ERROR: duplicate key value violates unique constraint "mytable_uniq"
DETAIL: Key (sort_array(interface))=({11,23}) already exists.
=> insert into mytable (interface) values (array[42,11]);
INSERT 0 1