关于类型:MySQL数据类型INT(11)而未签名INT(10)?

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

在MySQL中,如果我们创建的字段dataType为INT且未指定任何长度/值,则它会自动变为int(11);如果我们设置属性UNSIGNED或UNSIGNED ZEROFILL,则它将变为int(10)

这个长度(1)去哪了?


int值可以是-2147483648,这是11位数字,因此默认显示大小是11

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

如以下文档所示,存储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.

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


根据文档,此数字仅是显示宽度。

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.

UNSIGNED INT的默认显示宽度比非UNSIGNED INT的默认显示宽度小一,这是因为您永远不会显示-字符。

请注意,您仍然可以指定所需的任何显示宽度。这只是默认设置。

在本文中,术语"数字"的使用有些误导。


只是以防万一有人不太了解Shakti的答案(因为我没有)。这是原因的直观表示:

1
2
3
4
5
6
7
 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