Ira SQL ProIra SQL Pro

SERIAL and BIGSERIAL

beginnerpostgresql
2 min read

What are SERIAL and BIGSERIAL?

SERIAL and BIGSERIAL are shorthand notations for creating an auto-incrementing integer column backed by a sequence. SERIAL uses a 4-byte integer; BIGSERIAL uses an 8-byte integer.

Syntax

CREATE TABLE students (
  id SERIAL PRIMARY KEY,
  first_name VARCHAR(50)
);

CREATE TABLE orders (
  id BIGSERIAL PRIMARY KEY,
  order_date DATE
);

Modern alternative (SQL standard):

CREATE TABLE students (
  id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  first_name VARCHAR(50)
);

When to Use

  • Auto-incrementing primary keys for most tables
  • SERIAL for tables with fewer than ~2 billion rows
  • BIGSERIAL for very large tables or high-throughput systems
  • IDENTITY for new projects (SQL standard)

Key Points

  1. Auto Sequence — SERIAL creates a sequence named tablename_colname_seq automatically.
  2. Not Truly Auto-Increment — It is syntactic sugar for: INTEGER NOT NULL DEFAULT nextval('seq').
  3. Gaps — Sequence values can have gaps (e.g., after failed inserts or rollbacks).
  4. NOT NULL — SERIAL columns are implicitly NOT NULL.
  5. IDENTITY Preferred — PostgreSQL 10+ supports GENERATED ALWAYS AS IDENTITY, which is the SQL standard.

Guided Practice

Solve the challenge below. Use hints when stuck and check your answer for instant feedback.

Practice challengeGuided learning mode

SERIAL and BIGSERIAL Challenge

Write a query that solve this task: auto-incrementing ID for a students table.

Expected result

A students table where id auto-increments starting from 1.

Hidden checks

  • Returned rows and values
  • Output columns and result shape
  • Final database state after the query runs

Lesson guidance

What are SERIAL and BIGSERIAL?

Initializing database...Each run starts from fresh sample data.

More Examples

Insert without specifying the ID

Let PostgreSQL assign the next ID automatically.

Initializing database...Each run starts from fresh sample data.

IDENTITY column (modern approach)

Use the SQL-standard IDENTITY instead of SERIAL.

Initializing database...Each run starts from fresh sample data.

Frequently Asked Questions

What is the difference between SERIAL and BIGSERIAL?
SERIAL uses a 4-byte integer (max ~2.1 billion). BIGSERIAL uses an 8-byte integer (max ~9.2 quintillion). Use BIGSERIAL for tables expected to grow very large.
Should I use SERIAL or IDENTITY?
For new projects, prefer GENERATED AS IDENTITY (SQL standard). SERIAL is still widely used and works fine, but IDENTITY is the modern approach.

Related Topics