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

点点圈 提交于 2019-12-03 06:28:18

问题


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

Where does this length(1) goes?


回答1:


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

unsigned int does not allow negative numbers so by default it need only display size 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:

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




回答2:


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.

The default display width for an UNSIGNED INT is one fewer than that for a non-UNSIGNED INT simply because you will never be displaying a - character.

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.




回答3:


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

 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


来源:https://stackoverflow.com/questions/5256560/mysql-datatype-int11-whereas-unsigned-int10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!