Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int(11) vs. int(anything else)

Tags:

mysql

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 width for integers and it's the default value if unless the integer is UNSIGNED (in this case it's 10).

When I see something like this:

id INT(11) not null AUTO_INCREMENT 

I have no questions. But why do I see different things in different tutorials? For examlpe, in some, it says,

id INT(10) not null AUTO_INCREMENT 

And even

id INT(4) not null AUTO_INCREMENT 

What are the authors of those tuts trying to achieve? None of them seem to bother to give an explanation what 10 or 4 means.

Alright, they're obviously reducing the display width, but why? What's wrong with the default width of 11? Why would they want to change it? Or are there any other reasons I don't know of?

Thanks.

like image 642
stillenat Avatar asked Sep 26 '11 08:09

stillenat


People also ask

What does it mean int 11?

in int(11), 11 is the display width of the integer column, unlike the characters columns where the number means number of character that can be stored. int(11) – The number in the parenthesis i.e () does not determines the max and min values that can be stored in the integer field.

What is the meaning of int 10 in MySQL?

INT(10) means you probably defined it as INT UNSIGNED . So, you can store numbers from 0 up to 4294967295 (note that the maximum value has 10 digits, so MySQL automatically added the (10) in the column definition which (10) is just a format hint and nothing more.

What is maximum display width?

The maximum display width is 255. Display width is unrelated to the range of values a type can store, as described in Section 11.1. 6, “Numeric Type Attributes”. For floating-point and fixed-point data types, M is the total number of digits that can be stored.


1 Answers

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 
like image 66
Stefan Gehrig Avatar answered Oct 19 '22 13:10

Stefan Gehrig