470,815 Members | 1,294 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

One or Both Foregin Keys

I am working on a design where a location for something can have:

Fully qualified address with even building names, room numbers, and
booth numbers.
**-OR-**
GPS location
**-OR-**
Both

Other than triggers, is there a way to enforce this using tables and
primary keys?

I imagine an address table, and then

either a some sort of intermediate table, in a one to many
relationship, GPS to Addresses,
or the correct columns in the address table.

Either way, I don't see a normal way to enforce EITHER or BOTH without a
trigger.

Thanks in Advance.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #1
20 1871
Dennis Gearon <ge*****@fireserve.net> writes:
I am working on a design where a location for something can have:
Fully qualified address with even building names, room numbers, and
booth numbers.
**-OR-**
GPS location
**-OR-**
Both Other than triggers, is there a way to enforce this using tables and
primary keys?


Put foreign key constraints on the address and GPS loc (so that they
must be either valid or NULL) and then add a CHECK constraint saying
they can't both be NULL.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #2
Dennis Gearon <ge*****@fireserve.net> writes:
I am working on a design where a location for something can have:
Fully qualified address with even building names, room numbers, and
booth numbers.
**-OR-**
GPS location
**-OR-**
Both Other than triggers, is there a way to enforce this using tables and
primary keys?


Put foreign key constraints on the address and GPS loc (so that they
must be either valid or NULL) and then add a CHECK constraint saying
they can't both be NULL.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #3
Hi,

I have a key structure like so:

a c b
+----------+ +---------+ +----------+
| PriKey1 |--->| ForKey |<---| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

....where c.ForKey is a value from PriKey1 OR PriKey2, which are different
values. All fields have the same data type; a.PriKey1 and b.PriKey2 are
sequences. How does one enfore referential integrity in this structure so
that c.ForKey references a.PriKeya when Flag is True or references b.PriKey2
when Flag is False? Looked pretty hard through the lists and on Google last
night with no luck :(

-Ryan Riehle
http://www.buildways.com

KEYWORDS: one foreign key, multiple primary keys, multiple tables,
generalization, superclass, subclass, constraint
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #4
Hi,

I have a key structure like so:

a c b
+----------+ +---------+ +----------+
| PriKey1 |--->| ForKey |<---| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

....where c.ForKey is a value from PriKey1 OR PriKey2, which are different
values. All fields have the same data type; a.PriKey1 and b.PriKey2 are
sequences. How does one enfore referential integrity in this structure so
that c.ForKey references a.PriKeya when Flag is True or references b.PriKey2
when Flag is False? Looked pretty hard through the lists and on Google last
night with no luck :(

-Ryan Riehle
http://www.buildways.com

KEYWORDS: one foreign key, multiple primary keys, multiple tables,
generalization, superclass, subclass, constraint
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #5
Use CHECK when you create the table.

Ryan Riehle wrote:
Hi,

I have a key structure like so:

a c b
+----------+ +---------+ +----------+
| PriKey1 |--->| ForKey |<---| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

...where c.ForKey is a value from PriKey1 OR PriKey2, which are different
values. All fields have the same data type; a.PriKey1 and b.PriKey2 are
sequences. How does one enfore referential integrity in this structure so
that c.ForKey references a.PriKeya when Flag is True or references b.PriKey2
when Flag is False? Looked pretty hard through the lists and on Google last
night with no luck :(

-Ryan Riehle
http://www.buildways.com

KEYWORDS: one foreign key, multiple primary keys, multiple tables,
generalization, superclass, subclass, constraint
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #6
Use CHECK when you create the table.

Ryan Riehle wrote:
Hi,

I have a key structure like so:

a c b
+----------+ +---------+ +----------+
| PriKey1 |--->| ForKey |<---| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

...where c.ForKey is a value from PriKey1 OR PriKey2, which are different
values. All fields have the same data type; a.PriKey1 and b.PriKey2 are
sequences. How does one enfore referential integrity in this structure so
that c.ForKey references a.PriKeya when Flag is True or references b.PriKey2
when Flag is False? Looked pretty hard through the lists and on Google last
night with no luck :(

-Ryan Riehle
http://www.buildways.com

KEYWORDS: one foreign key, multiple primary keys, multiple tables,
generalization, superclass, subclass, constraint
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #7
On Sat, May 01, 2004 at 14:51:14 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
Hi,

I have a key structure like so:

a c b
+----------+ +---------+ +----------+
| PriKey1 |--->| ForKey |<---| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

...where c.ForKey is a value from PriKey1 OR PriKey2, which are different
values. All fields have the same data type; a.PriKey1 and b.PriKey2 are
sequences. How does one enfore referential integrity in this structure so
that c.ForKey references a.PriKeya when Flag is True or references b.PriKey2
when Flag is False? Looked pretty hard through the lists and on Google last
night with no luck :(


Your diagram seems to indicate something other than what you said.
If c is supposed to reference a or b from one field I don't think you will
be able to do that without writing your own triggers. If you can use
two fields you call use NULL in the one that isn't active and use
constraints to make sure exactly the one that is suppused to be nonNULL is.

If you really have a and b pointing to c, then duplicate flag and a and b
and use a combined foreign key reference of the primary key and the flag
into c. Use constraints to make sure the flag field is always true for a
and always false for b.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #8
On Sat, May 01, 2004 at 14:51:14 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
Hi,

I have a key structure like so:

a c b
+----------+ +---------+ +----------+
| PriKey1 |--->| ForKey |<---| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

...where c.ForKey is a value from PriKey1 OR PriKey2, which are different
values. All fields have the same data type; a.PriKey1 and b.PriKey2 are
sequences. How does one enfore referential integrity in this structure so
that c.ForKey references a.PriKeya when Flag is True or references b.PriKey2
when Flag is False? Looked pretty hard through the lists and on Google last
night with no luck :(


Your diagram seems to indicate something other than what you said.
If c is supposed to reference a or b from one field I don't think you will
be able to do that without writing your own triggers. If you can use
two fields you call use NULL in the one that isn't active and use
constraints to make sure exactly the one that is suppused to be nonNULL is.

If you really have a and b pointing to c, then duplicate flag and a and b
and use a combined foreign key reference of the primary key and the flag
into c. Use constraints to make sure the flag field is always true for a
and always false for b.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #9
Sorry; Arrows are going the wrong way (seems like that caused some
confusion).
Corrected:

a c b
+----------+ +---------+ +----------+
| PriKey1 |<---| ForKey |--->| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

Kind Regards,

-Ryan Riehle
http://www.buildways.com

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Bruno Wolff III
Sent: Saturday, May 01, 2004 4:18 PM
To: Ryan Riehle
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] 1 foreign key to 2 different tables?
On Sat, May 01, 2004 at 14:51:14 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
Hi,

I have a key structure like so:

a c b
+----------+ +---------+ +----------+
| PriKey1 |--->| ForKey |<---| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

...where c.ForKey is a value from PriKey1 OR PriKey2, which are
different values. All fields have the same data type; a.PriKey1 and
b.PriKey2 are sequences. How does one enfore referential integrity in
this structure so that c.ForKey references a.PriKeya when Flag is True
or references b.PriKey2 when Flag is False? Looked pretty hard through
the lists and on Google last night with no luck :(


Your diagram seems to indicate something other than what you said. If c is
supposed to reference a or b from one field I don't think you will be able
to do that without writing your own triggers. If you can use two fields you
call use NULL in the one that isn't active and use constraints to make sure
exactly the one that is suppused to be nonNULL is.

If you really have a and b pointing to c, then duplicate flag and a and b
and use a combined foreign key reference of the primary key and the flag
into c. Use constraints to make sure the flag field is always true for a and
always false for b.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #10
Sorry; Arrows are going the wrong way (seems like that caused some
confusion).
Corrected:

a c b
+----------+ +---------+ +----------+
| PriKey1 |<---| ForKey |--->| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

Kind Regards,

-Ryan Riehle
http://www.buildways.com

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Bruno Wolff III
Sent: Saturday, May 01, 2004 4:18 PM
To: Ryan Riehle
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] 1 foreign key to 2 different tables?
On Sat, May 01, 2004 at 14:51:14 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
Hi,

I have a key structure like so:

a c b
+----------+ +---------+ +----------+
| PriKey1 |--->| ForKey |<---| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

...where c.ForKey is a value from PriKey1 OR PriKey2, which are
different values. All fields have the same data type; a.PriKey1 and
b.PriKey2 are sequences. How does one enfore referential integrity in
this structure so that c.ForKey references a.PriKeya when Flag is True
or references b.PriKey2 when Flag is False? Looked pretty hard through
the lists and on Google last night with no luck :(


Your diagram seems to indicate something other than what you said. If c is
supposed to reference a or b from one field I don't think you will be able
to do that without writing your own triggers. If you can use two fields you
call use NULL in the one that isn't active and use constraints to make sure
exactly the one that is suppused to be nonNULL is.

If you really have a and b pointing to c, then duplicate flag and a and b
and use a combined foreign key reference of the primary key and the flag
into c. Use constraints to make sure the flag field is always true for a and
always false for b.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #11
For what I am reading now it looks like this is an opportunity to use CREATE
ASSERTION, but this functionality appears to be unstable so far and is not
recommended for production environments. Is this correct? Otherwise, can
someone post an example of implementing a check constraint or trigger since
I have not created a check onstraint that is above common complexity and and
have never tried a trigger.

Kind Regards,

-Ryan Riehle
http://www.buildways.com
-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Ryan Riehle
Sent: Saturday, May 01, 2004 4:57 PM
To: 'Bruno Wolff III'
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] 1 foreign key to 2 different tables?
Sorry; Arrows are going the wrong way (seems like that caused some
confusion).
Corrected:

a c b
+----------+ +---------+ +----------+
| PriKey1 |<---| ForKey |--->| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

Kind Regards,

-Ryan Riehle
http://www.buildways.com

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Bruno Wolff III
Sent: Saturday, May 01, 2004 4:18 PM
To: Ryan Riehle
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] 1 foreign key to 2 different tables?
On Sat, May 01, 2004 at 14:51:14 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
Hi,

I have a key structure like so:

a c b
+----------+ +---------+ +----------+
| PriKey1 |--->| ForKey |<---| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

...where c.ForKey is a value from PriKey1 OR PriKey2, which are
different values. All fields have the same data type; a.PriKey1 and
b.PriKey2 are sequences. How does one enfore referential integrity in
this structure so that c.ForKey references a.PriKeya when Flag is True
or references b.PriKey2 when Flag is False? Looked pretty hard through
the lists and on Google last night with no luck :(


Your diagram seems to indicate something other than what you said. If c is
supposed to reference a or b from one field I don't think you will be able
to do that without writing your own triggers. If you can use two fields you
call use NULL in the one that isn't active and use constraints to make sure
exactly the one that is suppused to be nonNULL is.

If you really have a and b pointing to c, then duplicate flag and a and b
and use a combined foreign key reference of the primary key and the flag
into c. Use constraints to make sure the flag field is always true for a and
always false for b.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #12
For what I am reading now it looks like this is an opportunity to use CREATE
ASSERTION, but this functionality appears to be unstable so far and is not
recommended for production environments. Is this correct? Otherwise, can
someone post an example of implementing a check constraint or trigger since
I have not created a check onstraint that is above common complexity and and
have never tried a trigger.

Kind Regards,

-Ryan Riehle
http://www.buildways.com
-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Ryan Riehle
Sent: Saturday, May 01, 2004 4:57 PM
To: 'Bruno Wolff III'
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] 1 foreign key to 2 different tables?
Sorry; Arrows are going the wrong way (seems like that caused some
confusion).
Corrected:

a c b
+----------+ +---------+ +----------+
| PriKey1 |<---| ForKey |--->| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

Kind Regards,

-Ryan Riehle
http://www.buildways.com

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Bruno Wolff III
Sent: Saturday, May 01, 2004 4:18 PM
To: Ryan Riehle
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] 1 foreign key to 2 different tables?
On Sat, May 01, 2004 at 14:51:14 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
Hi,

I have a key structure like so:

a c b
+----------+ +---------+ +----------+
| PriKey1 |--->| ForKey |<---| PriKey2 |
| | | Flag | | |
+----------+ +---------+ +----------+

...where c.ForKey is a value from PriKey1 OR PriKey2, which are
different values. All fields have the same data type; a.PriKey1 and
b.PriKey2 are sequences. How does one enfore referential integrity in
this structure so that c.ForKey references a.PriKeya when Flag is True
or references b.PriKey2 when Flag is False? Looked pretty hard through
the lists and on Google last night with no luck :(


Your diagram seems to indicate something other than what you said. If c is
supposed to reference a or b from one field I don't think you will be able
to do that without writing your own triggers. If you can use two fields you
call use NULL in the one that isn't active and use constraints to make sure
exactly the one that is suppused to be nonNULL is.

If you really have a and b pointing to c, then duplicate flag and a and b
and use a combined foreign key reference of the primary key and the flag
into c. Use constraints to make sure the flag field is always true for a and
always false for b.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #13
On Sat, May 01, 2004 at 18:09:34 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
For what I am reading now it looks like this is an opportunity to use CREATE
ASSERTION, but this functionality appears to be unstable so far and is not
recommended for production environments. Is this correct? Otherwise, can
someone post an example of implementing a check constraint or trigger since
I have not created a check onstraint that is above common complexity and and
have never tried a trigger.


The simplest way to do this is probably be to use two separate fields
to make the references and make sure exactly one of them is nonnull.
You also might want to rethink your design. That you want to do this
suggests that there is something odd about the schema design you
have come up with.

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #14
On Sat, May 01, 2004 at 18:09:34 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
For what I am reading now it looks like this is an opportunity to use CREATE
ASSERTION, but this functionality appears to be unstable so far and is not
recommended for production environments. Is this correct? Otherwise, can
someone post an example of implementing a check constraint or trigger since
I have not created a check onstraint that is above common complexity and and
have never tried a trigger.


The simplest way to do this is probably be to use two separate fields
to make the references and make sure exactly one of them is nonnull.
You also might want to rethink your design. That you want to do this
suggests that there is something odd about the schema design you
have come up with.

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #15
Thanks for your input. Yes, there is a lot more to this part of the schema.
The current stucture makes the most sense to us so far, after lots of
thinking. If you are interested I can offer you more details about the
structure, but for now I am looking for how to implement this type of
constraint with a trigger or another method - if there is a better way.

-Ryan Riehle
http://www.buildways.com

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Bruno Wolff III
Sent: Saturday, May 01, 2004 9:40 PM
To: Ryan Riehle
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] 1 foreign key to 2 different tables?
On Sat, May 01, 2004 at 18:09:34 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
For what I am reading now it looks like this is an opportunity to use
CREATE ASSERTION, but this functionality appears to be unstable so far and is not recommended for production environments. Is this correct? Otherwise, can someone post an example of implementing a check constraint or trigger
since I have not created a check onstraint that is above common
complexity and and have never tried a trigger.


The simplest way to do this is probably be to use two separate fields to
make the references and make sure exactly one of them is nonnull. You also
might want to rethink your design. That you want to do this suggests that
there is something odd about the schema design you have come up with.

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #16
Thanks for your input. Yes, there is a lot more to this part of the schema.
The current stucture makes the most sense to us so far, after lots of
thinking. If you are interested I can offer you more details about the
structure, but for now I am looking for how to implement this type of
constraint with a trigger or another method - if there is a better way.

-Ryan Riehle
http://www.buildways.com

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Bruno Wolff III
Sent: Saturday, May 01, 2004 9:40 PM
To: Ryan Riehle
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] 1 foreign key to 2 different tables?
On Sat, May 01, 2004 at 18:09:34 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
For what I am reading now it looks like this is an opportunity to use
CREATE ASSERTION, but this functionality appears to be unstable so far and is not recommended for production environments. Is this correct? Otherwise, can someone post an example of implementing a check constraint or trigger
since I have not created a check onstraint that is above common
complexity and and have never tried a trigger.


The simplest way to do this is probably be to use two separate fields to
make the references and make sure exactly one of them is nonnull. You also
might want to rethink your design. That you want to do this suggests that
there is something odd about the schema design you have come up with.

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #17
On Sun, May 02, 2004 at 03:51:44 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
Thanks for your input. Yes, there is a lot more to this part of the schema.
The current stucture makes the most sense to us so far, after lots of
thinking. If you are interested I can offer you more details about the
structure, but for now I am looking for how to implement this type of
constraint with a trigger or another method - if there is a better way.


So far I haven't seen a good case for why there needs to only be one
pointer instead of two. When you are doing joins you are going to know
which one you want to join on. They are internal keys so you shouldn't
need to print them out directly. If you want to display something that
comes out of one joined table or the other, you are going to have to
join both a and b to c using some sort of outer joins. You can use
coalesce to get the information form the appropiate joined table.
You will still have to do things like this even if you share the pointer
field c, but it will be easier to set up the constraints if there are
two separate fields.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #18
On Sun, May 02, 2004 at 03:51:44 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
Thanks for your input. Yes, there is a lot more to this part of the schema.
The current stucture makes the most sense to us so far, after lots of
thinking. If you are interested I can offer you more details about the
structure, but for now I am looking for how to implement this type of
constraint with a trigger or another method - if there is a better way.


So far I haven't seen a good case for why there needs to only be one
pointer instead of two. When you are doing joins you are going to know
which one you want to join on. They are internal keys so you shouldn't
need to print them out directly. If you want to display something that
comes out of one joined table or the other, you are going to have to
join both a and b to c using some sort of outer joins. You can use
coalesce to get the information form the appropiate joined table.
You will still have to do things like this even if you share the pointer
field c, but it will be easier to set up the constraints if there are
two separate fields.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #19
I don't know if this will work in your situation, but you might look
into having table A and table B inherit from a common base table (where
the column referenced by the foreign key in C is defined).

Tim

On May 2, 2004, at 2:51 AM, Ryan Riehle wrote:
Thanks for your input. Yes, there is a lot more to this part of the
schema.
The current stucture makes the most sense to us so far, after lots of
thinking. If you are interested I can offer you more details about the
structure, but for now I am looking for how to implement this type of
constraint with a trigger or another method - if there is a better way.

-Ryan Riehle
http://www.buildways.com

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Bruno Wolff
III
Sent: Saturday, May 01, 2004 9:40 PM
To: Ryan Riehle
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] 1 foreign key to 2 different tables?
On Sat, May 01, 2004 at 18:09:34 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
For what I am reading now it looks like this is an opportunity to use
CREATE ASSERTION, but this functionality appears to be unstable so
far and

is not
recommended for production environments. Is this correct?
Otherwise,

can
someone post an example of implementing a check constraint or trigger
since I have not created a check onstraint that is above common
complexity and and have never tried a trigger.


The simplest way to do this is probably be to use two separate fields
to
make the references and make sure exactly one of them is nonnull. You
also
might want to rethink your design. That you want to do this suggests
that
there is something odd about the schema design you have come up with.

---------------------------(end of
broadcast)---------------------------
TIP 8: explain analyze is your friend

---------------------------(end of
broadcast)---------------------------
TIP 8: explain analyze is your friend

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #20
I don't know if this will work in your situation, but you might look
into having table A and table B inherit from a common base table (where
the column referenced by the foreign key in C is defined).

Tim

On May 2, 2004, at 2:51 AM, Ryan Riehle wrote:
Thanks for your input. Yes, there is a lot more to this part of the
schema.
The current stucture makes the most sense to us so far, after lots of
thinking. If you are interested I can offer you more details about the
structure, but for now I am looking for how to implement this type of
constraint with a trigger or another method - if there is a better way.

-Ryan Riehle
http://www.buildways.com

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Bruno Wolff
III
Sent: Saturday, May 01, 2004 9:40 PM
To: Ryan Riehle
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] 1 foreign key to 2 different tables?
On Sat, May 01, 2004 at 18:09:34 -0400,
Ryan Riehle <rk*@buildways.com> wrote:
For what I am reading now it looks like this is an opportunity to use
CREATE ASSERTION, but this functionality appears to be unstable so
far and

is not
recommended for production environments. Is this correct?
Otherwise,

can
someone post an example of implementing a check constraint or trigger
since I have not created a check onstraint that is above common
complexity and and have never tried a trigger.


The simplest way to do this is probably be to use two separate fields
to
make the references and make sure exactly one of them is nonnull. You
also
might want to rethink your design. That you want to do this suggests
that
there is something odd about the schema design you have come up with.

---------------------------(end of
broadcast)---------------------------
TIP 8: explain analyze is your friend

---------------------------(end of
broadcast)---------------------------
TIP 8: explain analyze is your friend

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Blair Hall | last post: by
9 posts views Thread by Etienne Charland | last post: by
reply views Thread by Dennis Gearon | last post: by
reply views Thread by Menlalen | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.