IDENTITY Column

Last updated: 2026-11-04

IDENTITY Column

IDENTITY is the SQL-standard way to declare an auto-incrementing column. Unlike SERIAL, which attaches a separate sequence and uses nextval as a column default, IDENTITY binds the column and its sequence as one standard object. ALWAYS forbids manual values while BY DEFAULT permits them, and both are understood more reliably by tooling than SERIAL.

Kindconstraint
Example LanguageSQL

📝 Syntax

`column_name type GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY`
Options: `START WITH n` / `INCREMENT BY n`
Explicit write: `INSERT INTO t (id, ...) OVERRIDING SYSTEM VALUE VALUES (...)`

⚙️ Parameters

ALWAYS / BY DEFAULTALWAYS rejects any explicit value (unless OVERRIDING SYSTEM VALUE is used); BY DEFAULT allows explicit values, generating one only when the column is omitted.
START WITH / INCREMENT BYThe sequence's starting value and step, with the same semantics as CREATE SEQUENCE.

Returns:IDENTITY has no return value of its own. It is bound to an internal sequence whose name pg_get_serial_sequence() can reveal.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

SELECT 'BY DEFAULT -> auto ids' AS step,
       (SELECT string_agg(ticket_id::TEXT, ', ' ORDER BY ticket_id) FROM tickets) AS result
UNION ALL SELECT 'ALWAYS -> explicit id rejected',
       (SELECT pg_try('INSERT INTO strict_tickets (ticket_id, title) VALUES (100, ''Manual'')'))
UNION ALL SELECT 'ALWAYS -> OVERRIDING SYSTEM VALUE',
       (SELECT pg_try('INSERT INTO strict_tickets (ticket_id, title) OVERRIDING SYSTEM VALUE VALUES (100, ''Manual'')'))
UNION ALL SELECT 'backing sequence',
       (SELECT pg_get_serial_sequence('tickets','ticket_id'))
 step                              | result
-----------------------------------+-----------------------------------------------------------
 BY DEFAULT -> auto ids            | 1, 2
 ALWAYS -> explicit id rejected    | cannot insert a non-DEFAULT value into column "ticket_id"
 ALWAYS -> OVERRIDING SYSTEM VALUE | OK
 backing sequence                  | public.tickets_ticket_id_seq
(4 rows)

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QShould I use IDENTITY or SERIAL?
APrefer IDENTITY for new work: it is standard, models the column-as-identity idea directly, and needs no special handling when serializing foreign keys (PostgreSQL 10+). Existing SERIAL columns need no forced migration and behave identically.
QHow do I choose between BY DEFAULT and ALWAYS?
ABY DEFAULT permits explicit IDs, which suits bulk imports that must preserve original identifiers. ALWAYS insists the database owns identity, which fits strict append-only ledgers where outside interference is not wanted.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏