Re: Alternative to serial primary key - Mailing list pgsql-sql

From Aaron Bono
Subject Re: Alternative to serial primary key
Date
Msg-id bf05e51c0607061443t2eb0cbbfl569b7b25c74166d7@mail.gmail.com
Whole thread Raw
In response to Re: Alternative to serial primary key  ("David Clarke" <pigwin32@gmail.com>)
Responses Re: Alternative to serial primary key  (Scott Marlowe <smarlowe@g2switchworks.com>)
List pgsql-sql
On 7/6/06, David Clarke <pigwin32@gmail.com> wrote:

To recap, yes there is only a single column, yes it is varchar. I need
to do a lookup on the address column which is unique and use it as a
foreign key in other tables. Using a serial id would obviously work
and has been recommended. But having a hash function over the address
column as the primary key means I can always regenerate my primary key
from the data which is impossible with a serial key. I believe the
risk of collision using md5 is effectively zero on this data and I can
put a unique index over it.

So if you have:

addresses
    address_id  bigserial (pk),
    address

person
    person_id bigserial (pk),
    first_name,
    last_name,
    address_id

you can do something like

INSERT INTO person (
    address_id
)
SELECT
   'Joe',
   'Blow',
   address_id
FROM addresses
WHERE addresses.address = ?;

No regeneration of PK necessary.  If you index addresses.address the insert should run quickly, right?

-Aaron Bono

pgsql-sql by date:

Previous
From: "D'Arcy J.M. Cain"
Date:
Subject: Re: Alternative to serial primary key
Next
From: "Sander Steffann"
Date:
Subject: Re: Alternative to serial primary key