Sorry, but I found that the answers I gave are related questions (you can check this and this ). I changed them a little ...
You will find many posts devoted to this problem, and each choice you make has its pros and cons. The arguments for them usually relate to the theory of relational databases and database performance.
On this issue, my point of view is very simple: surrogate primary keys ALWAYS work , and Natural keys CANNOT ALWAYS work these days , and this is for several reasons: the field is too short, rule changes, etc.
At this point, you guessed that I was mainly a member of the uniqueIdentifier / surrogate primary key team, and even if I appreciate and understand arguments like the ones presented here, I'm still looking for a case where a ānaturalā key is better than a surrogate .. .
In addition to this, one of the most important but always forgotten arguments in favor of this basic rule is related to code normalization and performance :
every time i create a table i have to lose time
- indicating its primary key and its physical characteristics (type, size)
- remembering these characteristics every time I want to access it in my code?
- explaining my choice of PK to other developers on the team?
My answer does not match all of these questions:
- I donāt have time to lose trying to determine the ābest natural primary keyā when the surrogate option gives me a bulletproof solution.
- I donāt want to remember that the main key of my table_whatever is a string 10 characters long when I write the code.
- I donāt want to waste time linking the length of the natural key: āOK, if you need 10, why don't you take 12 to be on the safe side ? This āsafe sideā argument annoys me very much: if you want to stay on the safe side, it means that you are really close to the unsafe side! Choose a surrogate: it is bulletproof!
So, I have been working for the last five years with a very simple rule: each table (let it be called myTable) has its first field called 'id_MyTable' , which has a unique identifier type. Even if this table maintains a many-to-many relationship, where the combination of fields offers a very acceptable Primary Key, I prefer to create this field 'id_myManyToManyTable' as a unique identifier, just stick to this rule, and therefore, finally, it does not hurt.
The main advantage is that you no longer have to worry about using the Primary Key and / or foreign key in your code. When you have a table name, you know the name and type of PK. Once you know what references are implemented in your data model, you will find out the name of the available foreign keys in the table.
And if you still want to have your "Natural Key" somewhere in your table, I advise you to build it using a standard model, for example
Tbl_whatever id_whatever, unique identifier, primary key code_whatever, whateverTypeYouWant(whateverLengthYouEstimateTheRightOne), indexed .....
If id_ is the prefix for the primary key, and code_ is used for the "natural" indexed field. Some argue that the code_ field should be set as unique. This is true, and it can be easily controlled either using DDL or using external code. Please note that many "natural" keys are calculated (account numbers), so they are already generated using the code
I'm not sure my rule is the best. But it is very effective! If everyone used this, we would, for example, avoid time wasting an answer to this question!