MySQL 5.6 slightly changes DECIMAL data type

Posted . Visible to the public.

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.

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);
Dominik Schöler
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2014-01-13 10:50)