Re: database constraints

From: sklassen(at)commandprompt(dot)com
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:35:21
Message-ID: 20041006083521.A32367@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

* Ben <bench(at)silentmedia(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Fetter 2004-10-06 15:38:49 Re: database constraints
Previous Message Mark Gibson 2004-10-06 15:32:13 Re: Cache lookup failed for relation, when trying to DROP

Browse pgsql-hackers by date

  From Date Subject
Next Message David Fetter 2004-10-06 15:38:49 Re: database constraints
Previous Message Bruce Momjian 2004-10-06 15:32:49 Re: [pgsql-hackers-win32] win32 tablespace handing