Database Design – External ID for Every Relational Table

databasedatabase-designrelational-database

Should I add an external id for every table?

For example if I have these tables :

Customer
=======================================
Id       Name      Gender
---------------------------------------
1        John      M
2        Doe       M
---------------------------------------

CustomerPurchase
==========================================================================
Id       ExternalId      CustomerId            TotalQty
--------------------------------------------------------------------------
1        PO0313-0001     1                     10
--------------------------------------------------------------------------

In that example, I understand that ExternalId is required in CustomerPurchase table (for printing, etc).

But I don't see any reason why Customer table should need one as well.

FYI, the reason I asked this because a friend of mine said that SAP uses ExternalIds in their tables.

Should I use ExternalId in every table?

Am I missing any ExternalId's other usage besides for user's eye friendlier identification?

Thanks!

Best Answer

In layman's words:

Use surrogate primary keys as primary keys when:

  1. There are no natural or business keys
  2. Natural or business keys are bad ( change often )
  3. The value of natural or business key is not known at the time of inserting record
  4. Multicolumn natural keys ( usually several FK ) exceed three columns, which makes joins too verbose.

If a natural key exists that doesn't fall into the conditions listed above, for the sake of God, use it, especially if the key is an ISO standard or is issued by some respected institution, like country codes, airport or airline IATA codes, MAC addresses, car plaque numbers, IMDB movie codes, radio station call signs, etc. That would allow you to interoperate easier.

Above conditions mean surrogate keys will have to be used in many tables. But not all.

Related Topic