CREATE SEQUENCE (Create Sequence)

Last updated: 2026-11-04

CREATE SEQUENCE (Create Sequence)

CREATE SEQUENCE creates a standalone generator of integers, read with nextval(). A sequence controls its start value, increment, bounds, cache, and whether it cycles. Using nextval() as a column default is equivalent to a SERIAL or IDENTITY column, but a standalone sequence is more flexible: it can exist apart from any table, be shared by several tables, and be retuned on its own after a data repair.

Kindcommand
Example LanguageSQL

📝 Syntax

CREATE SEQUENCE [IF NOT EXISTS] <sequence name>
[AS <data type>]
[INCREMENT [BY] <step>]
[MINVALUE <min> | NO MINVALUE]
[MAXVALUE <max> | NO MAXVALUE]
[START [WITH] <start>]
[CACHE <cache size>]
[CYCLE | NO CYCLE]
[OWNED BY <table>.<column> | NONE]

⚙️ Parameters

INCREMENT BYHow much the value advances per call, 1 by default
START WITHThe first value handed out
MINVALUE / MAXVALUELower and upper bounds
CACHEHow many values are pre-fetched into the session
CYCLEWraps around to the minimum after the maximum
OWNED BYBinds the sequence to a column

Returns:No return value. Once it succeeds the sequence can be read with nextval().

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

DROP TABLE
DROP SEQUENCE
CREATE SEQUENCE
CREATE TABLE
INSERT 0 3
id | title 
---+-------
10 | First 
15 | Second
20 | Third

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QCan a sequence hand out the same value twice?
ANot for a monotonic sequence. Even after a rollback, a value already handed out is never issued again — which is exactly why sequences do not roll back.
QShould I use SERIAL or CREATE SEQUENCE?
ASERIAL/IDENTITY is enough for a plain auto-increment column; use a standalone sequence when the numbering must be shared across tables, needs a custom step, or must exist apart from any table.
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%

🙏 帮我们做得更好

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

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