I want to know if I can use human readable primary keys for a relatively small number of database objects, which will describe large metropolitan areas.
For example, using “washington_dc” as the pk for the Washington, DC metro area, or “nyc” for the New York City one.
Tons of objects will be foreign keyed to these metro area objects, and I’d like to be able to tell where a person or business is located just by looking at their database record.
I’m just worried because my gut tells me this might be a serious crime against good practices.
So, am I “allowed” to do this kind of thing?
Thanks!
It all depends on the application – natural primary keys make a good deal of sense on the surface, since they are human readable and don’t require any joins when displaying data to end users.
However, natural primary keys tend to be larger than
INT(or evenBIGINT) suragate primary keys and there are very few domains where there isn’t some danger of having a natural primary key change. To take your example, a city changing its name is not a terribly uncommon occurrence. When a city’s name changes you are then left with either an update that needs to touch every instance ofcityas a foreign key or with a primary key that no longer reflects reality (“The data shows Leningrad, but it really is St. Petersburg.”)So in sum, natural primary keys:
Whether #1 and #2 are sufficiently counteracted by #3 depends on what you are building and what its use is.