int(11) vs. int(anything else)

前端 未结 7 1969
借酒劲吻你
借酒劲吻你 2020-11-27 15:19

I\'m new to web programming and doing different tutorials that I can find on the net.

I did my research and found out that in int(11), 11 is the maximum display widt

相关标签:
7条回答
  • 2020-11-27 15:37

    Copying from this blog article:

    MySQL has a little know feature for numerical types known as zerofill. This feature effects the display size of numerical types. Unlike the string types the number inside the parentheses is not the storage size in characters for the type. For numerical types the type name itself determines storage size.

    The integer type it’s the padding size for zerofill.

    0 讨论(0)
  • 2020-11-27 15:44

    The number is just a representational option called "display width". It may be used by some applications to pad the field when displaying numeric datatypes.

    The int size is neither bits nor bytes. It's just the display width, that is used when the field has ZEROFILL specified.

    This blog explains the meaning of int(size) in MySQL.

    0 讨论(0)
  • 2020-11-27 15:45

    Yes it specifies the display width, and this comes into play only when ZEROFILL is specified. So at that time it can pad the field value with the required number of zeros.

    0 讨论(0)
  • 2020-11-27 15:49

    From the docs:

    "This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.)" That is, int(4) is politely asking that 123 be displayed as " 123". Don't know how many things actually pay attention or why they'd bother in a tutorial.

    "The display width does not constrain the range of values that can be stored in the column. Nor does it prevent values wider than the column display width from being displayed correctly." So if you shove 123456 into an int(4) it will still be 123456.

    Nothing to do with disk space or performance, just a hint to the application retrieving the data that it would like to be displayed with at least N digits.

    The related ZEROFILL option can be used in conjunction to actually pad out the returned data. So 123 in an int(4) ZEROFILL is returned as 0123.

    0 讨论(0)
  • 2020-11-27 15:53

    The x in INT(x) has nothing to do with space requirements or any other performance issues, it's really just the display width. Generally setting the display widths to a reasonable value is mostly useful with the UNSIGNED ZEROFILL option.

    //INT(4) UNSIGNED ZEROFILL
    0001
    0002 
    ...
    0099
    ...
    0999
    ...
    9999
    ...
    10000
    
    //INT(2) UNSIGNED ZEROFILL
    01
    02 
    ...
    09
    ...
    99
    ...
    100
    

    Without the UNSIGNED ZEROFILL option the value will be left-padded with spaces to the appropriate display width.

    //INT(4)
       1
       2 
    ...
      99
    ...
     999
    ...
    9999
    ...
    10000
    
    //INT(2)
     1
     2 
    ...
     9
    ...
    99
    ...
    100
    
    0 讨论(0)
  • 2020-11-27 15:57

    It's not performance increase neither difference in maximum allowed size.

    It's only used when ZEROFILL is applied on that column. (See: http://alexander.kirk.at/2007/08/24/what-does-size-in-intsize-of-mysql-mean/)

    0 讨论(0)
提交回复
热议问题