Advice on merging two primary keys... - Mailing list pgsql-general

From Eric D. Nielsen
Subject Advice on merging two primary keys...
Date
Msg-id 46693114-7CFD-43E1-8FF5-6A406B83E19E@mit.edu
Whole thread Raw
Responses Re: Advice on merging two primary keys...  (Richard Huxton <dev@archonet.com>)
List 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

pgsql-general by date:

Previous
From: Matt Miller
Date:
Subject: Re: Building Latest (8.1)
Next
From: Andreas
Date:
Subject: Re: automating backup ?