[英]Postgresql add check for empty array
有什么辦法可以給表添加限制規則
CREATE TABLE table1
(
id serial not null primary key,
values1 smallint[] not null DEFAULT ARRAY [12, 20],
values2 smallint[] not null DEFAULT ARRAY [],
CONSTRAINT constraint_check_error CHECK (values1 NOT ( IS NULL OR values1 = '{}') AND NOT (values2 IS NULL OR values2 = '{}') )
);
避免表values1
和values2
為空?
您可以添加檢查約束:
alter table table1 add constraint check_table1_arrays
check (cardinality(values1) > 0 and cardinality(values2) > 0);
不需要NOT ( IS NULL...
在約束中,因為您的表定義中已經not null
,您可以這樣做:
CREATE TABLE table1
(
id serial not null primary key,
values1 smallint[] not null DEFAULT ARRAY [12, 20],
values2 smallint[] not null DEFAULT '{}',
CONSTRAINT values1_check CHECK ( values1 <> '{}' ),
CONSTRAINT values2_check CHECK ( values2 <> '{}' )
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.