Re: database constraints

From: David Fetter <david(at)fetter(dot)org>
To: Ben <bench(at)silentmedia(dot)com>
Cc: Postgres general mailing list <pgsql-general(at)postgresql(dot)org>
Subject: Re: database constraints
Date: 2004-10-06 15:38:49
Message-ID: 20041006153849.GB30061@fetter.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Wed, Oct 06, 2004 at 08:23:11AM -0700, Ben wrote:
> 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?

You could write it like this:

CREATE TABLE foo
(
a INT REFERENCES bar(id),
b INT REFERENCES baz(id),
CHECK((a IS NULL AND b IS NOT NULL) OR (a IS NOT NULL AND b IS NULL))
);

> Is it even reasonable?

What's "reasonable?" ;)

BTW, "id" is a terrible name for a column. Better call it foo_id.

Cheers,
D
--
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mark Gibson 2004-10-06 15:44:07 Re: Cache lookup failed for relation, when trying to DROP
Previous Message sklassen 2004-10-06 15:35:21 Re: database constraints

Browse pgsql-hackers by date

  From Date Subject
Next Message Joshua D. Drake 2004-10-06 16:32:02 Re: database constraints
Previous Message sklassen 2004-10-06 15:35:21 Re: database constraints