Guid in SQL Server
Text version of the video
All SQL Server Text Articles
All SQL Server Slides
All SQL Server Tutorial Videos
All Dot Net and SQL Server Tutorials in English
All Dot Net and SQL Server Tutorials in Arabic
In this video we will discuss
1. What is a GUID in SQL Server
2. When to use GUID
3. Advantages and disadvantages of using a GUID
What is GUID data type in SQL Server
The GUID data type is a 16 byte binary data type that is globally unique. GUID stands for Global Unique Identifier. The terms GUID and UNIQUEIDENTIFIER are used interchangeably.
To declare a GUID variable, we use the keyword UNIQUEIDENTIFIER
Declare @ID UNIQUEIDENTIFIER
SELECT @ID = NEWID()
SELECT @ID as MYGUID
How to create GUID in sql server
To create a GUID in SQL Server use NEWID() function
For example, SELECT NEWID(), creates a GUID that is guaranteed to be unique across tables, databases, and servers. Every time you execute SELECT NEWID() query, you get a GUID that is unique.
Example GUID : 0BB83607-00D7-4B2C-8695-32AD3812B6F4
When to use GUID data type : Let us understand when to use a GUID in SQL Server with an example.
For example code used in the demo please refer to the following article
The main advantage of using a GUID is that it is unique across tables, databases and servers. It is extremely useful if you’re consolidating records from multiple SQL Servers into a single table.
The main disadvantage of using a GUID as a key is that it is 16 bytes in size. It is one of the largest datatypes in SQL Server. An integer on the other hand is 4 bytes,
An Index built on a GUID is larger and slower than an index built on integer column. In addition a GUID is hard to read compared to int.
So in summary, use a GUID when you really need a globally unique identifier. In all other cases it is better to use an INT data type.