← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

MySQL AUTO_INCREMENT

database Beginner

Also Known As

auto increment MySQL AUTO_INCREMENT primary key generated ID MySQL

TL;DR

A column attribute that automatically assigns sequential integer IDs on INSERT — the standard primary key pattern in MySQL.

Explanation

AUTO_INCREMENT columns must be an integer type and part of a key (usually PRIMARY KEY). The counter increments even on rolled-back transactions — gaps in the sequence are normal and expected. The current counter value is stored in InnoDB's in-memory state and resets on server restart for pre-8.0 MySQL (fixed in 8.0 via redo log persistence). Use UNSIGNED INT (max ~4.2B) or BIGINT UNSIGNED (max ~18.4 quintillion) depending on expected row count.

Watch Out

In MySQL < 8.0, the AUTO_INCREMENT counter resets on server restart if the maximum ID row was deleted — can reuse IDs. Fixed in MySQL 8.0.

Common Misconception

AUTO_INCREMENT IDs are always sequential with no gaps. Rolled-back transactions, failed inserts, and server restarts all create gaps — sequence gaps are normal and cannot be relied upon.

Why It Matters

AUTO_INCREMENT is the simplest primary key strategy. Gaps in sequences are harmless — do not write application logic that assumes sequential, gap-free IDs.

Common Mistakes

  • Using INT (signed) instead of INT UNSIGNED — halves the available range for no benefit.
  • Writing code that assumes the last inserted ID equals max(id) — concurrent inserts break this assumption.
  • Not switching to BIGINT when an INT column approaches its limit — requires ALTER TABLE which locks the table.

Avoid When

  • Do not write application logic that assumes sequential, gap-free IDs — rolled-back transactions create gaps.
  • Do not use AUTO_INCREMENT when you need a globally unique ID across distributed systems — use UUID instead.

When To Use

  • Use INT UNSIGNED AUTO_INCREMENT for tables expected to stay under 4.2 billion rows.
  • Use BIGINT UNSIGNED for high-volume tables like events, logs, or analytics.

Code Examples

✗ Vulnerable
-- INT can overflow on high-volume tables (~2.1B rows with signed, ~4.2B unsigned)
id INT AUTO_INCREMENT PRIMARY KEY
-- If you expect >4.2B rows, use BIGINT UNSIGNED
✓ Fixed
CREATE TABLE users (
    id    BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    email VARCHAR(255) NOT NULL UNIQUE,
    name  VARCHAR(100) NOT NULL
) ENGINE=InnoDB;

-- Get last inserted ID via PDO
$pdo->prepare('INSERT INTO users (email, name) VALUES (?, ?)')
    ->execute([$email, $name]);
$userId = (int) $pdo->lastInsertId();

Added 31 Mar 2026
Views 12
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Google 4 Unknown AI 3 Perplexity 1 Ahrefs 1
crawler 7 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
⚙ Fix effort: Low
⚡ Quick Fix
Use INT UNSIGNED AUTO_INCREMENT PRIMARY KEY for most tables, BIGINT UNSIGNED for high-volume tables
📦 Applies To
web cli
🔗 Prerequisites
🔍 Detection Hints
id INT AUTO_INCREMENT (signed) on high-volume tables
Auto-detectable: ✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Low Context: Line

✓ schema.org compliant