Link to home
Start Free TrialLog in
Avatar of chonabraham
chonabraham

asked on

How to create a check constraint or something similar to verify one or another but not both fields are not null in a create statement

I am trying to use something like a CHECK or an OR statement in a constraint within a table create statement. The objective is to create a condition that either one field or another may be null but not both. Such as   (Field_1 = NULL and Field_2 = NOT NULL) OR (Field_1 = NOT NULL and Field_2 = NULL) But not both

Can someone suggest the SQL to d this?
ASKER CERTIFIED SOLUTION
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chonabraham
chonabraham

ASKER

where does this statement go in a CREATE TABLE statement?
If you are trying to create table then probably i will create a index or primary key that both the columns can't be blank and that way will control the same...

Refer to link Here for the same...
What is the structure for creating the index using (field_1 is null and Field_2 is not null) or (field_1 is not null and Field_2 is null)?
On this link...refer to option of filter index..where it says you can give your where criteria in Index which is the same thing what you are looking for...

SQL Index