Postgresql为空数组添加检查
有没有办法向表中添加限制规则Postgresql为空数组添加检查,sql,postgresql,Sql,Postgresql,有没有办法向表中添加限制规则 CREATE TABLE table1 ( id serial not null primary key, values1 smallint[] not null DEFAULT ARRAY [12, 20], values2 smallint[] not null DEFAULT ARRAY [], CONSTRA
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);
可以添加检查约束:
alter table table1 add constraint check_table1_arrays
check (cardinality(values1) > 0 and cardinality(values2) > 0);
不需要约束中的NOT(IS NULL…
,因为您的表定义中已经有notnull
,您可以这样做:
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 <> '{}' )
);
创建表表1
(
id序列非空主键,
值1 smallint[]非空默认数组[12,20],
values2 smallint[]非空默认值“{}”,
约束值1_检查(值1'{}'),
约束值2\u检查检查(值2'{}')
);
不需要约束中的NOT(为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 <> '{}' )
);
创建表表1
(
id序列非空主键,
值1 smallint[]非空默认数组[12,20],
values2 smallint[]非空默认值“{}”,
约束值1_检查(值1'{}'),
约束值2\u检查检查(值2'{}')
);
我必须使用alter table
还是可以将检查添加到表创建中?我必须使用alter table
还是可以将检查添加到表创建中?我正在应用此创建,但它没有引发错误!@chatzich-Show query,从中可以看到错误,我正在应用此创建,但它没有引发错误r!@chatzich-Show查询,您希望从中抛出错误