MySQL int(11) 与 int(任何其他)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7552223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
int(11) vs. int(anything else)
提问by stillenat
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).
我做了我的研究,发现在 int(11) 中,11 是整数的最大显示宽度,除非整数是 UNSIGNED(在这种情况下是 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.
这些 tuts 的作者试图达到什么目的?他们似乎都懒得解释 10 或 4 的含义。
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?
好吧,他们显然减少了显示宽度,但为什么呢?默认宽度 11 有什么问题?他们为什么要改变它?还是有其他我不知道的原因?
Thanks.
谢谢。
采纳答案by Stefan Gehrig
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.
该x
中INT(x)
有无关空间要求或其他任何性能问题,它其实只是显示宽度。通常,将显示宽度设置为合理的值对于该UNSIGNED ZEROFILL
选项最有用。
//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.
如果没有该UNSIGNED ZEROFILL
选项,该值将用空格左填充到适当的显示宽度。
//INT(4)
1
2
...
99
...
999
...
9999
...
10000
//INT(2)
1
2
...
9
...
99
...
100
回答by Ashwin A
The number is just a representational option called "display width". It may be used by some applications to pad the field when displaying numeric datatypes.
该数字只是一个称为“显示宽度”的代表性选项。某些应用程序可能会在显示数字数据类型时使用它来填充字段。
The int size is neither bits nor bytes. It's just the display width, that is used when the field has ZEROFILL specified.
int 大小既不是位也不是字节。它只是显示宽度,当字段指定了 ZEROFILL 时使用。
This blogexplains the meaning of int(size) in MySQL.
这个博客解释了 MySQL 中 int(size) 的含义。
回答by Schwern
From the docs:
从文档:
"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.)" That is, int(4)
is politely asking that 123 be displayed as " 123". Don't know how many things actually pay attention or why they'd bother in a tutorial.
“应用程序可以使用这个可选的显示宽度来显示宽度小于为列指定的宽度的整数值,方法是用空格向左填充它们。(也就是说,这个宽度存在于结果集返回的元数据中。是否是否使用取决于应用程序。)”即int(4)
礼貌地要求将 123 显示为“123”。不知道有多少事情真正受到关注,也不知道他们为什么要在教程中费心。
"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." So if you shove 123456 into an int(4)
it will still be 123456
.
“显示宽度不会限制可以存储在列中的值的范围。也不会阻止比列显示宽度更宽的值正确显示。” 因此,如果您int(4)
将123456 推入一个,它仍然会是123456
.
Nothing to do with disk space or performance, just a hint to the application retrieving the data that it would like to be displayed with at least N digits.
与磁盘空间或性能无关,只是提示应用程序检索它希望以至少 N 位显示的数据。
The related ZEROFILL option can be used in conjunction to actually pad out the returned data. So 123 in an int(4) ZEROFILL
is returned as 0123.
可以结合使用相关的 ZEROFILL 选项来实际填充返回的数据。所以 123 in anint(4) ZEROFILL
返回为 0123。
回答by Jan S
Yes it specifies the display width, and this comes into play only when ZEROFILL
is specified. So at that time it can pad the field value with the required number of zeros.
是的,它指定了显示宽度,只有在ZEROFILL
指定时才起作用。所以那时它可以用所需数量的零填充字段值。
回答by ChrisR
I agree that the MySQL manual is a little vague on the length/display width of integers. You might think that this limits the value you can store in the column to the number of digits but the valid range of the colomn doesnt change, wether you set it to 1 or 11.
我同意 MySQL 手册对整数的长度/显示宽度有点含糊。您可能认为这将您可以存储在列中的值限制为位数,但列的有效范围不会改变,无论您将其设置为 1 还是 11。
What it really does is determine the display width of the column's value. The only case this is usefull is if you use ZEROFILL to pad your values with zero's.
它真正做的是确定列值的显示宽度。唯一有用的情况是,如果您使用 ZEROFILL 用零填充您的值。
So basically there is no real difference between INT(1) and INT(11) in terms of what you can store in the column, the only case when it becomes relevant is when you want to ZEROFILL your values.
因此,就您可以在列中存储的内容而言,INT(1) 和 INT(11) 之间基本上没有真正的区别,唯一相关的情况是您想要对您的值进行 ZEROFILL。
More info:
http://alexander.kirk.at/2007/08/24/what-does-size-in-intsize-of-mysql-mean/
http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
更多信息:
http: //alexander.kirk.at/2007/08/24/what-does-size-in-intsize-of-mysql-mean/
http://dev.mysql.com/doc/refman/5.0 /en/numeric-types.html
回答by MC Emperor
It's notperformance increase neitherdifference in maximum allowed size.
这不是性能提升,也不是最大允许大小的差异。
It's onlyused when ZEROFILL is applied on that column. (See: http://alexander.kirk.at/2007/08/24/what-does-size-in-intsize-of-mysql-mean/)
它仅在对该列应用 ZEROFILL 时使用。(参见:http: //alexander.kirk.at/2007/08/24/what-does-size-in-intsize-of-mysql-mean/)
回答by stivlo
Copying from this blog article:
从这篇博客文章复制:
MySQL has a little know feature for numerical types known as zerofill. This feature effects the display size of numerical types. Unlike the string types the number inside the parentheses is not the storage size in characters for the type. For numerical types the type name itself determines storage size.
MySQL 对数字类型有一个鲜为人知的特性,称为 zerofill。此功能会影响数字类型的显示大小。与字符串类型不同,括号内的数字不是该类型的字符存储大小。对于数字类型,类型名称本身决定了存储大小。
The integer type it's the padding size for zerofill.
整数类型是零填充的填充大小。