Re: Foreign key with check?

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Jeff Boes <jeff(at)endpoint(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Foreign key with check?
Date: 2005-07-27 20:37:59
Message-ID: 20050727203759.GA3884@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, Jul 27, 2005 at 16:08:19 -0400,
Jeff Boes <jeff(at)endpoint(dot)com> wrote:
> Given a table like this:
>
> create table primary (
> a integer primary key,
> b boolean
> );
>
> And another like this:
>
> create table secondary (
> a integer,
> some_other_fields
> );
>
>
> I would like a foreign key constraint on the "secondary" table that
> looks something like:
>
> foreign key (a, true) references primary (a, b)
>
> That is, a row in "secondary" is allowed to reference a row in "primary"
> if and only if that referenced row has (b = true).

If what you are saying is that all entries in secondary need to reference
a row in primary and in addition the referenced row must have b true, then
you can do this fairly simply using a bit of extra space.
Add a row b to secondary with a constraint that b = true. Then make the
foreign key reference on both a and b.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2005-07-28 05:08:55 Re: REINDEX DATABASE
Previous Message Jeff Boes 2005-07-27 20:08:19 Foreign key with check?