【发布时间】:2020-05-13 15:52:02
【问题描述】:
我想为文章创建一个表,它可以有(但不需要有)指向 img-Source 的链接。对于所有具有链接的文章,还需要一个 img-Type(应该是“png”、“svg”或“jpg”)。我不太明白如何使 img-Type 字段不为空,仅适用于 img-Src 字段不为空的值。
这是我的代码(字段 img-Type 和 img-Src 没有非空/空约束)
create TABLE Article(
articleID varchar(15) primary key ,
articleDescription varchar (80) null ,
imgSrc varchar (20) ,
imgType char(3),
check imgType = 'png' or imgType = 'svg' or imgType = 'jpg'
);
【问题讨论】:
-
有什么问题?您使用的是哪个 dbms?
-
check (imgType IN ( 'png', 'svg', 'jpg')) -
我正在使用 postgresql,问题是如何使 imgType 属性不为空,但前提是 imgSrc 也不为空。所以我不需要一个 imgSrc 值,但如果我有一个,我还需要一个 imgType 的值。
-
@DerWolferl
check imgSrc is null or imgType is not null。这样,如果 imgSrc 为 null,则约束通过,如果 imgSrc 不是,则检查 imgType 是否为 null。
标签: sql postgresql constraints create-table notnull