Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: An improved technique for database primary keys

by Jenda (Abbot)
on Nov 05, 2010 at 18:52 UTC ( [id://869729]=note: print w/replies, xml ) Need Help??


in reply to Re: An improved technique for database primary keys
in thread An alternate technique for database primary keys

Multicolumn primary keys are ... a major pita as soon as you need to do any joins. I had to write some queries for a schema that used them and it was horrible and error prone. It was way too easy to forget one of the three columns in the sixth join. Plus of course having to repeat all those columns in all those related tables is wasteful.

If you want to ensure something is unique, use a unique index/constraint, but please do not ever even think of using a multicolumn PK!

Update: I forgot to add ... unless the table is basically just implementation of an N-N relation. With or without additional columns. In that case you are unlikely to need to join using both columns at the same time (see ... you can't assume you'll always include the whole PK in all joins) and the surrogate ID would (almost) never be used.

Jenda
Enoch was right!
Enjoy the last years of Rome.

  • Comment on Re^2: An improved technique for database primary keys

Replies are listed 'Best First'.
Re^3: An improved technique for database primary keys
by herveus (Prior) on Nov 08, 2010 at 16:01 UTC
    Howdy!

    It's a tradeoff. If you use surrogate keys, you always have to join to get the actual key values. Multicolumn PKs are natural. Surrogate keys obfuscate the true structure. Foreign key constraints are essential to document the key structures and to enforce referential integrity. They also help query building tools get the joins right.

    yours,
    Michael

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://869729]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found