pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedCreating a foreign key on the Union of two tables
5+ messages / 5 participants
[nested] [flat]
* Creating a foreign key on the Union of two tables
@ 2003-04-06 21:53 Saad Saeed <saads@umich.edu>
2003-04-07 16:48 ` Re: Creating a foreign key on the Union of two tables Rod Taylor <rbt@rbt.ca>
2003-04-07 16:50 ` Re: Creating a foreign key on the Union of two tables Bruno Wolff III <bruno@wolff.to>
2003-04-07 18:27 ` Re: Creating a foreign key on the Union of two tables Josh Berkus <josh@agliodbs.com>
0 siblings, 3 replies; 5+ messages in thread
From: saads@umich.edu @ 2003-04-06 21:53 UTC (permalink / raw)
To: pgsql-sql
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.
Thanks,
-Saad
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Creating a foreign key on the Union of two tables
2003-04-06 21:53 Creating a foreign key on the Union of two tables Saad Saeed <saads@umich.edu>
@ 2003-04-07 16:48 ` Rod Taylor <rbt@rbt.ca>
2003-04-07 21:41 ` Re: Creating a foreign key on the Union of two tables Jan Wieck <JanWieck@Yahoo.com>
2 siblings, 1 reply; 5+ messages in thread
From: Rod Taylor @ 2003-04-07 16:48 UTC (permalink / raw)
To: Saad Saeed <saads@umich.edu>; +Cc: pgsql-sql
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.
--
Rod Taylor <rbt@rbt.ca>
PGP Key: http://www.rbt.ca/rbtpub.asc
Attachments:
signature.asc (application/pgp-signature, 186B) download
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Creating a foreign key on the Union of two tables
2003-04-06 21:53 Creating a foreign key on the Union of two tables Saad Saeed <saads@umich.edu>
2003-04-07 16:48 ` Re: Creating a foreign key on the Union of two tables Rod Taylor <rbt@rbt.ca>
@ 2003-04-07 21:41 ` Jan Wieck <JanWieck@Yahoo.com>
0 siblings, 0 replies; 5+ messages in thread
From: Jan Wieck @ 2003-04-07 21:41 UTC (permalink / raw)
To: Rod Taylor <rbt@rbt.ca>; +Cc: Saad Saeed <saads@umich.edu>; 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@Yahoo.com #
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Creating a foreign key on the Union of two tables
2003-04-06 21:53 Creating a foreign key on the Union of two tables Saad Saeed <saads@umich.edu>
@ 2003-04-07 16:50 ` Bruno Wolff III <bruno@wolff.to>
2 siblings, 0 replies; 5+ messages in thread
From: Bruno Wolff III @ 2003-04-07 16:50 UTC (permalink / raw)
To: Saad Saeed <saads@umich.edu>; +Cc: pgsql-sql
On Sun, Apr 06, 2003 at 14:53:50 -0700,
Saad Saeed <saads@umich.edu> 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.
One way to do this is to have two foreign key references from C. One to
A and the other to B. To make sure just one of these is used, you can
use a constraint in C to make sure exactly one of the columns is null.
This technique doesn't need for the IDs in A and B to be unique accross
both tables.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Creating a foreign key on the Union of two tables
2003-04-06 21:53 Creating a foreign key on the Union of two tables Saad Saeed <saads@umich.edu>
@ 2003-04-07 18:27 ` Josh Berkus <josh@agliodbs.com>
2 siblings, 0 replies; 5+ messages in thread
From: Josh Berkus @ 2003-04-07 18:27 UTC (permalink / raw)
To: saads@umich.edu; pgsql-sql
Saad,
> 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.
IMHO, you cannot do this in MySQL. How about upgrading to PostgreSQL? Then
we can help you.
--
-Josh Berkus
Aglio Database Solutions
San Francisco
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2003-04-07 21:41 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-04-06 21:53 Creating a foreign key on the Union of two tables Saad Saeed <saads@umich.edu>
2003-04-07 16:48 ` Rod Taylor <rbt@rbt.ca>
2003-04-07 21:41 ` Jan Wieck <JanWieck@Yahoo.com>
2003-04-07 16:50 ` Bruno Wolff III <bruno@wolff.to>
2003-04-07 18:27 ` Josh Berkus <josh@agliodbs.com>
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