Re: Constraint that compares and limits field values - Mailing list pgsql-general

From MargaretGillon@chromalloy.com
Subject Re: Constraint that compares and limits field values
Date
Msg-id OF703239B0.5DF7FA0C-ON88257101.0066FC9D-88257101.00670C61@CHROMALLOY.COM
Whole thread Raw
In response to Re: Constraint that compares and limits field values  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general

>Martijn van Oosterhout <kleptog@svana.org> wrote on 01/25/2006 10:20:40 AM:
> On Wed, Jan 25, 2006 at 09:55:58AM -0800, MargaretGillon@chromalloy.com wrote:
>
> Parhaps something like:
>
> CHECK((CASE WHEN evenid1 IS NOT NULL THEN 1 ELSE 0 END) +
>       (CASE WHEN evenid2 IS NOT NULL THEN 1 ELSE 0 END) +
>       (CASE WHEN evenid3 IS NOT NULL THEN 1 ELSE 0 END)) = 1;
>

This works with a few modifications... needed an extra ( ) enclosing entire statement.

ALTER TABLE event
ADD CONSTRAINT two_nulls_1  
CHECK (((CASE WHEN evenid1 IS NOT NULL THEN 1 ELSE 0 END) +
      (CASE WHEN evevid1 IS NOT NULL THEN 1 ELSE 0 END) +
      (CASE WHEN evreid1 IS NOT NULL THEN 1 ELSE 0 END)) = 1);

> If you can find a function to turn a bool into an int it becomes even
> easier.

I am in version 7.3 and it will not let me cast the boolean to an integer.

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Margaret Gillon, IS Dept., Chromalloy Los Angeles, ext. 297

pgsql-general by date:

Previous
From: Patrick Hatcher
Date:
Subject: Trigger question: ROW or STATEMENT?
Next
From: Michael Fuhr
Date:
Subject: Re: Constraint that compares and limits field values