Sunday, October 19, 2008

Why is Guid Sometimes better than Identity in Database

When the application demands that the just inserted value should be returned back.In such cases it is much wise to use GUID instead of auto- incrementing identity column in the database.

One benifit of using GUID is that it can be generated programatically beforehand using the GUID class in .net frmework

We can have Guid guid  = new Guid();

Now we can pass this newly generated guid to any no of insert statements this way we can save our application from the complex logic of using output parameters or return statement and also from the use of SCOPE_IDENTITY().

But you are free to use the other approach also it all depends on the need of the application

2 comments :

Anonymous said...

this will make the sql server slower.

Aashish said...

@Anonymous
"this will make the sql server slower."

Ya to an extent as i have mentioned it all depends on the application demands, if you are having simple application with no SCOPE_IDENTITY() calls and there is no need of long GUIDs then you can continue with you identity element but most enterprise level applications use Guids at many places.

Post a Comment