Re: Shared Foreign Keys From Two Tables

Bruno Wolff III <bruno@wolff.to>

From: Bruno Wolff III <bruno@wolff.to>
To: "Thomas T. Thai" <tom@minnesota.com>
Cc: pgsql-general@postgresql.org
Date: 2004-07-30T18:08:12Z
Lists: pgsql-general
On Fri, Jul 30, 2004 at 12:01:44 -0500,
  "Thomas T. Thai" <tom@minnesota.com> wrote:
> 
> The above setup works. In table groupie_mod_pref, I needed mod_id to be 
> a shared common foreign key in two other tables. Consequently in used 
> two table constraints in groupie_mod_pref. However, I thought that was a 
> waste of storage space having to repeat groupie_id and mod_id from 
> groupie_mod. Is there anyway to keep the functionality like above, while 
> using groupie_mod_id from groupie_mod in groupie_mod_pref in place of 
> groupie_id and mod_id and still constrain mod_id to mod_pref table?

Not easily. (It should be possible to do with triggers.) What you could do
is use a compound primary key in groupie_mod and mod_pref get rid of the
(presumably) surrogate primary keys.