Subject Check Constraint
Author Edwin A. Epstein, III
I just ran into a problem due to an error with a single line of code.
Pretty gosh darn important line of code. To say the crap hit the fan is a
major understatement. I am now trying to put a "safety" into the database
to never allow this again. To do this I think I need a check constraint,
but I am at a loss on exactly how to implement it.

I have 3 fields in a table:

PHONE DOUBLE PRECISION
D_STATE INTEGER
LOG_ID INTEGER

Now there is a primary key on PHONE and LOG_ID together. This allows me to
create multiple lists of phone numbers, but to not have more than one phone
number in any single list.

every time a phone number is in "use" the d_state is equal to 0. Any
D_STATE value other than 0 is not in use.
A phone number should not be in use more than once at any given time.

The following situation is okay:

PHONE,D_STATE,LOG_ID
7025551212,0,4000
7025551212,1,3000
7025551212,1,2000
7025551212,-3,1000

This situation is NOT:

PHONE,D_STATE,LOG_ID
7025551212,0,4001
7025551212,0,4000
7025551212,1,3000
7025551212,1,2000
7025551212,-3,1000

Now the line of code that was so important, has effectively prevented this
since I have created this database well over a year ago. However, I now
want to put the check in to give it an extra level of safety. I also need
to do this to assure those above me that I have in fact taken measures to
prevent any future occurances (soften the blow, silver lining, take your
pick)

I cannot just create a unique index in addition to the PK since as you can
see it is possible for the D_STATE to have 2 values of "1". They both
indicate the phone number was no longer in use on the list.

I beleive if I could create a constraint that would not allow 2 D_STATE
fields to share the specific value of "0" while sharing the same PHONE
number that would give me my safety.

If this can be done with a check constraint I would sincerely appreciate
your help! :)

P.S - Sorry about the novel, but I wanted to fully explain myself the first
time :)

Thanks, Ed