Read more

MySQL 5.6 slightly changes DECIMAL data type

Dominik Schöler
January 13, 2014Software engineer at makandra GmbH

About

A MySQL DECIMAL column is used when it is important to preserve exact precision. It takes two parameters, where precision is the total number of digits and scale the number of digits to the right of the decimal point. A DECIMAL(6,2) column may store numbers up to 9,999.99.

Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more Show archive.org snapshot

In Rails, a decimal column definition looks like this: t.decimal :amount, :precision => 6, :scale => 2.

Issue

MySQL prior to 5.6 stored leading zeros (0003.1) and +/- characters (+2.1) within the column. However, it would permit storing one extra digit in lieu of a + sign, meaning the example column above would store numbers up to 99,999.99.

Starting with 5.6, MySQL does not store leading zeros nor +/- signs any more and "the hacky extra digit" is not supported any more. See the attached link for details.

Solution

If you were relying on the extra digit, just give the column a higher precision like this (7 for 6):

ALTER TABLE your_table MODIFY COLUMN your_column DECIMAL(7,2);
Posted by Dominik Schöler to makandra dev (2014-01-13 11:50)