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.
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.
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.
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.
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.
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.
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.
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.
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
DECIMALcolumns. - Assuming every
DECIMALindex improves performance without checking execution plans.
Summary
- Use
DECIMALfor 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
- How to use mysql JOIN without ON condition?
- How to use MySQLdb with Python and Django in OSX 10.6?
- How to use MySQLdb with Python and Django in OSX 10.6?
- how to use pymysql executemany insert many rows and get the ids of each rows
- How to use Spring Boot with MySQL database and JPA?
- How to use Spring managed Hibernate interceptors in Spring Boot?
- How to use the dumped data by mongodump?
- How to use the Kafka Connect JDBC to source PostgreSQL with multiple schemas that contain tables with the same name?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.