I need a check constraint on two columns, at least one must be not null

。_饼干妹妹 提交于 2019-12-04 23:13:35

This can be done with a check constraint that verifies null value and matches the result with or

create table #t (i int
               , j int
               , constraint chk_null check (i is not null or j is not null))

The following are the test cases

insert into #t values (null, null) --> error
insert into #t values (1, null) --> ok
insert into #t values (null, 1) --> ok
insert into #t values (1, 1) --> ok

late answer, but here is a solution for Sql Server for any number of columns to check:

CONSTRAINT CK_one_is_not_null CHECK (COALESCE(col1, col2, col3) IS NOT NULL )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!