// tables reference

SQL ALTER SEQUENCE

Retunes a running counter: new step size, new bounds, or a rewind behind the current value. MySQL and SQLite have no sequences, so their reset paths are table-level.

Categoria
Tabelas
In this category
16 of 16

Sintaxe

ALTER SEQUENCE [IF EXISTS] name [AS type] [INCREMENT BY n] [RESTART [WITH n]]  -- PostgreSQL: plus MINVALUE, MAXVALUE, CYCLE, CACHE, OWNED BY
ALTER SEQUENCE name RESTART WITH 1  -- SQL Server: parameters only, the datatype never changes
ALTER SEQUENCE name RESTART START WITH 500  -- Oracle 18c+: RESTART alone rewinds to MINVALUE
-- MySQL: no sequence to alter; ALTER TABLE t AUTO_INCREMENT = 500 sets the counter
-- SQLite: no sequence; AUTOINCREMENT counters sit in the sqlite_sequence table

Each block is a separate form. The support table and the examples show which engine takes which.

Suporte a mecanismos

MotorSupportNotas
SQL Server Sim RESTART [WITH n], INCREMENT BY, MINVALUE, MAXVALUE, CYCLE, CACHE; the datatype is not alterable
PostgreSQL Sim the full set including AS type and OWNED BY table.column or OWNED BY NONE
MySQL No equivalent nothing to alter: the table option AUTO_INCREMENT = n sets the next value
SQLite No equivalent no sequences; the AUTOINCREMENT counter is one row in the sqlite_sequence table, holding the last id used and written on the table's first insert
Oracle Different syntax INCREMENT BY and bounds always worked; RESTART needs 18c or newer

Exemplos

PostgreSQL

ALTER SEQUENCE order_id_seq RESTART WITH 5000;
-- detach a serial's sequence so the table drop leaves it alone:
ALTER SEQUENCE order_id_seq OWNED BY NONE;

SQL Server

ALTER SEQUENCE dbo.invoice_number RESTART WITH 1000 INCREMENT BY 10;

Oracle

-- 18c+: rewind to MINVALUE, or to a chosen value
ALTER SEQUENCE invoice_number RESTART;
ALTER SEQUENCE invoice_number RESTART START WITH 5000;

MySQL

-- no sequences: the table option is the reset
ALTER TABLE invoices AUTO_INCREMENT = 5000;

SQLite

-- counter rows live in sqlite_sequence, one per AUTOINCREMENT table; seq is the last id used, so the next insert gets 5000
UPDATE sqlite_sequence SET seq = 4999 WHERE name = 'invoices';

Bom saber

  • RESTART changes the future, not the past: rows already written keep their ids. Set the restart point past MAX(id) or an insert collides with an existing row as soon as the counter reaches it, and a unique or primary key rejects it.
  • Oracle before 18c had no RESTART at all: the moves were drop-and-recreate, which invalidates every stored object using the sequence, or the increment-swing trick (set INCREMENT BY negative, pull one NEXTVAL, set it back). 18c's RESTART START WITH ends both rituals.
  • PostgreSQL's OWNED BY table.column makes the sequence drop with its column, which is the linkage serial builds for you; OWNED BY NONE cuts it loose. A bare CREATE SEQUENCE leaves an orphan behind whenever the table goes.
  • SQL Server fixes the datatype at creation: ALTER SEQUENCE retunes the running behaviour but never the AS type, so an INT sequence heading past 2.1 billion is a drop-and-recreate job with dependency juggling.

Questions about ALTER SEQUENCE

How do I reset a sequence to match the table's max id?

PostgreSQL: SELECT setval('order_id_seq', COALESCE((SELECT MAX(id) FROM orders), 0) + 1, false), or ALTER SEQUENCE ... RESTART WITH n. Oracle 18c: RESTART START WITH n. MySQL: ALTER TABLE invoices AUTO_INCREMENT = n, which InnoDB quietly raises to MAX(id) + 1 if n is lower. On PostgreSQL, SQL Server and Oracle, check MAX(id) again afterwards: a restart at or below an existing id makes inserts collide once the counter reaches it.

What does OWNED BY actually change?

Lifecycle, not values: an owned sequence drops automatically when its column or table drops, while a bare sequence survives as an orphan. serial and GENERATED AS IDENTITY set the ownership up for you; the number stream is identical either way.

Can I change a sequence's datatype?

PostgreSQL yes: ALTER SEQUENCE ... AS bigint (10+) revalidates the bounds. SQL Server no: the type is fixed at creation, so a cramped INT sequence means recreate-and-repoint. Oracle sequences have no type clause at all; values are NUMBER.

Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.

// dialect-aware editing

DDL that matches the engine you're connected to

SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.