Live data from Hacker News

You'll regret using natural keys

blog.ploeh.dk

61–70 of 568 posts

Re: You'll regret using natural keys

#61
post #18

Earlier quoted context omitted.

That’s quite the absolute statement to make without even one example.

Maybe? Their statement was that you should "not always add," as opposed to the much stronger "always not add."

At least the latter provided some examples.

Re: You'll regret using natural keys

#62
post #44

You think your surrogate key will save you? It will not. The world has an external reality that needs to be reflected in your database. If the unique identifier for your object — VIN, CUSIP, whatever — if it changes, the world will henceforth refer to it by both. You will need to track both. Adding a synthetic key only means you have to track all three. Plus you have to generate a meaningless number, which is actuall…

In the wise words of Patsy, "it's only a model". The real world is resistent to clean abstractions and abstractions are distressingly subject to change. What made your row unique today is quite likely to become non-unique in the days/months/years to come. Always use surrogate keys. Your future self will thank you.

And always kids, write code for one person, and one person only: your future self.

Re: You'll regret using natural keys

#64
post #61
post #18

Earlier quoted context omitted.

Maybe? Their statement was that you should "not always add," as opposed to the much stronger "always not add."

At least the latter provided some examples.

Here's a real world example if you run a script once every 5 minutes that launch sub-task you might be tempted to add a non natural auto-increment number that identify each occurence to create a link between the script and the subtasks.

However it will be way more painless to use a timestamp of the script starting point a natural key.

This way when shit happen you have a relevant and stable way to identify each occurrence. And timestamp is easily indexable and ordered.

Re: You'll regret using natural keys

#65
post #16
post #9

Feels like this could have used a few more solid examples up-front. I think another good example would be PlayStation Network using the natural key of "gamer tags" as the primary key to identify players would be a good example. Since this effectively locks players into having to keep a gamertag in order to uniquely identify them in the service -- instead of having a synthetic key that carries no meaning or data other…

yeah the way he described it it's like... well who would ever do that. but foreign keying to emails or usernames is much easier to "accidentally" do and is a classic source of long-term headaches.

As a great example: My steam account name is the email I was using in 2003. I have largely not used it since then. The email on the account has been updated, but the account name? Stuck.

Re: You'll regret using natural keys

#66
post #23
post #9

Feels like this could have used a few more solid examples up-front. I think another good example would be PlayStation Network using the natural key of "gamer tags" as the primary key to identify players would be a good example. Since this effectively locks players into having to keep a gamertag in order to uniquely identify them in the service -- instead of having a synthetic key that carries no meaning or data other…

Last I checked, Steam still has me logging in with my two decade old hotmail address as my account name. At least it's not something that shows publicly, I think.

Mine too, but with a British ISP that hasn't existed for fifteen years.

Re: You'll regret using natural keys

#67
Here in Czech republic, everyone has an id number (rodné číslo) but as a foreigner, I have multiple id numbers. Further complicating matters, I was given a rč with the wrong gender (the gender is encoded in the number) and my nationality was at some point incorrectly listed as "Ireland". I do wonder if the Czech government thinks I am just two completely different people.

Re: You'll regret using natural keys

#68
The title could probably be extended to 'You'll regret using natural keys as primary keys' and it would be right in some ways. Personally, I've come to the conclusion that it's probably best to use both surrogate and natural keys. Surrogates (IDENTITYs, UUIDs what-have-you) as PK from a technical perspective and natural key as 'PK' from a business/data modeling perspective.

Re: You'll regret using natural keys

#69
post #64
post #61

Earlier quoted context omitted.

At least the latter provided some examples.

Here's a real world example if you run a script once every 5 minutes that launch sub-task you might be tempted to add a non natural auto-increment number that identify each occurence to create a link between the script and the subtasks. However it will be way more painless to use a timestamp of the script starting point a natural key. This way when shit happen you have a relevant and stable way to identify each occur…

Fast-forward a couple years: now I have scripts that launch sub-tasks more than once a second.
Post reply on HN