MySQL 中的 `unsigned` 是什么意思以及何时使用它?

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

What does `unsigned` in MySQL mean and when to use it?

mysqltypes

提问by HELP

What does "unsigned" mean in MySQL and when should I use it?

“无符号”在 MySQL 中是什么意思,我应该什么时候使用它?

回答by codaddict

MySQLsays:

MySQL说:

All integer types can have an optional (nonstandard) attribute UNSIGNED. Unsigned type can be used to permit only nonnegative numbers in a columnor when you need a larger upper numeric rangefor 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。

When do I use it ?

我什么时候使用它?

Ask yourself this question: Will this field ever contain a negative value?
If the answer is no, then you want an UNSIGNEDdata type.

问问自己这个问题:这个字段会包含负值吗?
如果答案是否定的,那么您需要一种UNSIGNED数据类型。

A common mistake is to use a primary key that is an auto-increment INTstarting at zero, yet the type is SIGNED, in that case you'll never touch any of the negative numbers and you are reducing the range of possible id's to half.

一个常见的错误是使用一个主键是自动递增INT起始于,但类型是SIGNED,在这种情况下,你将永远不会触及任何负数的,你是减少了可能的id范围的一半。