I've lately seen a database where there was a table Types
with columns Id
, Key
and Name
.
Id
was just an Id of the type, Key
was a short key name for the type, for example "beer", and the Name
was text that could be displayed for the user (for example, "Our greatest beers"). Id was of course unique and was a primary key for this table. Key was also unique. Other tables were always linked with table Types using its Id column, but stored procedures were always using Key for filtering (e.g. "X inner join Types on X.type_Id = Types.Id where Types.Key = 'beer' "
instead of "X.type_Id = 3"
).
I thought about it as a bad approach. I would use Id
rather than Key
, even if i knew Key
is unique. I think it would be possible (and ok) that the Key changes but the Id shouldn't change as it is used in another tables for linking. Are there any rules for not doing this? I mean if we changed Key
"beer" to "beers", some of the stored procedures would stop working properly (and there actually was such a situation). For me it was quite intuitive that if the Id
identifies the row in a table, we should always use the id, as the other attributes may change if needed and it should not cause problems. Am I right?