Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 512
Miguel Moura Avatar asked Apr 24 '15 11:04

Miguel Moura


1 Answers

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) )
like image 148
potashin Avatar answered Sep 21 '22 15:09

potashin