What is the size of column of int(11) in mysql in bytes?
Solution 1:
An INT
will always be 4 bytes no matter what length is specified.
-
TINYINT
= 1 byte (8 bit) -
SMALLINT
= 2 bytes (16 bit) -
MEDIUMINT
= 3 bytes (24 bit) -
INT
= 4 bytes (32 bit) -
BIGINT
= 8 bytes (64 bit).
The length just specifies how many characters to pad when selecting data with the mysql command line client. 12345 stored as int(3)
will still show as 12345, but if it was stored as int(10)
it would still display as 12345, but you would have the option to pad the first five digits. For example, if you added ZEROFILL
it would display as 0000012345.
... and the maximum value will be 2147483647 (Signed) or 4294967295 (Unsigned)
Solution 2:
INT(x) will make difference only in term of display, that is to show the number in x digits, and not restricted to 11. You pair it using ZEROFILL
, which will prepend the zeros until it matches your length.
So, for any number of x in INT(x)
- if the stored value has less digits than x,
ZEROFILL
will prepend zeros.
INT(5) ZEROFILL with the stored value of 32 will show 00032
INT(5) with the stored value of 32 will show 32
INT with the stored value of 32 will show 32
- if the stored value has more digits than x, it will be shown as it is.
INT(3) ZEROFILL with the stored value of 250000 will show 250000
INT(3) with the stored value of 250000 will show 250000
INT with the stored value of 250000 will show 250000
The actual value stored in database is not affected, the size is still the same, and any calculation will behave normally.
This also applies to BIGINT
, MEDIUMINT
, SMALLINT
, and TINYINT
.
Solution 3:
According to here, int(11)
will take 4 bytes of space that is 32 bits of space with 2^(31) = 2147483648
max value and -2147483648
min value. One bit is for sign.