当其他属性在SQL中也不为空时,该属性为非空

问题描述 投票:0回答:1

我想为Articles创建一个表,该表可以具有(但不必具有)指向img-Source的链接。对于所有具有链接的文章,还需要一个img-Type(应为“ png”,“ svg”或“ jpg”)。我不太了解如何仅对于img-Src字段不为null的值使img-Type字段不为null。这是我的代码(对于img-Type和img-Src字段没有null / null约束)

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

感谢您的帮助!

sql create-table notnull
1个回答
0
投票

添加一个新的约束,要么均为空,要么都不为空

check (imgSrc is not null and imgType is not null 
    or imgSrc is null and imgType is null)
© www.soinside.com 2019 - 2024. All rights reserved.