我什么时候应该在 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
When should I use UNSIGNED and SIGNED INT in 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
UNSIGNED
only 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 INTEGER
type can store:
这是每种INTEGER
类型可以存储的值范围的表格:
Source: http://dev.mysql.com/doc/refman/5.6/en/integer-types.html
来源:http: //dev.mysql.com/doc/refman/5.6/en/integer-types.html
UNSIGNED
ranges from 0
to n
, while signed ranges from about -n/2
to n/2
.
UNSIGNED
范围从0
到n
,而带符号的范围从 about-n/2
到n/2
。
In this case, you have an AUTO_INCREMENT
ID column, so you would not have negatives. Thus, use UNSIGNED
. If you do not use UNSIGNED
for the AUTO_INCREMENT
column, your maximum possible value will be half as high (and the negative half of the value range would go unused).
在这种情况下,您有一个AUTO_INCREMENT
ID 列,因此您不会有负数。因此,使用UNSIGNED
. 如果您不使用UNSIGNED
该AUTO_INCREMENT
列,您的最大可能值将是其一半(值范围的负一半将不被使用)。
回答by Paul Denisevich
Use UNSIGNED
for 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, SIGNED
is used and for non-negative integer value, UNSIGNED
is used. It always suggested to use UNSIGNED
for id as a PRIMARY KEY.
对于负整数值,SIGNED
使用,对于非负整数值,UNSIGNED
使用。它总是建议将UNSIGNED
for id 用作 PRIMARY KEY。
回答by Kamal
I think, UNSIGNED
would 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 bit
will 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 mysql
,1 bit
将用于表示sign
. -1 for negative and 0 for positive.
因此,如果您的应用程序仅插入正值,则最好指定无符号。