mysql上需要大于20位的整数怎么办?
看起来 BIGINT 是 MySQL 上可用的最大整数,对吧?
例如,当您需要存储 BIGINT(80) 时该怎么办?
为什么在某些情况下,比如 Twitter API 文档中的某个地方,他们建议我们将这些大整数存储为 varchar
?
选择使用一种类型而不是另一种类型的真正原因是什么?
Seems like BIGINT
is the biggest integer available on MySQL, right?
What to do when you need to store a BIGINT(80) for example?
Why in some cases, like somewhere in the Twitter API docs, they recommend us to store these large integers as varchar
?
Which is the real reason behind the choice of using one type over another?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
大整数实际上并不限于 20 位数字,它们仅限于可以用 64 位表示的数字(例如,数字
99,999,999,999,999,999,999
不是有效的大整数,尽管它是 20 位数字长的)。存在此限制的原因是底层硬件可以相对快速地操作本机格式整数,而数字的文本版本(往往)需要一次处理一位数字。
如果您想要一个大于最大 64 位无符号整数
18,446,744,073,709,551,615
的数字,那么您需要将其存储为varchar
(或其他文本字段),并希望您不要这样做不需要对其进行太多的数学运算。或者,您可以查看范围较大但精度较低的浮点数,或者应该能够为您提供 65 位整数值的十进制数字,其中
decimal(65,0)
为列类型。Big integers aren't actually limited to 20 digits, they're limited to the numbers that can be expressed in 64 bits (for example, the number
99,999,999,999,999,999,999
is not a valid big integer despite it being 20 digits long).The reason you have this limitation is that native format integers can be manipulated relatively fast by the underlying hardware whereas textual versions of a number (tend to) need to be processed one digit at a time.
If you want a number larger than the largest 64-bit unsigned integer
18,446,744,073,709,551,615
then you will need to store it as avarchar
(or other textual field) and hope that you don't need to do much mathematical manipulation on it.Alternatively, you can look into floating point numbers which have a larger range but less precision, or decimal numbers which should be able to give you 65 digits for an integral value, with
decimal(65,0)
as the column type.您可以指定一个
numeric(65,0)
,但如果您需要变得更大,则需要一个 varchar。选择其中一种的原因是用途、效率和空间。如果您需要对其进行数学运算,使用 int 比 bigint 或我相信的 numeric 更有效。
You can specify a
numeric(65,0)
, but if you need to get larger, you'll need a varchar.The reason to select one over another is usage, efficiency and space. Using an int is more efficient than a bigint or, I believe, numeric If you need to do math on it.
您可以将大整数存储为任意二进制字符串 如果你想要最大的存储效率。
但我不确定这是否值得,因为您还必须在应用程序中处理超过 64 位整数,这也是 如果没有充分的理由,这不是您想做的事情。
最好保持简单并使用
varchar
。You can store that big integers as an arbitrary binary string if you want maximum storage efficiency.
But I'm not sure if it worth it because you'll have to deal with over 64 bit integers in your application too, which is also not the thing you want to do without a strong reason.
Better keep things simple and use
varchar
.BIGINT 根据定义限制为 8 位数字。 DECIMAL 类型的最大位数为 64。您必须使用 VARCHAR 来存储更高精度的值,并注意此类值无法直接进行数学运算。
BIGINT is limited by definition to 8 digits. The maximum number of digits in DECIMAL type is 64. You must use VARCHAR to store values of larger precision and be aware that there is no direct math of such values.