What is the range of MySQL DECIMAL(x,0)?

MySQLMySQLi Database

The range of DECIMAL data type is more than the INTEGER data type and BIGINT. As we know the BIGINT can store 18446744073709551615 while in DECIMAL you can store DECIMAL(65,0) where x represents the 65 nines(9). The DECIMAL stores the number in bytes and the storage requirement formula is: DECIMAL(x,0) i.e.

StorageRequirementInBytes = (x/9) + remaining;
WHERE remaining = round_up( (x%9) / 2 )

The DECIMAL(65,0) storage is as follows −

99999999999999999999999999999999999999999999999999999999999999999

To understand what we discussed above, let us create a table. The query to create a table is as follows −

mysql> create table DecimalDemo
-> (
-> UserId DECIMAL(65,0)
-> );
Query OK, 0 rows affected (0.62 sec)

Now you can insert some records in the table using insert command. The query is as follows −

mysql> insert into DecimalDemo
values(99999999999999999999999999999999999999999999999999999999999999999);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DecimalDemo
values(99999999999999999999999999999999999999999999999999999999999999999.0);
Query OK, 1 row affected (0.28 sec)

Display all records from the table using a select statement. The query is as follows −

mysql> select *from DecimalDemo;

Output

+-------------------------------------------------------------------+
| UserId |
+-------------------------------------------------------------------+
| 99999999999999999999999999999999999999999999999999999999999999999 |
| 99999999999999999999999999999999999999999999999999999999999999999 |
+-------------------------------------------------------------------+
2 rows in set (0.00 sec)

If you try to give 66 at the time of creation of the table, you will get the following error −

mysql> create table DecimalDemo1
-> (
-> UserId DECIMAL(66,0)
-> );
ERROR 1426 (42000): Too-big precision 66 specified for 'UserId'. Maximum is 65.
raja
Published on 19-Mar-2019 07:00:41
Advertisements