我什么时候应该在 MySQL 中使用 UNSIGNED 和 SIGNED INT?

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

When should I use UNSIGNED and SIGNED INT in MySQL?

mysql

提问by Tux

When should I use UNSIGNED and SIGNED INT in MySQL ? What is better to use or this is just personal prefernce ? Because I've seen it used like this;

我什么时候应该在 MySQL 中使用 UNSIGNED 和 SIGNED INT ?什么更好用,或者这只是个人喜好?因为我见过它是这样使用的;

id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT

and

id INT(11) NOT NULL AUTO_INCREMENT

回答by Wiseguy

UNSIGNEDonly stores positive numbers (or zero). On the other hand, signed can store negative numbers (i.e., may have a negative sign).

UNSIGNED只存储正数(或零)。另一方面,signed 可以存储负数(即可能有负)。

Here's a table of the ranges of values each INTEGERtype can store:

这是每种INTEGER类型可以存储的值范围的表格:

MySQL INTEGER types and lengths
Source: http://dev.mysql.com/doc/refman/5.6/en/integer-types.html

MySQL INTEGER 类型和长度
来源:http: //dev.mysql.com/doc/refman/5.6/en/integer-types.html

UNSIGNEDranges from 0to n, while signed ranges from about -n/2to n/2.

UNSIGNED范围从0n,而带符号的范围从 about-n/2n/2

In this case, you have an AUTO_INCREMENTID column, so you would not have negatives. Thus, use UNSIGNED. If you do not use UNSIGNEDfor the AUTO_INCREMENTcolumn, your maximum possible value will be half as high (and the negative half of the value range would go unused).

在这种情况下,您有一个AUTO_INCREMENTID 列,因此您不会有负数。因此,使用UNSIGNED. 如果您不使用UNSIGNEDAUTO_INCREMENT列,您的最大可能值将是其一半(值范围的负一半将不被使用)。

回答by Paul Denisevich

Use UNSIGNEDfor non-negative integers.

使用UNSIGNED非负整数。

回答by Srneczek

Basically with UNSIGNED, you're giving yourself twice as much space for the integer since you explicitly specify you don't need negative numbers (usually because values you store will never be negative).

基本上,使用UNSIGNED,您为整数提供了两倍的空间,因为您明确指定不需要负数(通常是因为您存储的值永远不会是负数)。

回答by Kinga the Witch

I'm not agree with vipin cp.

我不同意vipin cp

The true is that first bit is used for represent the sign. But 1is for negative and 0is for positive values. More over negative values are coded in different way (two's complement). Example with TINYINT:

真实的是第一位用于表示符号。但是1表示负值,0表示正值。更多的负值以不同的方式编码(二进制补码)。TINYINT 示例:

The sign bit
|
1000 0000b = -128d  
...  
1111 1101b = -3d  
1111 1110b = -2d  
1111 1111b = -1d  

0000 0000b = 0d  
0000 0001b = 1d  
0000 0010b = 2d  
...  
0111 1111b = 127d  

回答by Debanik Dawn

If you know the type of numbers you are going to store, your can choose accordingly. In this case your have 'id' which can never be negative. So you can use unsigned int. Range of signed int: -n/2 to +n/2 Range of unsigned int: 0 to n So you have twice the number of positive numbers available. Choose accordingly.

如果您知道要存储的数字类型,则可以相应地进行选择。在这种情况下,您的 'id' 永远不会为负数。所以你可以使用无符号整数。有符号整数的范围:-n/2 到 +n/2 无符号整数的范围:0 到 n 所以你有两倍的可用正数。相应地选择。

回答by Sheikh Hafizur Rahman

For negative integer value, SIGNEDis used and for non-negative integer value, UNSIGNEDis used. It always suggested to use UNSIGNEDfor id as a PRIMARY KEY.

对于负整数值,SIGNED使用,对于非负整数值,UNSIGNED使用。它总是建议将UNSIGNEDfor id 用作 PRIMARY KEY。

回答by Kamal

I think, UNSIGNEDwould be the best option to store something like time_duration(Eg: resolved_call_time = resolved_time(DateTime)-creation_time(DateTime)) value in minutes or hours or seconds format which will definitely be a non-negative number

我认为,以分钟或小时或秒格式UNSIGNED存储类似time_duration(Eg:)resolved_call_time = resolved_time(DateTime)-creation_time(DateTime)值的最佳选择,这肯定是一个非负数

回答by vipin cp

One thing i would like to add In a signed int, which is the default value in mysql, 1 bitwill be used to represent sign. -1 for negative and 0 for positive.So if your application insert only positive value it should better specify unsigned.

我想在 a 中添加一件事signed int,即default value in mysql1 bit将用于表示sign. -1 for negative and 0 for positive.因此,如果您的应用程序仅插入正值,则最好指定无符号。