Explain the length value of mysql int type in detail

 

 

Below is the storage and range of each integer type (from the mysql manual)

Types of

byte

minimum

maximum value

 

 

( signed / unsigned )

( signed / unsigned )

TINYINT

1

-128

127

 

 

0

255

SMALLINT

2

-32768

32767

 

 

0

65535

MEDIUMINT

3

-8388608

8388607

 

 

0

16777215

INT

4

-2147483648

2147483647

 

 

0

4294967295

BIGINT

8

-9223372036854775808

9223372036854775807

 

 

0

18446744073709551615

The table has a total of four columns: the field type, the number of bytes occupied, the minimum value allowed to be stored, and the maximum value allowed to be stored.

Let's take the int type as an example:

Int type, the number of bytes occupied is 4 bytes, students who have studied computer principles should know that the byte (byte) is not the smallest unit of computer storage, and there is a smaller unit than the byte (byte), that is, the bit (bit) ), a bit represents a 0 or 1; 8 bits make up a byte; general bytes use uppercase B to represent byte, and bits use lowercase b to represent bit.

Conversion of computer storage units:

1B = 8b

1KB=1024B

1MB=1024KB

Then according to the number of bytes allowed to be stored by the int type is 4 bytes, we can convert that the minimum value that can be stored in the int  UNSIGNED (unsigned) type is 0, and the maximum value is 4294967295 (ie 4B=32b, the maximum value is is composed of 32 1s);

 

 

 

The display width of the integer value (for example, INT(4)) is specified in parentheses after the Mysql type keyword . The optional display width specification is used to fill the width from the left when the display width is smaller than the specified column width. The display width does not limit the range of values ​​that can be stored within the column, nor does it limit the display of values ​​that exceed the specified width of the column.

当结合可选扩展属性ZEROFILL使用时, 默认补充的空格用零代替。例如,对于声明为INT(5) ZEROFILL的列,值4检索为00004。请注意如果在整数列保存超过显示宽度的一个值,当MySQL为复杂联接生成临时表时会遇到问题,因为在这些情况下MySQL相信数据适合原列宽度。

所有整数类型可以有一个可选(非标准)属性UNSIGNED。当你想要在列内只允许非负数和该列需要较大的上限数值范围时可以使用无符号值。

 

所以int(10)与int(11)后的括号中的字符表示显示宽度,整数列的显示宽度与mysql需要用多少个字符来显示该列数值,与该整数需要的存储空间的大小都没有关系,int类型的字段能存储的数据上限还是2147483647(有符号型)和4294967295(无符号型)。

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326097186&siteId=291194637