pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: sqllist <pgsql-sql@postgresql.org>
Subject: two tables - foreign keys referring to each other...]
Date: Wed, 21 Feb 2001 08:42:07 -0800
Message-ID: <3A93EFDF.C4FC5689@agliodbs.com> (raw)

Chris,

> ..or generally: how do you create two crosslinked foreign keyed tables?
> 
> hopefully an easy problem for the real professionals!

	No.  The problem is:  Why would you want to create two crosslinked
foriegn-keyed tables?  As an experienced SQL professional, this seems
like a recipe for instant disaster to me.

	The purpose of a foriegn key is to enforce a parent-->child or data
table<--reference list relationships, to prevent incomplete or erroneous
records from being added.  This requires the foriegn key relationship to
be one-way.

	In fact, if you built your two-way foriegn keys (using ALTER TABLE), I
think you might find that you can't add any records to either table. 
Certainly you won't be able to delete any.  I once had an experience
with a complex legacy data structure where I accidentally set up a
circular foriegn key relationship (among 5 tables), and I had to
re-build the database from scripts to fix it.

	If you're looking to set up a many-to-many relationship, then what you
want is a linking table.  However, I highly advise you to pick up a
primer on database design (such as "Database Design for Mere Mortals")
before proceeding any further, or you'll end up spending the next year
paying for what you don't understand now.

					-Josh Berkus
-- 
______AGLIO DATABASE SOLUTIONS___________________________
                                        Josh Berkus
   Complete information technology      josh@agliodbs.com
    and data management solutions       (415) 565-7293
   for law firms, small businesses       fax  621-2533
    and non-profit organizations.       San Francisco



view thread (8+ messages)  latest in thread

Message-ID: <3A93EFDF.C4FC5689@agliodbs.com>
Permalink:  ../3A93EFDF.C4FC5689@agliodbs.com/
Also on:    postgresql.org/message-id/3A93EFDF.C4FC5689@agliodbs.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com
  Subject: Re: two tables - foreign keys referring to each other...]
  In-Reply-To: <3A93EFDF.C4FC5689@agliodbs.com>
  References: <3A93EFDF.C4FC5689@agliodbs.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