Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mySQL datatypes - why is there a number after int: int(11)

Tags:

types

mysql

According to the mySQL Docs a datatype of int (signed) has a range of -2147483648 to 2147483647. When I create a table with phpMyAdmin, and export the table structure it shows the following:

`unit_id` int(11) NOT NULL

Why the (11)? Doesn't int tell mySQL everything it needs to know?

like image 659
Will Avatar asked Apr 21 '11 21:04

Will


1 Answers

This is the optional 'width' attribute, which can be used by applications to display the value.

To quote the 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.

like image 177
Eugene Yarmash Avatar answered Oct 23 '22 13:10

Eugene Yarmash