DECIMAL数据类型的范围大于INTEGER数据类型和BIGINT。众所周知,BIGINT可以存储18446744073709551615,而在DECIMAL中则可以存储DECIMAL(65,0),其中x代表65个9(9)。DECIMAL以字节为单位存储数字,并且存储要求公式为:DECIMAL(x,0)即
StorageRequirementInBytes = (x/9) + remaining; WHERE remaining = round_up( (x%9) / 2 )
DECIMAL(65,0)存储如下-
99999999999999999999999999999999999999999999999999999999999999999
要了解我们上面讨论的内容,让我们创建一个表。创建表的查询如下-
mysql> create table DecimalDemo -> ( -> UserId DECIMAL(65,0) -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into DecimalDemo values(99999999999999999999999999999999999999999999999999999999999999999); mysql> insert into DecimalDemo values(99999999999999999999999999999999999999999999999999999999999999999.0);
使用select语句显示表中的所有记录。查询如下-
mysql> select *from DecimalDemo;
输出结果
+-------------------------------------------------------------------+ | UserId | +-------------------------------------------------------------------+ | 99999999999999999999999999999999999999999999999999999999999999999 | | 99999999999999999999999999999999999999999999999999999999999999999 | +-------------------------------------------------------------------+ 2 rows in set (0.00 sec)
如果您在创建表时尝试给出66,则会出现以下错误-
mysql> create table DecimalDemo1 -> ( -> UserId DECIMAL(66,0) -> ); ERROR 1426 (42000): Too-big precision 66 specified for 'UserId'. Maximum is 65.