Search code examples
sqlsql-servernullsql-server-2012check-constraints

Check if all three columns are either not null or null


I have a table with 4 columns:

create table dbo.Table ( 
  Id int not null,
  A int null,
  B int null,   
  C nvarchar (4000) null
)

How can I make sure that A, B and C are all three null or all three not null?


Solution

  • You can set a check constraint:

    constraint [check_abc] check ( ([A] is null and [B] is null and [C] is null) or
                                   ([A] is not null and [B] is not null and [C] is not null) )