什么时候应该在MySQL中使用UNSIGNED和SIGNED INT? [英] When should I use UNSIGNED and SIGNED INT in MySQL?
问题描述
何时应在MySQL中使用UNSIGNED和SIGNED INT? 使用哪种更好,或者这只是个人偏爱? 因为我看过它像这样使用;
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;
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT
和
id INT(11) NOT NULL AUTO_INCREMENT
推荐答案
UNSIGNED
仅存储正数(或零).另一方面,已签名可以存储负数(即,可以具有负 ).
UNSIGNED
only stores positive numbers (or zero). On the other hand, signed can store negative numbers (i.e., may have a negative sign).
这是每个INTEGER
类型可以存储的值范围的表:
Here's a table of the ranges of values each INTEGER
type can store:
来源: http://dev.mysql.com/doc/refman/5.6/en/integer-types.html
Source: http://dev.mysql.com/doc/refman/5.6/en/integer-types.html
UNSIGNED
的范围从0
到 n
,而有符号的范围从大约-n/2
到n/2
.
在这种情况下,您有一个AUTO_INCREMENT
ID列,因此您没有负数.因此,请使用UNSIGNED
.如果在AUTO_INCREMENT
列中不使用UNSIGNED
,则最大可能值将是最大值的一半(并且值范围的负一半将不使用).
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).
这篇关于什么时候应该在MySQL中使用UNSIGNED和SIGNED INT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!