pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Miles Keaton <mileskeaton@gmail.com>
To: pgsql-sql@postgresql.org
Subject: one table being used for two purposes with foreign key for each?
Date: Sun, 2 Jan 2005 02:05:18 -0800
Message-ID: <59b2d39b050102020525847461@mail.gmail.com> (raw)

What if, like Amazon, a customer can have more than one address on
file?  Then you make "addresses" a separate table with one-to-many
relationship.

So then you're making affiliates (or clients, or distributors) and you
realize it would be nice to re-use the fields you already have there
in the "addresses" table, even though the affiliate's address is
one-to-one, NOT a one-to-many.

Would it be bad design to have different foreign keys, as shown below?
 Is this a bad idea?

Should I just re-type the address field definitions directly into the
affiliates table, since it's not one-to-many?


create table customers (
id serial primary key,
name varchar(64)
);

create table affiliates (
id serial primary key,
name varchar(64)
);

create table addresses (
id serial primary key,
customer_id int REFERENCES customers(id),
affiliate_id int REFERENCES affiliates(id),
addr1 varchar(64),
addr2 varchar(64),
city varchar(64),
state varchar(12),
postalcode varchar(12),
country char(62),
CONSTRAINT needs_link CHECK (customer_id IS NOT NULL OR affiliate_id
IS NOT NULL)
);



Message-ID: <59b2d39b050102020525847461@mail.gmail.com>
Permalink:  ../59b2d39b050102020525847461@mail.gmail.com/
Also on:    postgresql.org/message-id/59b2d39b050102020525847461@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mileskeaton@gmail.com
  Subject: Re: one table being used for two purposes with foreign key for each?
  In-Reply-To: <59b2d39b050102020525847461@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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