Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do when you need integers larger than 20 digits on mysql?

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?

like image 475
Keyne Viana Avatar asked Aug 22 '11 02:08

Keyne Viana


1 Answers

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 a varchar (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.

like image 108
paxdiablo Avatar answered Sep 19 '22 17:09

paxdiablo