加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在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)

从手册:

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))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读