What does INT(5) in mysql mean?

前端 未结 7 413
孤独总比滥情好
孤独总比滥情好 2020-12-29 18:27

I always thought INT(5) means a number which has a max size of 5 digits. I tried entering a huge number inside it and it somehow got cut down to 2147483647

This obvi

相关标签:
7条回答
  • 2020-12-29 19:09

    An int can be between -2147483648 and 2147483647 signed, or 0 and 4294967295 unsigned.

    Thats why it was cut down to that if you entered a number larger than it as a signed value

    0 讨论(0)
  • 2020-12-29 19:13

    A very common misconception about what int(N) means in MySQL is that the column can store maximum integer value with N digits in length. However, this is not true. int(N) does not determines the maximum value that the column can store in it. N is the display width of the integer column, unlike the characters columns where the number means number of character that can be stored.

    The number in the parenthesis does not determines the max and min values that can be stored in the integer field. The max and min values that can be stored are always fixed. The following table shows the required storage and range for each integer type.

    0 讨论(0)
  • 2020-12-29 19:21

    From MySQL Docs, Numeric Type Attributes

    MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example, INT(4) specifies an INT with a display width of four digits. 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.)

    0 讨论(0)
  • 2020-12-29 19:25

    INT is always four bytes wide. The 5 is the "display width".

    0 讨论(0)
  • 2020-12-29 19:28

    http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html

    "M indicates the maximum display width for integer types. The maximum legal display width is 255. Display width is unrelated to the range of values a type can contain, as described in Section 10.2, “Numeric Types”"

    0 讨论(0)
  • 2020-12-29 19:31

    In MySQL, INT(5) does not mean that values are limited to 5-character values. It only means that MySQL will try to pad these values with spaces/zeroes when returning them.

    The numeric range of any signed INT including INT(10), INT(5) or any other INT(n) is: -2,147,483,648 ... 2,147,483,647, which is 10 digits at most.

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