Re: Basic Q on superfluous primary keys - Mailing list pgsql-performance

From Merlin Moncure
Subject Re: Basic Q on superfluous primary keys
Date
Msg-id b42b73150704180744h28d99ebflebe4405b3af5e44d@mail.gmail.com
Whole thread Raw
In response to Re: Basic Q on superfluous primary keys  ("Craig A. James" <cjames@modgraph-usa.com>)
Responses Re: Basic Q on superfluous primary keys
List pgsql-performance
On 4/18/07, Craig A. James <cjames@modgraph-usa.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".
Everythingelse (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
safetythe 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
partnumber 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

pgsql-performance by date:

Previous
From: Greg Smith
Date:
Subject: Re: Basic Q on superfluous primary keys
Next
From: "Steven Flatt"
Date:
Subject: Foreign Key Deadlocking