SQL Server Interview Question #29

What is UNIQUEIDENTIFIER in SQL Server?

Data Types, Keys & Constraints Junior Beginner

Detailed Explanation

UNIQUEIDENTIFIER is SQL Server's data type for globally unique identifiers (GUIDs). It stores a 16-byte value.

GUIDs are useful when identifiers need to be generated independently across applications or distributed systems. They are also common with ASP.NET Core Identity and some domain models.

Random GUIDs used as clustered keys can contribute to page splits and index fragmentation. Sequential key strategies may be considered when appropriate for the workload.

Code Example

CREATE TABLE CaseReports
(
    Id UNIQUEIDENTIFIER NOT NULL
        CONSTRAINT DF_CaseReports_Id DEFAULT NEWID(),
    Description NVARCHAR(500) NULL,
    CONSTRAINT PK_CaseReports PRIMARY KEY (Id)
);