Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional constraint.

Status
Not open for further replies.

ssv45324

Programmer
Mar 17, 2006
16
US
I need to make a column not null when a value in some other column is not null.
How can i acheive this in SQL?
 
with table CHECK constraint:
Code:
ALTER TABLE blah
ADD CONSTRAINT chk_anynameyoulike
CHECK (NOT (someOtherColumn IS NOT NULL AND aColumn IS NULL))

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
You can create a trigger on the table that updates Col2 to NULL when Col1 is NOT NULL.
 
I'd say there is no need for trigger - because integrity rule does not access data outside current row.

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Vongrunt, I thought both columns would be on the same row.

ssv45324, a check constraint will not update the column value for you. Is it your intention to change the value or just to ensure a column is null when another one is not?
 
My intention is not to change, just to ensure that a column in null when another one is not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top