pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Foreign key with check?
2+ messages / 2 participants
[nested] [flat]

* Foreign key with check?
@ 2005-07-27 20:08 Jeff Boes <jeff@endpoint.com>
  2005-07-27 20:37 ` Re: Foreign key with check? Bruno Wolff III <bruno@wolff.to>
  0 siblings, 1 reply; 2+ messages in thread

From: Jeff Boes @ 2005-07-27 20:08 UTC (permalink / raw)
  To: pgsql-sql

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).

I think I have to do this with an additional trigger. Any other options?




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Foreign key with check?
  2005-07-27 20:08 Foreign key with check? Jeff Boes <jeff@endpoint.com>
@ 2005-07-27 20:37 ` Bruno Wolff III <bruno@wolff.to>
  0 siblings, 0 replies; 2+ messages in thread

From: Bruno Wolff III @ 2005-07-27 20:37 UTC (permalink / raw)
  To: Jeff Boes <jeff@endpoint.com>; +Cc: pgsql-sql

On Wed, Jul 27, 2005 at 16:08:19 -0400,
  Jeff Boes <jeff@endpoint.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.



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2005-07-27 20:37 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-07-27 20:08 Foreign key with check? Jeff Boes <jeff@endpoint.com>
2005-07-27 20:37 ` Bruno Wolff III <bruno@wolff.to>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox