如果我有PostgreSQL中表:添加在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
或studiedAt
需要不能为空(其中一个必须有信息)。
我该怎么做?
这张支票并不防范学校名称和研究设置,我认为OP也考虑过了。 – pithyless 2011-07-26 18:17:07
该问题要求至少强调'schoolName'和'studyingAt'包含一些信息的不变量。我已经添加了符合您评论的约束变体,但我不同意这个问题的解释。 – 2011-07-26 19:47:59
这有点迟了,但是这是一个XOR约束,所以你可以把它表示为'CHECK((schoolName IS NULL)<>(studyingAt IS NULL))' – norcalli 2017-08-16 22:11:02