相关疑难解决方法(0)

Postgres UNIQUE CONSTRAINT用于数组

如何创建对数组中所有值的唯一性的约束,如:

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下检查

arrays postgresql unique-constraint

14
推荐指数
3
解决办法
4889
查看次数

检查Postgres数组中是否存在NULL

此问题类似,如何查找数组中是否存在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)

sql arrays postgresql null postgresql-9.1

11
推荐指数
1
解决办法
4945
查看次数