Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Check Constraint Expand / Collapse
Author
Message
Posted Wednesday, February 11, 2009 10:59 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, April 04, 2012 6:36 AM
Points: 96, Visits: 228
Can I use a check constraint to enforce the business rule that if one field is populated then another field must be null?

e.g. if field AAA is not null then field BBB must be null
and if field BBB is not null the field AAA must be null
Post #655106
Posted Wednesday, February 11, 2009 12:05 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, April 04, 2012 6:36 AM
Points: 96, Visits: 228
I've managed to solve my own problem. It was easier than I thought, I've just never done anything like that before.

It amounts to;

CONTSTRAINT One_or_the_Other CHECK ((AAA IS NULL AND BBB IS NOT NULL) OR (AAA IS NOT NULL AND BBB IS NULL))
Post #655156
Posted Thursday, February 12, 2009 1:53 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, April 01, 2014 5:55 PM
Points: 957, Visits: 1,027
Actually, this is a good example of what I most commonly use check constraints for. Some sort of dependency between the columns in the table. The other common use I make is where I have a column that needs a specific format or range.
Post #656087
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse