MySQL int(11) 和 int(11) UNSIGNED 有什么区别?

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

What's the difference in int(11) and int(11) UNSIGNED?

mysql

提问by Webnet

What's the difference in int(11)and int(11) UNSIGNED?

什么是在差异int(11)int(11) UNSIGNED

回答by Mark Byers

An UNSIGNED type cannot be negative, but on the other hand it has twice as large a range for the positive integers. The types TINYINT, SMALLINT, MEDIUMINT, INT and BIGINT all have signed and unsigned versions.

UNSIGNED 类型不能为负数,但另一方面,它的正整数范围是其两倍。TINYINT、SMALLINT、MEDIUMINT、INT 和 BIGINT 类型都有有符号和无符号版本。

For INT the ranges are defined as follows:

对于 INT,范围定义如下:

Type          Storage         Min           Max
INT                 4 -2147483648    2147483647
INT UNSIGNED        4           0    4294967295

The signed and unsigned types take the same storage space (4 bytes for INT).

有符号和无符号类型占用相同的存储空间(INT 为 4 个字节)。

See the documentationfor more details.

有关更多详细信息,请参阅文档

回答by Matteo Riva

INT goes from -2147483648to +2147483647
UNSIGNED INT goes from 0to 4294967295

INT 从-2147483648+2147483647
UNSIGNED INT 从04294967295

the 11between the braces has no effect on the number, just how it's displayed.

11大括号之间对数量没有影响,它只是显示方式。

回答by Armen Tsirunyan

UNSIGNED means that it can hold only nonnegative values, i.e. it can't hold for example -20

UNSIGNED 意味着它只能容纳非负值,即它不能容纳例如 -20

回答by rayman86

UNSIGNEDis exactly that, its all positive (no sign) numbers. The size of bytes is the same, but if your data is never negative you can get larger positive numbers out of it. The 11 is the default of how many characters it will fetch and display. For the exact size, do a search for the DBMS you are using and the type.

UNSIGNED就是这样,它都是正数(无符号)。字节的大小是相同的,但是如果您的数据从不为负数,则可以从中获得更大的正数。11 是它将获取和显示的字符数的默认值。对于确切的大小,请搜索您正在使用的 DBMS 和类型。

回答by Tyilo

All integer types can have an optional (nonstandard) attribute UNSIGNED. 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.

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

see here: 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 tloach

The unsigned one can't hold negative numbers.

无符号的不能容纳负数。

回答by KeithS

An unsigned integer can handle values from 0 to 2^(size in bits of the integer field). A signed integer can handle values from -2^(size of the integer field-1) to 2^(size of the integer field-1)-1.

无符号整数可以处理从 0 到 2^(整数字段的位大小)的值。有符号整数可以处理从 -2^(整数字段的大小-1)到 2^(整数字段的大小-1)-1 的值。

回答by Jayhello

I think you may want to know the difference between int and int(10).

我想您可能想知道 int 和 int(10) 之间的区别。

Let's give an example for int(10) one with zerofill keyword, one not, the table likes that:

让我们举一个 int(10) 的例子,一个是 zerofill 关键字,一个不是,表格是这样的:

create table tb_test_int_type(
    int_10 int(10),
    int_10_with_zf int(10) zerofill,
    unit int unsigned
);

Let's insert some data:

让我们插入一些数据:

insert into tb_test_int_type(int_10, int_10_with_zf, unit)
values (123456, 123456,3147483647), (123456, 4294967291,3147483647) 
;

Then

然后

select * from tb_test_int_type; 

# int_10, int_10_with_zf, unit
'123456', '0000123456', '3147483647'
'123456', '4294967291', '3147483647'

We can see that

我们可以看到

  • with keyword zerofill, num less than 10 will fill 0, but without zerofillit won't

  • Secondly with keyword zerofill, int_10_with_zf becomes unsigned int type, if you insert a minus you will get error Out of range value for column...... But you can insert minus to int_10. Also if you insert 4294967291 to int_10 you will get error Out of range value for column.....

  • 使用关键字zerofill,num 小于 10 将填充 0,但没有zerofill它不会

  • 其次,使用关键字zerofill,int_10_with_zf 变为 unsigned int 类型,如果插入减号,则会出现错误Out of range value for column.....。但是您可以在 int_10 中插入减号。此外,如果您将 4294967291 插入 int_10,您将收到错误Out of range value for column.....

Conclusion:

结论:

  1. int(X) without keyword zerofill, is equal to int range -2147483648~2147483647

  2. int(X) with keyword zerofill, the field is equal to unsigned int range 0~4294967295, if num's length is less than X it will fill 0 to the left

  1. int(X) 不带关键字zerofill,等于 int 范围 -2147483648~2147483647

  2. int(X) with keyword zerofill,字段等于unsigned int 范围0~4294967295,如果num的长度小于X,则向左填充0