什么时候应该在MySQL中使用UNSIGNED和SIGNED INT? [英] When should I use UNSIGNED and SIGNED INT in MySQL?

查看:553
本文介绍了什么时候应该在MySQL中使用UNSIGNED和SIGNED INT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时应在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/2n/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆