473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2061
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Blair Hall | last post by:
I would like to determine whether two dictionaries have the same set of keys. Can anyone tell me if I HAVE to sort the key sequences as in this code snippet: # d1, d2 already created k1 =...
31
by: Robert Brown | last post by:
Let's say I have a type hierarchy: (just an example) the general entity customer: CREATE TABLE customer(customer_id int, customer_name varchar(250), customer_type int) three specific...
9
by: Etienne Charland | last post by:
Hi, there is an application running on a remote desktop (under Citrix ICA, but the same problem applies for RDC or PC Anywhere). Now, I want to send keys to the remote application from a local app....
0
by: OC | last post by:
Because of the requirements on a high-speed data entry form, I need to reliably trap certain keys (specifically the "/", "*", "-", and "+") keys on the 10-key pad and treat them as "Function keys"....
0
by: Dennis Gearon | last post by:
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 ...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
11
by: John | last post by:
I am coding a radix sort in python and I think that Python's dictionary may be a choice for bucket. The only problem is that dictionary is a mapping without order. But I just found that if the...
2
by: Charles Law | last post by:
I'll kick myself when you tell me, but ... I have a user control on a form, and I want the user control to see the arrow keys when I press them. If I press just about any other key the control's...
0
by: Menlalen | last post by:
K E Y S , A L I C I A M P 3 D O W N L O A D *Fallin 2001* 'Keys, Alicia - Fallin' (http://tinyurl.com/4zg3sr) (3:30 4.82Mb 192kbps) 'Keys, Alicia - Fallin (ft BustaRhymes and Rampage)'...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.