MySQL
DECIMAL data type
database management
SQL
data precision

How to use MySQL DECIMAL?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

MySQL DECIMAL is the right choice for fixed-point values where precision matters, especially money and financial calculations. Unlike floating-point types, DECIMAL stores exact values according to a defined precision and scale. Choosing proper column definitions and query patterns is essential for reliable results.

Core Sections

Understand Precision and Scale

DECIMAL(p, s) means total digits equals p, and digits after the decimal point equals s. For example, DECIMAL(10, 2) can store up to eight digits before the decimal point and two after it.

sql
1CREATE TABLE invoices (
2    id BIGINT PRIMARY KEY,
3    subtotal DECIMAL(10, 2) NOT NULL,
4    tax_rate DECIMAL(5, 4) NOT NULL,
5    total DECIMAL(12, 2) NOT NULL
6);

Choosing too small a precision can cause truncation or insert errors depending on SQL mode.

Use DECIMAL for Currency, Not FLOAT

Floating-point math can produce representation artifacts such as 0.30000000000000004 in some contexts. For business rules, use DECIMAL end to end.

sql
1INSERT INTO invoices (id, subtotal, tax_rate, total)
2VALUES (1, 100.00, 0.0750, 107.50);
3
4SELECT subtotal * tax_rate AS tax_amount
5FROM invoices
6WHERE id = 1;

This approach keeps calculations deterministic for billing and reporting.

Pick Column Sizes from Real Domain Limits

Define precision based on actual business ranges, not guesses. If your maximum invoice value is under ten million with two decimals, DECIMAL(10,2) is fine. If values can exceed that, pick a larger precision early to avoid schema churn.

sql
ALTER TABLE invoices
MODIFY COLUMN total DECIMAL(15, 2) NOT NULL;

Schema changes on large tables can be expensive, so upfront planning helps.

Control Rounding Behavior Explicitly

MySQL may round results depending on function and context. In reporting queries, round intentionally to the scale your business rules require.

sql
1SELECT
2  id,
3  subtotal,
4  ROUND(subtotal * tax_rate, 2) AS tax_amount,
5  ROUND(subtotal + (subtotal * tax_rate), 2) AS computed_total
6FROM invoices;

Rounding only at the final stage and documenting rules keeps accounting behavior consistent across systems.

Application Layer Considerations

If your application language has decimal or big-number libraries, use them instead of binary floats when constructing SQL values. This prevents subtle conversion issues before data reaches MySQL.

python
1from decimal import Decimal
2
3subtotal = Decimal("100.00")
4tax_rate = Decimal("0.0750")
5print(subtotal * tax_rate)  # 7.500000

Using string literals with decimal constructors is usually safer than float literals.

Validate and Index Thoughtfully

DECIMAL columns can be indexed, but be intentional about indexing strategy. For monetary ranges, indexes help reporting filters. For exact match on high-cardinality values, test query plans to confirm index usefulness.

sql
CREATE INDEX idx_invoices_total ON invoices (total);
EXPLAIN SELECT * FROM invoices WHERE total BETWEEN 100.00 AND 500.00;

Data Migration and Backfill Considerations

If you are migrating from FLOAT to DECIMAL, run conversion in a controlled way and validate aggregates before cutover. A common workflow is adding a new decimal column, backfilling in batches, validating totals, then swapping columns.

sql
ALTER TABLE invoices ADD COLUMN total_decimal DECIMAL(12,2) NULL;
UPDATE invoices SET total_decimal = ROUND(total, 2);
SELECT SUM(total), SUM(total_decimal) FROM invoices;

After validation, switch application reads to the new column first, then writes, and only then remove the old column. This phased approach reduces risk and gives you a clear rollback point.

Common Pitfalls

  • Using floating-point columns for currency and seeing inconsistent arithmetic results.
  • Choosing precision too small for future business growth.
  • Relying on implicit rounding without defining explicit rules.
  • Mixing float math in application code before writing to DECIMAL columns.
  • Assuming every DECIMAL index improves performance without checking execution plans.

Summary

  • Use DECIMAL for exact fixed-point values such as money.
  • Select precision and scale based on real domain limits.
  • Round explicitly in queries according to business policy.
  • Keep decimal-safe arithmetic in both database and application layers.
  • Validate index decisions with actual query plans.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.