我有一个列,我想接受从1到100的值,这是可能的吗?我知道这是通过函数完成的,但我正在寻找一个简单的解决方案,如上所述。
您可以使用检查约束:
create table foo
(
some_value int not null check (some_value between 1 and 100)
);
或者,如果你需要在多个表中,一个域可能很有用
create domain one_to_hundred
as integer not null
check (value between 1 and 100);
create table foo
(
some_value one_to_hundred
);
在range上使用check约束:
CREATE TABLE foo (
bar int4range NOT NULL,
CONSTRAINT foo_bar_check CHECK (bar <@ '[0,100]'::int4range)
)
;
INSERT INTO foo(bar) VALUES('[20,30]');
INSERT INTO foo(bar) VALUES('(30,200]'); -- fail