Difference between “int” and “int(2)” data types in my sql

前端 未结 4 2237
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 02:16

Just I wonder why the range is given with the my sql data types. I define a table with a field name \"id\" and the data type is \"int(2)\". I inserted the value to the field

4条回答
  •  庸人自扰
    2020-12-08 02:45

    For INT and other numeric types that attribute only specifies the display width.

    See Numeric Type Attributes in the MySQL documentation:

    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.)

    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. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range permitted by three digits are displayed in full using more than three digits.

提交回复
热议问题