Re: Constraint that compares and limits field values

From: MargaretGillon(at)chromalloy(dot)com
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: Richard Huxton <dev(at)archonet(dot)com>, MargaretGillon(at)chromalloy(dot)com, Michael Fuhr <mike(at)fuhr(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: Constraint that compares and limits field values
Date: 2006-01-25 18:52:55
Message-ID: OF703239B0.5DF7FA0C-ON88257101.0066FC9D-88257101.00670C61@CHROMALLOY.COM
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>Martijn van Oosterhout <kleptog(at)svana(dot)org> wrote on 01/25/2006 10:20:40
AM:
> On Wed, Jan 25, 2006 at 09:55:58AM -0800, MargaretGillon(at)chromalloy(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2006-01-25 18:58:14 Re: Constraint that compares and limits field values
Previous Message MargaretGillon 2006-01-25 18:51:23 Re: Constraint that compares and limits field values