在postgresql中添加一对一的非空约束
发布时间:2020-12-13 16:28:30 所属栏目:百科 来源:网络整理
导读:如果我在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));
如果我在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) 从手册:
编辑:替代遵守无形式解释: constraint chk_education check ((schoolName is not null and studiedAt is null) or (schoolName is null and studiedAt is not null)) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |