Identity Column

SQL · Working with Tables

Theory

An identity column, also called auto-increment, is a column whose value the database generates for each new row, usually 1, 2, 3 and so on. You insert only the other columns and the engine fills in the next number, which makes it the most common way to create a surrogate primary key.

Syntax differs by engine: standard SQL, PostgreSQL and Oracle use GENERATED ALWAYS AS IDENTITY (or BY DEFAULT); SQL Server uses IDENTITY(1,1); MySQL uses AUTO_INCREMENT; SQLite uses INTEGER PRIMARY KEY; Snowflake uses AUTOINCREMENT or IDENTITY. Numbers are unique but not guaranteed gap-free: a rolled-back insert or a deleted row leaves a gap, and numbers are not reused. With GENERATED ALWAYS, you cannot supply your own value without a special override.

Why we use it

Coming up with unique ids by hand is error-prone, especially when many users insert at the same time. An identity column hands out unique, increasing numbers safely and automatically.

When to use it

Use an identity column for surrogate primary keys on most entity tables. Use a natural key instead when a stable business id already exists, and consider UUIDs when ids must be created in many systems without coordination.

How it works

  1. INSERT a row without the id
  2. Ask the identity generator for the next value
  3. Assign it to the id column
  4. Does the insert succeed? Yes: Row stored with the new id. No: Value is used up, leaving a gap.
  5. Next insert gets a higher number

Syntax

column INT GENERATED ALWAYS AS IDENTITY

Example

CREATE TABLE employees (
  id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name VARCHAR(100)
);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic