What does it mean to set the length for int in MySQL?

Source: Internet
Author: User

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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.