Is Bigint(8) the Largest Integer MySQL Can Store

Is BIGINT(8) the largest integer MySQL can store?

The number represents how it's displayed - it doesn't affect how the data is stored.

From the manual:

Another extension is supported by MySQL for optionally specifying the display width of integer data types in parentheses following the base keyword for the type (for example, INT(4)). This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.)

The display width does not constrain the range of values that can be stored in the column, nor the number of digits that are displayed for values having a width exceeding that specified for the column. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range allowed by three characters are displayed using more than three characters.

A BIGINT is always 8 bytes and can store -9223372036854775808 to 9223372036854775807 (signed) or 0 to 18446744073709551615 (unsigned).

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

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.

Types in MySQL: BigInt(20) vs Int(20)

See http://dev.mysql.com/doc/refman/8.0/en/numeric-types.html

  • INT is a four-byte signed integer.

  • BIGINT is an eight-byte signed integer.

They each accept no more and no fewer values than can be stored in their respective number of bytes. That means 232 values in an INT and 264 values in a BIGINT.

The 20 in INT(20) and BIGINT(20) means almost nothing. It's a hint for display width. It has nothing to do with storage, nor the range of values that column will accept.

Practically, it affects only the ZEROFILL option:

CREATE TABLE foo ( bar INT(20) ZEROFILL );
INSERT INTO foo (bar) VALUES (1234);
SELECT bar from foo;

+----------------------+
| bar |
+----------------------+
| 00000000000000001234 |
+----------------------+

It's a common source of confusion for MySQL users to see INT(20) and assume it's a size limit, something analogous to CHAR(20). This is not the case.

MySQL: bigint Vs int

The difference is purely in the maximum value which can be stored (18,446,744,073,709,551,615 for the bigint(20) and 4,294,967,295 for the int(10), I believe), as per the details on the MySQL Numeric Types manual page.

Incidentally, the use of (20) and (10) is largely irrelevant unless you're using ZEROFILL. (i.e.: It doesn't actually change the size of the number stored - that's all down to the type.)

However, in practical terms it should be noted that you're not likely to hit either of these limits any time soon, unless you're a really active blogger.

why a 10 digit phone number cant be stored in an integer of length 10?

the INT data type in mysql does not go by length of the integer, but rather the max and min value that can be stored with 4 bytes. Also, it should be noted that you can have either a SIGNED INT, or an UNSIGNED INT, which also effects the max and min value.

So for instance, a SIGNED INT can store values from -2147483648 to 2147483648, and an UNSIGNED INT can store values from 0 to 4294967295. Neither option could store a ten digit phone number.

You could however use a BIGINT, which uses 8 bytes of data to store its values, allowing a much higher min and max allowed. But you may be better served by VARCHAR, as a phone number is not really an integer. For instance, the phone number 0012345678, would just be stored as 12345678 in an INT field, and you would then have to run formatting conversions on the data before displaying.

What is the MAX number if I store int(255) in MySQL?

Something is probably just converting that to int(11) for you. Since you can't have 255 visible digits in an int, the maximum value will be 2147483647.

If you need more than that you can set it to be unsigned, since I'm assuming you have no negative ids and then you can have up to 4294967295.

If you are ever going to have more than 4 billion records (very unlikely if you're at 1 million right now), then you could use a bigint instead, which allows you to store numbers up to 18446744073709551615 at a cost of more storage space of course.



Related Topics



Leave a reply



Submit