Jim*_*mmy 24 sql postgresql constraints database-schema
如果我在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)
);
Run Code Online (Sandbox Code Playgroud)
我需要创建一个约束,以便schoolName或者studiedAt不需要为null(其中一个必须包含信息).
我该怎么做呢?
Ale*_*aho 38
您可以使用检查约束,例如
constraint chk_education check (schoolName is not null or studiedAt is not null)
Run Code Online (Sandbox Code Playgroud)
从手册:
检查约束是最通用的约束类型.它允许您指定某列中的值必须满足布尔(真值)表达式.
编辑:符合Pithyless解释的替代方案:
constraint chk_education check ((schoolName is not null and studiedAt is null) or (schoolName is null and studiedAt is not null))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11466 次 |
| 最近记录: |