liveBook · Manning

Decimal

As explained in Grokking Relational Database Design

A DECIMAL data type is an SQL fixed-point numeric type whose precision and scale control the represented number of digits.

DECIMAL is a fixed-point SQL numeric type for values where every decimal place matters, especially prices and money. Its two parameters control the total digits and the digits after the decimal point. section 1.10 section 4.7

A product price

In DECIMAL(5,2), the 5 allows five digits in total and the 2 reserves two digits to the right of the decimal point. A product price such as 29.99 fits this definition. section 1.10

Choose DECIMAL or NUMERIC when accuracy is required. FLOAT and DOUBLE use finite precision and can introduce rounding errors, which is risky for currency conversions and other financial values. section 4.7

Read the two DECIMAL parameters this way:

  • Precision is the total number of significant digits. section 4.7
  • Scale is the number of digits stored to the right of the decimal point. section 1.10 section 4.7
  • Use a fixed-point type when rounding could make a measurable difference. section 4.7

How Grokking Relational Database Design covers it

How deep it goes
Explains precision, scale, fixed-point accuracy, and floating-point rounding risks through database and currency examples.
Examples
Uses DECIMAL(5,2) for product prices and contrasts floating-point currency conversion with fixed-point accuracy.
What it assumes
Assumes basic CREATE TABLE and column definitions while defining precision and scale before using them.
What you can do after
Apply it via CREATE TABLE by specifying total and fractional digit limits for accurate numeric columns.
How it connects
Connects precision and scale with fixed-point types, floating-point types, rounding errors, table columns, and accuracy requirements.

Also explained in

Compare how 5 books explain Decimal →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Relational Database Design. The section links point to the parts of the book it draws on.