MySQL 在mysql上需要大于20位的整数怎么办?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7142604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What to do when you need integers larger than 20 digits on mysql?
提问by Keyne Viana
Seems like BIGINT
is the biggest integer available on MySQL, right?
似乎BIGINT
是 MySQL 上可用的最大整数,对吗?
What to do when you need to store a BIGINT(80) for example?
例如,当您需要存储 BIGINT(80) 时该怎么办?
Why in some cases, like somewhere in the Twitter API docs, they recommend us to store these large integers as varchar
?
为什么在某些情况下,比如 Twitter API 文档中的某个地方,他们建议我们将这些大整数存储为varchar
?
Which is the real reason behind the choice of using one type over another?
选择使用一种类型而不是另一种类型背后的真正原因是什么?
回答by paxdiablo
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).
大整数实际上并不限于 20 位,它们仅限于可以用 64 位表示的数字(例如,99,999,999,999,999,999,999
尽管该数字有 20 位长,但它不是有效的大整数)。
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 a varchar
(or other textual field) and hope that you don't need to do much mathematical manipulation on it.
如果您想要一个大于最大 64 位无符号整数的数字,18,446,744,073,709,551,615
那么您需要将其存储为一个varchar
(或其他文本字段),并希望您不需要对其进行大量数学运算。
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.
或者,您可以查看具有更大范围但精度较低的浮点数,或应该能够为整数值提供 65 位数字的十进制数,decimal(65,0)
作为列类型。
回答by Doug Kress
You can specify a numeric(65,0)
, but if you need to get larger, you'll need a varchar.
您可以指定 a numeric(65,0)
,但如果您需要变大,则需要一个 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.
选择一个而不是另一个的原因是使用、效率和空间。使用 int 比 bigint 更有效,我相信,如果您需要对其进行数学运算,则使用 numeric 。
回答by sanmai
You can store that big integers as an arbitrary binary stringif 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 dowithout a strong reason.
但我不确定这是否值得,因为您还必须在应用程序中处理超过 64 位的整数,如果没有充分的理由,这也不是您想要做的事情。
Better keep things simple and use varchar
.
最好保持简单和使用varchar
。
回答by mozillanerd
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.
BIGINT 根据定义限制为 8 位数字。DECIMAL 类型中的最大位数为 64。您必须使用 VARCHAR 来存储更高精度的值,并注意这些值没有直接的数学运算。