定义涉及模式匹配的类型



如果我想在PostgreSQL中定义一个类型,其中类型应如下所示:

[A-Z]{4}[0-9]{4}

这是在进行模式匹配。我该怎么办?

CREATE DOMAIN可能适合你。

create domain NEWTYPE as char(8)
  constraint newtype_regex not null check (value ~ '[A-Z]{4}[0-9]{4}');

我猜到了not null.像内部数据类型一样使用它。

create table test (
  nt NEWTYPE
);

烟雾测试

. . .
insert into test values ('ABCD0123'); -- Succeeds
insert into test values ('ABCD012');  -- Fails
insert into test values (null);       -- Fails
insert into test values ('abcd0123'); -- Fails

最新更新