This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
My team is having issues these days because one of them has been converted to Hibernate, including the view that primary keys can be arbitrary integers. That seems to violate all rules of normalization. Does your book comment on the issue of what makes a proper primary key?
Ken, This is a point of high contention. It is certainly safer to have a primary key as an arbitrary number. Some people use business values. With that approach, there is the risk that something may change. For example, a person's social security number seems unique. But what happens if there was a typo or mistake?
Originally posted by Jeanne Boyarsky: Ken, This is a point of high contention. It is certainly safer to have a primary key as an arbitrary number. Some people use business values. With that approach, there is the risk that something may change. For example, a person's social security number seems unique. But what happens if there was a typo or mistake?
It might be safer to use an arbitrary number but it makes programming much harder because you can't use your key as a meaningful search term any longer. In fact, I was really surprised to see anyone even suggest such a thing as an arbitrary key, as though all the research done in the 1980's and 1990's on relational database by people like Chris Date was not only not correct but not even engaged. It seems to me that before one maintains that arbitrary keys are best one would be well-advised to tell me why all the work that has gone on before doesn't matter.
This is a topic people feel very strongly about, and I'm not going to take sides on this one. I try to be agnostic about this in the book, but in practice I will confess that I tend to use artificial keys. I tend to agree with Jeanne (and even use a similar example in the book). When you're in the middle of trying to figure out why you even need a key at all, going into the great debate is a little off track.
Originally posted by Bear Bibeault: I'm no DB guru, but those who are have repeatedly told me that primary keys should have no semantic meaning. It's just an opaque identifier that carries no data of its own.
From a data modeling standpoint, using a surrogate key instead of a natural key removes the business requirements from your database key structure. If requirements change for the data related to the business, you do not have to change the key structure of your database. This decoupling can sometimes be a disadvantage from a user standpoint with ad-hoc queries.
The advantage of a natural key is that it allows the user with business knowledge to query the data without understanding surrogate key relationships.
From a SQL standpoint, it really makes little difference with respect to your query beyond performance of number keys joining to number keys.