views:

99

answers:

7

Hello, I've been reading up on foreign keys and such for postgres and I noticed that it allows a cascading update for foreign keys.

Well, my question is, when would you need to update the primary key of a row?

Apparently this guy needs to http://www.oreillynet.com/onlamp/blog/2004/10/hey_sql_fans_check_out_foreign.html but I'm not quite understanding how it could ever be useful.

Edit: I see for natural primary keys, how this could be used. But what about technical primary keys? Ones that have no meaning and are almost always auto generated on insert?

+1  A: 

You would need to do it if you chose your primary key as a natural key instead of a surrogate key, and then later found out that the user changed their surname, or that they wrote their SSN incorrectly on the application form.

Moral of story: don't use natural keys as primary keys.

Mark Byers
I use nothing but `serial`(auto-incremented unique number) for primary keys because using anything else is basically banned where I work(and for good reason)
Earlz
What about a table that describes a many to many relationship where the primary key is a combo of the two foreign keys it relates? Surely then you don't need the superfluous serial primary key?
Mike Sherov
I'm not too wise on how the whole multi-column primary key bit works..
Earlz
@Mike Sherov: +1 You are correct: you don't need to have superfluous column just to create a primary key for no good reason. If you already have a **suitable** candidate key (i.e. not a natural key), you can and should use that as the primary key. Note that if you are referencing a table with a two column foreign key, there may be a performance benefit in creating a redundant column. Another time a superfluous column is not necessary is if you are modelling a set (i.e a table consisting of only one column where the important detail is whether or not an item is in the set).
Mark Byers
Hey Mark, always good to see your answers. The important thing I was trying to communicate to @Earlz (I suppose my rhetorical question should've been an answer instead) was what you just said. Don't use natural keys as primary keys, but serial keys aren't always the right answer either.... I'd say, always seek out a suitable candidate key first (and you usually can find a good one when your table is describing a relationship between more than one of the other tables in the DB).
Mike Sherov
I don't see a problem with natural keys provided they won't change in the domain of your application. Surnames are bad choice. Social security numbers can change and probably shouldn't be used anyway for privacy reasons. ISBN and UPC/EAN codes should be fine (manufacturers who reuse them on similar products notwithstanding) as their only reason to change is to add more digits. I may be wrong, but I doubt that Amazon would have a synthetic key in addition to the (user-visible) ASIN on every product.
Duncan
A: 

You might need to do this if using a natural primary key (one that has an actual meaning in the problem domain). If the meaning changed, then you'd need to cascade the change.

I supposed a bad example of this would be a database of buildings on a school campus, with the building name as a primary key (don't do this at home). If the building is renamed to bribe honor a new donor, then the key would need to change.

John Saunders
+1  A: 

Well... we have a lot of primary keys that are defined as a human readable code. Terrible idea, but not much choice in the matter.

It is very very handy to be able to fix that PK, and all dependent records, when someone realizes it is misspelled, or the meaning has changed.

Andrew Backer
+1  A: 

I had to change my PK several times, when exposing my PK to a third party system. From time to time they called us asking to change the PKs, to fit the records in their database (from time to time due to tech problem, the synchronization between there two systems - fails).

After several times we just stopped exposing the PK and add a new column.

anthares
+1  A: 

For a synthetic, meaningless primary key like an autoincrementing column there should (with a few exceptions) never be any reason to update the PK value. If the PK is a user-visible value you might have to update it (which is one of the many arguments in favour of synthetic keys). An example of this situation is an insurance policy number. In some cases the year is a part of the number, and may tick over on every renewal. In some data models the record is just updated in situ.

Where this happens you would be better off to use a synthetic key, so that other items are not dependent on the visible number.

One possible scenario where you would need to update a synthetic key is if you were merging two or more application databases together. In this case you may need to shift keys en masse to avoid collissions with the keys of records from the other source.

ConcernedOfTunbridgeWells
+1  A: 

You may get in this situation if you use natural primary key.

Here's one very fresh example: in Croatia government changed tax identification numbers for both companies and individuals. New law was introduced with January 1st 2010.

Last year, I was consultant in several projects where companies were changing natural key (old tax number) to surrogate key in existing applications. Natural key seemed logical selection to original designers of those apps because it was defined by law. And then it changed.

zendar
A: 

For autogenerated keys, one example that I came across here (can't remember question) is if you need to merge two database tables together. In this case, you'll likely have duplicates unless your keys happened to be offset enough.

Dana the Sane