Re: Creating a foreign key on the Union of two tables

From: Jan Wieck <JanWieck(at)Yahoo(dot)com>
To: Rod Taylor <rbt(at)rbt(dot)ca>
Cc: Saad Saeed <saads(at)umich(dot)edu>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Creating a foreign key on the Union of two tables
Date: 2003-04-07 21:41:00
Message-ID: 3E91F06C.2435AD35@Yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Rod Taylor wrote:
>
> On Sun, 2003-04-06 at 17:53, Saad Saeed wrote:
> > I have 2 tables "A" and "B" in mySQL. They both have an attribute 'id'
> > which is unique between the 2 tables. I want the union of these 2
> > attributes to be a foreign key in a 3rd table "C". How do I specify
> > this constraing in sql (mySQL) in table C? And for complex reasons I
> > cannot merge the 2 tables A and B.
>
> Can't say for MySQL (since this is a PostgreSQL list!!!!!), but creating
> a foreign key like that to 2 different tables is against spec because
> one cannot create a unique constraint across 2 tables.
>
> That said, you could create a set of pl/pgsql functions that can imitate
> what a foreign key does and will work in the situation you describe --
> if you're using PostgreSQL.

Or seeting up table D plus custom triggers on A and B so that D contains
the union of both. That would result in a virtual unique constraint over
A and B together since the foreign key constraint now against D requires
it's id column to be unique.

Without more background on the purpose or the underlying business
process it's hard though to tell what's best.

Jan

--
#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================== JanWieck(at)Yahoo(dot)com #

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Rudi Starcevic 2003-04-08 08:29:07 CASE
Previous Message Josh Berkus 2003-04-07 18:27:03 Re: Creating a foreign key on the Union of two tables