SERIAL (auto-increment pseudo-type)

Last updated: 2026-11-04

SERIAL (auto-increment pseudo-type)

SERIAL is not a real storage type but a PostgreSQL pseudo-type: writing SERIAL is shorthand for creating an INTEGER column, a standalone SEQUENCE and a DEFAULT nextval(...) clause pointing at that sequence. When the column is omitted on insert, the sequence hands out the next value. The construct predates standard identity columns, so SQL-standard IDENTITY is usually the better choice today, yet SERIAL remains the most common form in existing code.

Kindtype
Example LanguageSQL

📝 Syntax

SERIAL  |  SERIAL4
--  equivalent to:
--  id integer NOT NULL DEFAULT nextval('t_serial_id_seq')
--  plus an automatically created sequence named t_serial_id_seq

Returns:A SERIAL column is physically an INTEGER whose values come from the backing sequence, which starts at 1 and increments by 1 unless changed with ALTER SEQUENCE.

▶ 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:

 id | name  | data_type |            column_default
----+-------+-----------+--------------------------------------
  1 | Alice | integer   | nextval('t_serial_id_seq'::regclass)
  2 | Bob   | integer   | nextval('t_serial_id_seq'::regclass)

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QSERIAL or IDENTITY?
APrefer IDENTITY for new tables: it is SQL-standard, binds the sequence to the column and lets you switch policy with SET GENERATED. SERIAL's main remaining advantage is broader tooling compatibility, plus the fact that its sequence is a standalone object you can nextval() directly.
QHow do I reset a SERIAL column to 1?
ARun ALTER SEQUENCE t_serial_id_seq RESTART WITH 1;, or empty the table and reset the sequence together with TRUNCATE t_serial RESTART IDENTITY.
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%

🙏 帮我们做得更好

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

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