According to the individual experiment and combine the data:
1. Length is independent of the range of values that can be used, and the range of values is only related to the number of storage bytes and unsigned of the type;
2, length refers to the display width, for example, specify 3-bit int, then the value of ID 3 and ID 300, how to align when displayed? As the manual says, the default is to fill the left with a "space". such as the following table
CREATE TABLE ' inttest ' ( ' id ' int (3) unsigned not NULL auto_increment, ' value ' varchar (2) is not NULL, PRIMARY KE Y (' id ')) EN
Now insert the data with an ID length of 2,3,4,5 bits, and the query will be displayed as follows:
MariaDB[mytest]> Select * frominttest;+-------+-------+|Id|Value|+-------+-------+| - |A|| 111 |B|| + |C|| 10000 |D|+-------+-------+4Rowsinch Set(0.00Sec
If we change to zerofill, the result of the query again is as follows:
MariaDB[mytest]> Select * frominttest;+-------+-------+|Id|Value|+-------+-------+| 013 |A|| 111 |B|| + |C|| 10000 |D|+-------+-------+4Rowsinch Set(0.00Sec
We will find that values that are less than 3 bits long are automatically populated, and values longer than 3 bits are not populated.
The following is the explanation for setting the width of the MySQL official website:
MySQL also supports the option to specify the display width of integer values (for example, INT (4)) within parentheses after the type keyword. The optional display width specifies that the width is filled from the left when the value that is less than the specified width of the column is displayed. The display width does not limit the range of values that can be saved within the column, nor does it limit the display of values that exceed the specified width of the column. “
"When combined with optional extended attribute Zerofill, the default supplemental space is replaced with 0. For example, for a column declared as int (5) Zerofill, the value 4 is retrieved as 00004. Note that if you save a value that exceeds the display width in an integer column, you will encounter problems when MySQL generates temporary tables for complex joins, because in these cases MySQL believes that the data fits the original column width. "
What does it mean to set the length for int in MySQL?