Advice on merging two primary keys...

From: "Eric D(dot) Nielsen" <nielsene(at)MIT(dot)EDU>
To: pgsql-general(at)postgresql(dot)org
Cc: "Eric D(dot) Nielsen" <nielsene(at)mit(dot)edu>
Subject: Advice on merging two primary keys...
Date: 2005-06-28 23:21:06
Message-ID: 46693114-7CFD-43E1-8FF5-6A406B83E19E@mit.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I've come into a situation where I will often need to merge two
primary keys, with numerous foreign keys hanging off of them. For
instance:

CREATE TABLE people (
peopleid SERIAL PRIMARY KEY,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL
);

CREATE TABLE users (
username TEXT PRIMARY KEY,
peopleid INT NOT NULL REFERENCES people ON UPDATE CASCADE ON
DELETE RESTRICT,
...
);

CREATE TABLE results (
peopleid INT NO NULL REFERENCES peopleid ON UPDATE CASCADE ON
DELETE CASCADE,
eventid INT ...
score INT...
);

There are some other tables keyed by peopleid that are normally only
populated by user related peopleids.

The site in question is a sports ranking site. Typically speaking
most "people" are not "users" are have their information populated
from placement sheets. Some people will later create an account and
after in real life authentication the records need to be merged -- ie
there will be records from both peopleid that will need should be
adjusted to a single value.

While any update of the either primary key will cascade to all
relevant tables, such an update is disallowed for uniqueness reasons.

Is there a good SQL-base method to accomplish this type of merging or
does this need application logic?

Eric

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andreas 2005-06-28 23:56:21 Re: automating backup ?
Previous Message Matt Miller 2005-06-28 22:43:04 Re: Building Latest (8.1)