What is “better” generating Primary Keys on the database or generating them in application code, specifically when using GUID/UniqueIdentifier datatype for the keys.
I have read up on the difference between using Guid’s and int data types, and it sounds like Guids are feasable for so called “generating offline”.
E.g.
instead of having a NEWID() contstraint in the database, In one project (where we are using Entity Framework) we use in the application code Guid.NewGuid() to generate the PK when inserting data.
Is this a bad approach ?
My concerns are:
- Database indexes: Database performance because Id’s might not be sequential
- The one in 64 billion chance that the key is already used. (considering that the application will not be enormous but may need room to grow)
- Perhaps there are other disadvantages ?
well actually, GUID could be sequential from SQL Server 2005. There is a function in named
NEWSEQUENTIALID(), link here