Integer field of MySQL field type

11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT

write picture description here

First of all, we should clearly understand: the concept of byte, 1 byte is equal to 8 bits (bit) , and one bit stores 0 or 1

So it can be clearly seen from the above picture:

1) tinyint : storage occupies one byte, and one byte is equal to 8 bits. According to 1 bit, two possibilities from 0 to 1 can be stored, so the tinyint type can store 2 to the 8th power, that is, 256 possibilities, starting from 0 Counting, unsigned means that it can store 0~255, if it is signed, it is -128~127.

2) smallint : storage occupies two bytes, the same as above, that is, it can store 2 to the 16th power, that is, it can store 65536 possibilities, unsigned from 0, it can store 0~65535, signed is - 32768~32767.

3) mediumint : The storage occupies three bytes, which is the 24th power of 2. It can store 16777216 possibilities. Unsigned can store 0~16777215, and signed can store -8388608~8388607.

4) int : The storage occupies four bytes, which is the 32nd power of 2. It can store 4294967296 possibilities. Unsigned can store 0~4294967295, and signed is -2147483648~2147483647.

5) bigint : storage occupies 8 bytes, that is, the 64th power of 2, which can store 64 possibilities of 2, unsigned can store 0~((2³²×²)-1), signed is -(2³²×²)/2 ~ (2³²×²)/2-1.

[note]
1. Definitions of tables that we often see: what do int(11), int(2) mean?

In fact, the number in the parentheses represents the maximum display width. This number has nothing to do with the storage size and its type. That is to say, int(11) and int(3) both store four bytes, not because of the parentheses. The number in it changes, this number just shows the maximum width.

The maximum width of the display is actually how many digits a number is displayed, and the insufficient digits are filled with 0 at the front; for example: int (3) stores 3 and represents 003; int (5) stores 23 and represents 00023.

2. Note: The mysql manual clearly states that you may encounter problems when mysql generates temporary tables for some complex joins, because in this case mysql trusts that all values ​​are suitable Original column width!

Guess you like

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