Re: Making a foreign key chain - good idea or bad idea?

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Frank Joerdens <frank(at)joerdens(dot)de>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Making a foreign key chain - good idea or bad idea?
Date: 2001-01-24 20:30:18
Message-ID: Pine.BSF.4.21.0101241225520.58399-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


On Wed, 24 Jan 2001, Frank Joerdens wrote:

> I just did something which seems to work alright and which makes sense
> to me now but which I have a funny feeling about. It may be good
> standard practice (and I just don't know about it) or dangerously
> foolish or just plain silly: I created a foreign key reference on a
> column that is also the primary key for this table as in
>
> create table institute (
> id int4 references index ( id ) PRIMARY KEY,
> . . .
>
> and then used that column as a reference for a foreign key constraint in
> a couple of other tables:
>
> create table boss (
> institute_id int4 references institute ( id ),
> . . .
>
> create table staff (
> institute_id int4 references institute ( id ),
> . . .
>
> I am not really sure what happens when I delete or modify the id column
> in the table at the top of this "chain". Except for this uncertainty I
> don't think this scheme would pose a problem, but I may be wrong. Can
> anyone enlighten me?

If you mean index(id), as long as there exists an institute(id) that
references it, you won't be allowed to delete or update it to a distinct
value. And, you won't be able to delete or update institute(id) as long
as there exists at least one boss(id) or staff(id) that references it.
You know that there shouldn't be orphaned boss(id) or staff(id) rows
because those can't exist without a institute(id) row of the correct
value and that requires the index(id) value.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Mike Castle 2001-01-24 20:55:52 Re: Problem with Dates
Previous Message Glen and Rosanne Eustace 2001-01-24 19:49:27 Problem with Dates