Re: database constraints - Mailing list pgsql-general

From sklassen@commandprompt.com
Subject Re: database constraints
Date
Msg-id 20041006083521.A32367@commandprompt.com
Whole thread Raw
In response to database constraints  (Ben <bench@silentmedia.com>)
Responses Re: database constraints
List pgsql-general
* Ben <bench@silentmedia.com> [2004-10-06 08:23:11 -0700]:

> If I have have the table:
>
> create table foo
> (
>     a int references bar(id),
>     b int references baz(id)
> )
>
> ... how do I make sure one and only one of the columns a and b are
> non-null? Is it even reasonable?

ALTER TABLE foo ADD CONSTRAINT must_have_a_or_b CHECK (a::int IS NULL
AND b::int IS NOT NULL OR a::int IS NOT NULL AND b::int IS NULL);

--
Steven Klassen - Lead Programmer
Command Prompt, Inc. - http://www.commandprompt.com/
PostgreSQL Replication & Support Services, (503) 667-4564

pgsql-general by date:

Previous
From: Mark Gibson
Date:
Subject: Re: Cache lookup failed for relation, when trying to DROP
Next
From: David Fetter
Date:
Subject: Re: database constraints