如何创建对数组中所有值的唯一性的约束,如:
CREATE TABLE mytable
(
interface integer[2],
CONSTRAINT link_check UNIQUE (sort(interface))
)
Run Code Online (Sandbox Code Playgroud)
我的排序功能
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;
Run Code Online (Sandbox Code Playgroud)
我需要的是值{10,22}和{22,10}被认为是相同的并且在UNIQUE CONSTRAINT下检查
与此问题类似,如何查找数组中是否存在NULL值?
这是一些尝试.
SELECT num, ar, expected,
ar @> ARRAY[NULL]::int[] AS test1,
NULL = ANY (ar) AS test2,
array_to_string(ar, ', ') <> array_to_string(ar, ', ', '(null)') AS test3
FROM (
SELECT 1 AS num, '{1,2,NULL}'::int[] AS ar, true AS expected
UNION SELECT 2, '{1,2,3}'::int[], false
) td ORDER BY num;
num | ar | expected | test1 | test2 | test3
-----+------------+----------+-------+-------+-------
1 | {1,2,NULL} | t | f | | t
2 | {1,2,3} | f | f …Run Code Online (Sandbox Code Playgroud)