简体   繁体   English

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

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

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

MySQL says: MySQL说:

All integer types can have an optional (nonstandard) attribute UNSIGNED.所有整数类型都可以有一个可选的(非标准)属性 UNSIGNED。 Unsigned type can be used to permit only nonnegative numbers in a column or when you need a larger upper numeric range for 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.例如,如果 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 UNSIGNED data type.如果答案是否定的,那么您需要一个UNSIGNED数据类型。

A common mistake is to use a primary key that is an auto-increment INT starting 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 范围减少到一半.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM