Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Decimal Type Out Of Range Error

Tags:

mysql

This code:

DROP DATABASE IF EXISTS `my_db`;
CREATE DATABASE `my_db`; 
CREATE TABLE `my_db`.`my_table` (
    `id` integer NOT NULL AUTO_INCREMENT, 
    `multiplier` decimal(18, 10) NOT NULL, 
    PRIMARY KEY (`id`)
) Engine=InnoDB;

INSERT INTO `my_db`.`my_table` (`multiplier`) VALUES (100000000.0);

Returns an error:

Error Code: 1264. Out of range value for column 'multiplier' at row 1

Why? There are only 9 digits before comma whereas the column should work until 18 digits - or am I missing something here? Thank you.

like image 398
agiap Avatar asked Oct 26 '15 14:10

agiap


1 Answers

decimal(18, 10) means 10 digits after decimal and 8 before decimal, not until 18 digits

https://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-characteristics.html

For example, a DECIMAL(18,9) column has nine digits on either side of the decimal point, so the integer part and the fractional part each require 4 bytes. A DECIMAL(20,6) column has fourteen integer digits and six fractional digits. The integer digits require four bytes for nine of the digits and 3 bytes for the remaining five digits. The six fractional digits require 3 bytes.

like image 172
Abhik Chakraborty Avatar answered Oct 12 '22 20:10

Abhik Chakraborty