与 Mysql 中的 bigint 数据类型相关
related to the bigint datatype in Mysql
声明 x bigint;
设置 x = 180181182149150151152;
错误:第 1 行第 'x' 列的值超出范围。
所以我的问题是,我可以在 bigint 中存储一个大数字吗?
这是 BIGINT UNSIGNED
支持的最大值,与您使用的数字相比:
180181182149150151152 = your number
18446744073709551615 = 2^64-1, largest value for BIGINT UNSIGNED
您可以看到您的数字大约是 MySQL 支持的最大整数值的 10 倍。您的号码需要 68 位来存储,但 MySQL 仅支持 integer data types up to 64-bit.
您可以在 FLOAT
或 DOUBLE
中存储更大数量级的值,但会失去精度。换句话说,你根本无法将 68 位信息塞入 64 位编码。
mysql> create procedure p() begin declare x double; set x = 180181182149150151152; select x; end;;
mysql> call p;;
+-----------------------+
| x |
+-----------------------+
| 1.8018118214915015e20 |
+-----------------------+
如果你给它足够的精度,你可以使用DECIMAL()
:
mysql> create procedure p() begin declare x decimal(64,0); set x = 180181182149150151152; select x; end;;
mysql> call p;;
+-----------------------+
| x |
+-----------------------+
| 180181182149150151152 |
+-----------------------+
阅读更多:https://dev.mysql.com/doc/refman/8.0/en/fixed-point-types.html
声明 x bigint; 设置 x = 180181182149150151152;
错误:第 1 行第 'x' 列的值超出范围。
所以我的问题是,我可以在 bigint 中存储一个大数字吗?
这是 BIGINT UNSIGNED
支持的最大值,与您使用的数字相比:
180181182149150151152 = your number
18446744073709551615 = 2^64-1, largest value for BIGINT UNSIGNED
您可以看到您的数字大约是 MySQL 支持的最大整数值的 10 倍。您的号码需要 68 位来存储,但 MySQL 仅支持 integer data types up to 64-bit.
您可以在 FLOAT
或 DOUBLE
中存储更大数量级的值,但会失去精度。换句话说,你根本无法将 68 位信息塞入 64 位编码。
mysql> create procedure p() begin declare x double; set x = 180181182149150151152; select x; end;;
mysql> call p;;
+-----------------------+
| x |
+-----------------------+
| 1.8018118214915015e20 |
+-----------------------+
如果你给它足够的精度,你可以使用DECIMAL()
:
mysql> create procedure p() begin declare x decimal(64,0); set x = 180181182149150151152; select x; end;;
mysql> call p;;
+-----------------------+
| x |
+-----------------------+
| 180181182149150151152 |
+-----------------------+
阅读更多:https://dev.mysql.com/doc/refman/8.0/en/fixed-point-types.html