3 ms·
I go against the trend here: I start with natural with FoxPro, then I use surrogate keys for more than a decade (following dogma...) and revert to natural ones
by mamcx 5y ago
I go against the trend here: I start with natural with FoxPro, then I use surrogate keys for more than a decade (following dogma...) and revert to natural ones a few years ago.
To make it more fun, the data come from DOZENS of different ERPs sources, with massively divergent ideas about what is data (like: One of them store money in different STRINGS formats with different decimal separators... in the SAME column). Plus, normal shenigans like nulls inside chars, uncommon ideas about what is ascii, etc.
And I sync the data in near real time, and the app is used concurrently by many users... etc.
---
The major reason: A LOT easier to sync data. A LOT.
UUIDs is one of the biggest anti-patterns to use for this. It means NOTHING. It demand to correlate with the REAL thing.
It makes harder to locate stuff later.
And when sync, it looks like are 2 different records. Still need to correlate with the actual natural keys.
But, how about changes on the key? How about mistakes? The natural keys can and will change, and using UUIDs or Integers NOT CHANGE THAT FACT. So deal it upfront. Is not that hard.
The only real true major drawbacks is to alter the rest of the places where it could be. Hapily, DBs have something called triggers. It works fine.
--
What is far better than UUIds where natural keys can't fit is to compute a hash of the relevant rows and use that as ID. It also make wonders for sync logic.
--
The key here is that the relational model is made for modeling the facts of the "real world".
Embracing this make everything nicer: Is easier to correlate things ("this line of invoice already tell me the Invoice Sequence and the TaxId of the customer and the City code!") making easier to do reporting, to use the DB admins, etc.