SELECT COUNT(*) FROM table_name;
My algorithm is:
- check count
- count+1 is the new primary key starting point
- Then keep on incrementing before every insert operation
But what is this GUID? Does SQL Server provide something where it automatically generates and incremented primary key?
I’m not sure if you’re also asking about IDENTITY or not- but a GUID is a unique identifier that is (almost) guaranteed to be unique. It can be used on primary keys but isn’t recommended unless you’re doing offline work or planning on merging databases.
For example a “normal”, IDENTITY primary key is
which when merging with another database which looks like
will be tricky. You’ve got to re-key some columns, make sure that your FKs are in order, etc. Using GUIDs, the data looks like this, and is easy to merge:
Note that a GUID takes a lot more space than an INT, and because of this it’s recommended to use an INT as a primary key unless you absolutely need to.