Re: Basic Q on superfluous primary keys

From: "Merlin Moncure" <mmoncure(at)gmail(dot)com>
To: "Craig A(dot) James" <cjames(at)modgraph-usa(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Basic Q on superfluous primary keys
Date: 2007-04-18 14:44:44
Message-ID: b42b73150704180744h28d99ebflebe4405b3af5e44d@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On 4/18/07, Craig A. James <cjames(at)modgraph-usa(dot)com> wrote:
> Merlin Moncure wrote:
> > In the context of this debate, I see this argument all the time, with
> > the implied suffix: 'If only we used integer keys we would not have
> > had this problem...'. Either the customer identifies parts with a
> > part number or they don't...and if they do identify parts with a
> > number and recycle the numbers, you have a problem...period.
>
> On the contrary. You create a new record with the same part number. You mark the old part number "obsolete". Everything else (the part's description, and all the relationships that it's in, such as order history, catalog inclusion, revision history, etc.) is unaffected. New orders are placed against the new part number's DB record; for safety the old part number can have a trigger that prevent new orders from being placed.
>
> Since the part number is NOT the primary key, duplicate part numbers are not a problem. If you had instead used the part number as the primary key, you'd be dead in the water.

You are redefining the primary key to be (part_number,
obsoletion_date). Now, if you had not anticipated that in the
original design (likely enough), you do have to refactor queries that
join on the table...so what? If that's too much work, you can use a
view to take care of the problem (which may be a good idea anyways).
*you have to refactor the system anyways because you are now allowing
duplicate part numbers where previously (from the perspective of the
user), they were unique *.

The hidden advantage of pushing the full key through the database is
it tends to expose holes in the application/business logic. Chances
are some query is not properly distinguishing obsoleted parts and now
the real problems come...surrogate keys do not remove complexity, they
simply sweep it under the rug.

merlin

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Steven Flatt 2007-04-18 15:07:08 Foreign Key Deadlocking
Previous Message Greg Smith 2007-04-18 12:19:55 Re: Basic Q on superfluous primary keys