假设我想要一个复合类型的地址,例如:
create type address as (
city text,
address_line text,
zip_code int
);
为了使数据完整性更好,我不想让NULL
s 成为city
、address_line
或zip_code
. 所以我想not null
对这些领域有一个约束。
创建域检查对我不起作用。所以这段代码会产生错误:
create domain address_domain as address
check (
value.city is not null and
value.address_line is not null and
value.zip_code is not null
);
您可能会说:“好吧,为什么不将地址存储为三列,并在字段中添加约束?”。我会回答说我希望能够使地址本身为空,但如果地址存在,它的所有字段也应该存在。像这样的东西:
create table companies (
id serial primary key,
name text not null,
headquaters address -- this one can be null tho
)