MySQL 数据类型 INT(11) 而 UNSIGNED INT(10)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5256560/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:03:30  来源:igfitidea点击:

MySQL datatype INT(11) whereas UNSIGNED INT(10)?

mysqltypesintunsigned-integer

提问by diEcho

in MySQL if we create a field dataType of INTand does not specify any length/values then it automatically became int(11)and if we set the attribute UNSIGNEDor UNSIGNED ZEROFILLthen it turns into int(10)

在 MySQL 中,如果我们创建一个字段 dataTypeINT并且没有指定任何长度/值,那么它会自动变成int(11),如果我们设置属性UNSIGNEDUNSIGNED ZEROFILL那么它变成int(10)

Where does this length(1)goes?

这个 长度(1)去哪里了?

回答by Shakti Singh

int value can be -2147483648 these are 11 digits so the default displaysize is 11

int 值可以是 -2147483648 这些是 11 位数字所以默认显示大小是 11

unsigned int does not allow negative numbers so by default it need only displaysize 10

unsigned int 不允许负数,因此默认情况下它只需要显示大小 10

As the documentation below shows, the number of bits required to store SIGNED INT and UNSIGNED INT is the same, the range of storable numbers is merely shifted:

正如下面的文档所示,存储 SIGNED INT 和 UNSIGNED INT 所需的位数是相同的,可存储数字的范围只是移位:

Unsigned type can be used to permit only nonnegative numbers in a column or when you need a larger upper numeric range for the column. For example, if an INT column is UNSIGNED, the size of the column's range is the same but its endpoints shift from -2147483648 and 2147483647 up to 0 and 4294967295.

无符号类型可用于在列中仅允许非负数,或者当您需要更大的列数值上限时。例如,如果 INT 列是 UNSIGNED,则该列的范围大小相同,但其端点从 -2147483648 和 2147483647 变为 0 和 4294967295。

http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

回答by Lightness Races in Orbit

According to the documentation, this number is merely the display width.

根据文档,这个数字仅仅是显示宽度

For example, INT(4) specifies an INT with a display width of four digits.

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.

例如,INT(4) 指定显示宽度为四位的 INT。

显示宽度不限制列中可以存储的值的范围。它也不会阻止比列显示宽度更宽的值正确显示。例如,指定为 SMALLINT(3) 的列通常具有 -32768 到 32767 的 SMALLINT 范围,超出三位数字允许范围的值使用三位以上数字完整显示。

The defaultdisplay width for an UNSIGNEDINTis one fewer than that for a non-UNSIGNEDINTsimply because you will never be displaying a -character.

an的默认显示宽度UNSIGNEDINT比 non-的默认显示宽度小一,UNSIGNEDINT因为您永远不会显示-字符。

Note that you can still specify whatever display width you like. This is just the default.

请注意,您仍然可以指定您喜欢的任何显示宽度。这只是默认值

The use of the term "digits" in the documentation is slightly misleading here.

文档中使用术语“数字”在这里有点误导。

回答by Darryl Hein

Just incase anyone doesn't quite understand Shakti's answer(as I didn't). Here's a visual representation of why:

以防万一有人不太明白Shakti的回答(因为我没有)。这是原因的直观表示:

 Signed minimum:
 - 2 1 4 7 4 8 3 6 4  8
 1 2 3 4 5 6 7 8 9 10 11

 Unsigned max (also the signed max):
 4 2 9 4 9 6 7 2 9 5
 1 2 3 4 5 6 7 8 9 10