SQL Server Interview Question #17

What is IDENTITY in SQL Server?

SQL Server Fundamentals Junior Beginner

Detailed Explanation

IDENTITY is a SQL Server column property that automatically generates numeric values, commonly for surrogate primary keys.

IDENTITY(1,1) means the seed starts at 1 and the increment is 1. Identity values should not be assumed to be gap-free because deletes, rollbacks, failed inserts, caching, and other events can leave gaps.

Code Example

CREATE TABLE Products
(
    Id INT IDENTITY(1,1) PRIMARY KEY,
    Name NVARCHAR(200) NOT NULL
);

INSERT INTO Products (Name)
VALUES ('Laptop');