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

From Martijn van Oosterhout
Subject Re: Constraint that compares and limits field values
Date
Msg-id 20060125182040.GD31698@svana.org
Whole thread Raw
In response to Re: Constraint that compares and limits field values  (MargaretGillon@chromalloy.com)
Responses Re: Constraint that compares and limits field values  (MargaretGillon@chromalloy.com)
List pgsql-general
On Wed, Jan 25, 2006 at 09:55:58AM -0800, MargaretGillon@chromalloy.com wrote:
> ALTER TABLE event
> ADD CONSTRAINT two_nulls_1 CHECK
> ((evenid1 IS NULL) AND (evevid1 IS NULL) and (evreid1 IS NOT NULL)) OR
> ((evenid1 IS NULL) AND (evevid1 IS NOT NULL) and (evreid1 IS NULL)) OR
> ((evenid1 IS NOT NULL) AND (evevid1 IS NULL) and (evreid1 IS  NULL))

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;

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

Hope this helps,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

Attachment

pgsql-general by date:

Previous
From: "gianni.zammarchi@tiscali.it"
Date:
Subject: xml support on win32 postgres
Next
From: Michael Fuhr
Date:
Subject: Re: Constraint that compares and limits field values