Thursday, October 30, 2008

Identity vs uniqueidentifier

If you are a developer working with SQL server, you have used Identity columns more than once. With SQL 7 Microsoft introduced a new data type called “uniqueidentifier”. From a top level the usage of both are the same.

But there are differences as well…

Identity column contains numbers where uniqueidentifier contains a GUID.

If you use an identity column, when ever there is a new row added to a table which has an identity column, SQL server will assign the next identity number on the sequence (this will depend on the identity seed settings). You can easily retrieve this by using @@identity.

If you have a uniqueidentifier column in your table, system won’t automatically create a value to that column. You have to use “NEWID()” SQL function to create a new GUID and then assign it to the column which is uniqueidentifier type.

uniqueidentifier type is good since GUID’s are unique in space and time. So even if you merge two tables with uniqueidentifier columns, there won’t be any duplicate uniqueidentifier’s. But only constrain with uniqueidentifier is the size (16 bit).

Software

0 comments: