Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

mysql - int(11) vs. int(anything else)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...