2
Should I set the primary key of my table as auto_increment
or define it as numerical, generating the key within my application?
Contextualization
I am developing a C# application and testing it in an Access database, then passing it to a Mysql database.
One problem that crossed my mind was:
1) With a key of the type auto_increment
, to know the primary key of the record I saved, I will have to go to the bank and get it after saving the data.
2) With a manual key (created by the application), I will not need to do step 1), as I will have already created the primary key within the application before saving the data.
"With an auto_increment key, to know the primary key of the record I saved, I will have to go to the database and get it after saving the data." - normally this value is returned by the DB in a safe manner, without significant "cost" when the application makes new insertion. In the case of 2 you don’t have the least control of not creating repeated keys if your more than one application runs, or if your application has more than one thread changing the DB.
– Bacco