{
    "slug": "mysql_numeric_types",
    "term": "MySQL Numeric Types",
    "category": "database",
    "difficulty": "beginner",
    "short": "INT, BIGINT, DECIMAL, FLOAT, DOUBLE — the right type prevents overflow, precision loss, and money calculation bugs.",
    "long": "Integer types: TINYINT (1B), SMALLINT (2B), MEDIUMINT (3B), INT (4B, ~2.1B signed), BIGINT (8B). UNSIGNED doubles the positive range. DECIMAL(p,s) stores exact decimal values using string-based arithmetic — use for money and prices. FLOAT (4B) and DOUBLE (8B) are IEEE 754 approximate values — never use for currency as 0.1 + 0.2 ≠ 0.3. Use DECIMAL(10,2) for prices, BIGINT for large counters, INT UNSIGNED for most IDs.",
    "aliases": [
        "MySQL INT BIGINT DECIMAL",
        "MySQL money type",
        "FLOAT vs DECIMAL MySQL",
        "MySQL numeric column"
    ],
    "tags": [
        "mysql",
        "database"
    ],
    "misconception": "FLOAT is fine for prices since the errors are tiny. In financial calculations, tiny floating-point errors accumulate — £0.001 per transaction × millions of rows causes real discrepancies.",
    "why_it_matters": "Using FLOAT for prices introduces rounding errors — £9.99 may be stored as 9.989999999. Overflowing an INT column silently wraps or errors — a high-volume table with INT id will eventually hit 2.1B rows.",
    "common_mistakes": [
        "Using FLOAT or DOUBLE for prices, totals, or tax calculations.",
        "Using signed INT when UNSIGNED would double the available range for no cost.",
        "Not switching to BIGINT before an INT column approaches its limit — the ALTER TABLE is expensive on large tables."
    ],
    "when_to_use": [
        "Use DECIMAL for any monetary or precise decimal value.",
        "Use BIGINT UNSIGNED for primary keys on tables expected to exceed hundreds of millions of rows."
    ],
    "avoid_when": [
        "Never use FLOAT or DOUBLE for financial values — use DECIMAL.",
        "Avoid signed integer types for IDs — UNSIGNED doubles the range at zero cost."
    ],
    "related": [
        "mysql_auto_increment",
        "mysql_string_types"
    ],
    "prerequisites": [
        "mysql_auto_increment"
    ],
    "refs": [
        "https://dev.mysql.com/doc/refman/8.0/en/numeric-types.html"
    ],
    "bad_code": "-- FLOAT for money: rounding errors accumulate\nprice FLOAT NOT NULL,\n-- INT for high-volume event log: overflows at ~2.1B rows\nid INT AUTO_INCREMENT PRIMARY KEY",
    "good_code": "CREATE TABLE products (\n    id        INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, -- up to 4.2B\n    price     DECIMAL(10,2) NOT NULL,                  -- exact: 99999999.99\n    quantity  MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,   -- up to 16.7M\n    weight_kg DECIMAL(8,3)                             -- exact decimal\n);\n\nCREATE TABLE events (\n    id         BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY -- up to 18.4 quintillion\n);",
    "quick_fix": "Use DECIMAL(10,2) for money, INT UNSIGNED for most IDs, BIGINT UNSIGNED for high-volume tables",
    "effort": "low",
    "created": "2026-03-31",
    "updated": "2026-03-31",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/mysql_numeric_types",
        "html_url": "https://codeclaritylab.com/glossary/mysql_numeric_types",
        "json_url": "https://codeclaritylab.com/glossary/mysql_numeric_types.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 Numeric Types](https://codeclaritylab.com/glossary/mysql_numeric_types) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/mysql_numeric_types"
            }
        }
    }
}