{
    "slug": "mysql_auto_increment",
    "term": "MySQL AUTO_INCREMENT",
    "category": "database",
    "difficulty": "beginner",
    "short": "A column attribute that automatically assigns sequential integer IDs on INSERT — the standard primary key pattern in MySQL.",
    "long": "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.",
    "aliases": [
        "auto increment MySQL",
        "AUTO_INCREMENT primary key",
        "generated ID MySQL"
    ],
    "tags": [
        "mysql",
        "database"
    ],
    "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."
    ],
    "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."
    ],
    "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."
    ],
    "related": [
        "pdo_lastinsertid",
        "db_transactions",
        "mysql_innodb_myisam"
    ],
    "prerequisites": [
        "mysql_innodb_myisam"
    ],
    "refs": [
        "https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html"
    ],
    "bad_code": "-- INT can overflow on high-volume tables (~2.1B rows with signed, ~4.2B unsigned)\nid INT AUTO_INCREMENT PRIMARY KEY\n-- If you expect >4.2B rows, use BIGINT UNSIGNED",
    "good_code": "CREATE TABLE users (\n    id    BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n    email VARCHAR(255) NOT NULL UNIQUE,\n    name  VARCHAR(100) NOT NULL\n) ENGINE=InnoDB;\n\n-- Get last inserted ID via PDO\n$pdo->prepare('INSERT INTO users (email, name) VALUES (?, ?)')\n    ->execute([$email, $name]);\n$userId = (int) $pdo->lastInsertId();",
    "quick_fix": "Use INT UNSIGNED AUTO_INCREMENT PRIMARY KEY for most tables, BIGINT UNSIGNED for high-volume tables",
    "effort": "low",
    "created": "2026-03-31",
    "updated": "2026-03-31",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/mysql_auto_increment",
        "html_url": "https://codeclaritylab.com/glossary/mysql_auto_increment",
        "json_url": "https://codeclaritylab.com/glossary/mysql_auto_increment.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[MySQL AUTO_INCREMENT](https://codeclaritylab.com/glossary/mysql_auto_increment) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/mysql_auto_increment"
            }
        }
    }
}