如果我在Postgresql中有一个表:
create table Education ( id integer references Profiles(id),finished YearValue not null,started YearValue,qualification text,schoolName text,studiedAt integer references Organizations(id),primary key (id) );
我需要做一个约束,以使schoolName或studyingAt不需要为空(其中一个必须有信息).
我该如何做?
您可以使用
check constraint
constraint chk_education check (schoolName is not null or studiedAt is not null)
从手册:
A check constraint is the most generic constraint type. It allows you to specify that the value in a certain column must satisfy a Boolean (truth-value) expression.
编辑:替代遵守无形式解释:
constraint chk_education check ((schoolName is not null and studiedAt is null) or (schoolName is null and studiedAt is not null))