小编Ale*_*ein的帖子

检查数组列的约束以验证长度 > 0

我正在使用 postgres 表验证规则并尝试CHECK为数组列设置约束。一个想法是只允许长度 > 0 的数组。

这是我想要实现它的方式:

create table words_table (
  id serial primary key, 
  words varchar(20)[] CHECK (array_length(words, 1) > 0)
);
Run Code Online (Sandbox Code Playgroud)

但看起来它不起作用。o_o

insert into words_table (words) values ('{}');
//INSERT 0 1
Run Code Online (Sandbox Code Playgroud)

如何实现这样的约束?

postgresql null constraint check-constraints

6
推荐指数
2
解决办法
2544
查看次数

选择列值出现多次的所有行

我在 Postgres 中有以下表结构:

 id | account_id | plan_id | active
----+------------+---------+--------
  1 |    0cYd7Ak |       1 |      f
  2 |    Uk02q1d |       1 |      t
  3 |    eRlk810 |       2 |      f
  4 |    Uk02q1d |       2 |      t
  5 |    0cYd7Ak |       1 |      t
  6 |    yT3nv3p |       3 |      t  
Run Code Online (Sandbox Code Playgroud)

如何选择account_id出现多次的所有行?对于上面的示例,它将返回第 1、2、4、5 行

我试过这个查询,但它没有返回我所期望的:

select * from table_name t1 
where (select count(*) from table_name t2 
  where t1.account_id = t2.account_id) > 1 
order by t1.account_id;
Run Code Online (Sandbox Code Playgroud)

postgresql

1
推荐指数
2
解决办法
2万
查看次数

标签 统计

postgresql ×2

check-constraints ×1

constraint ×1

null ×1