11.1.6 Numeric Type Attributes数字类型属性

MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. MySQL支持一个扩展,可以选择在类型的base关键字后面的括号中指定整数数据类型的显示宽度。For example, INT(4) specifies an INT with a display width of four digits. 例如,INT(4)指定一个显示宽度为四位数的INTThis optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. 应用程序可以使用这个可选的显示宽度来显示宽度小于为列指定的宽度的整数值,方法是用空格左填充。(That is, this width is present in the metadata returned with result sets. Whether it is used is up to the application.)(也就是说,此宽度存在于随结果集返回的元数据中。是否使用取决于应用程序。)

The display width does not constrain the range of values that can be stored in the column. 显示宽度限制可存储在列中的值的范围。Nor does it prevent values wider than the column display width from being displayed correctly. 它也不会阻止比列显示宽度更宽的值正确显示。For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range permitted by three digits are displayed in full using more than three digits.例如,指定为SMALLINT(3)的列的SMALLINT范围通常为-3276832767,超出三位允许范围的值将使用三位以上的数字完整显示。

When used in conjunction with the optional (nonstandard) ZEROFILL attribute, the default padding of spaces is replaced with zeros. 当与可选(非标准)ZEROFILL属性一起使用时,空格的默认填充将替换为零。For example, for a column declared as INT(4) ZEROFILL, a value of 5 is retrieved as 0005.例如,对于声明为INT(4) ZEROFILL的列,值5检索为0005

Note注意

The ZEROFILL attribute is ignored for columns involved in expressions or UNION queries.表达式或联合查询中涉及的列将忽略ZEROFILL属性。

If you store values larger than the display width in an integer column that has the ZEROFILL attribute, you may experience problems when MySQL generates temporary tables for some complicated joins. 如果将大于显示宽度的值存储在具有ZEROFILL属性的整数列中,当MySQL为一些复杂的联接生成临时表时,可能会遇到问题。In these cases, MySQL assumes that the data values fit within the column display width.在这些情况下,MySQL假设数据值符合列显示宽度。

As of MySQL 8.0.17, the ZEROFILL attribute is deprecated for numeric data types, as is the display width attribute for integer data types. 从MySQL8.0.17开始,数字数据类型不再使用ZEROFILL属性,整数数据类型也不再使用显示宽度属性。You should expect support for ZEROFILL and display widths for integer data types to be removed in a future version of MySQL. 您应该期望在将来的MySQL版本中删除对整型数据类型的ZEROFILL和显示宽度的支持。Consider using an alternative means of producing the effect of these attributes. 考虑使用另一种方法来产生这些属性的效果。For example, applications can use the LPAD() function to zero-pad numbers up to the desired width, or they can store the formatted numbers in CHAR columns.例如,应用程序可以使用LPAD()函数将填充数字归零到所需的宽度,也可以将格式化的数字存储在CHAR列中。

All integer types can have an optional (nonstandard) UNSIGNED attribute. 所有整数类型都可以具有可选(非标准)UNSIGNED属性。An 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 up, from -2147483648 and 2147483647 to 0 and 4294967295.例如,如果INT列是无符号的,则该列的范围大小相同,但其端点上移,从-21474836482147483647变为04294967295

Floating-point and fixed-point types also can be UNSIGNED. 浮点和定点类型也可以是UNSIGNEDAs with integer types, this attribute prevents negative values from being stored in the column. 与整数类型一样,此属性防止在列中存储负值。Unlike the integer types, the upper range of column values remains the same. 与整数类型不同,列值的上限保持不变。As of MySQL 8.0.17, the UNSIGNED attribute is deprecated for columns of type FLOAT, DOUBLE, and DECIMAL (and any synonyms) and you should expect support for it to be removed in a future version of MySQL. 从MySQL 8.0.17开始,对于FLOATDOUBLEDECIMAL(以及任何同义词)类型的列,UNSIGNED属性就不受欢迎了,您应该希望在将来的MySQL版本中删除对它的支持。Consider using a simple CHECK constraint instead for such columns.考虑对此类列使用简单的CHECK约束。

If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute.如果为数字列指定ZEROFILL,MySQL会自动添加UNSIGNED属性。

Integer or floating-point data types can have the AUTO_INCREMENT attribute. 整数或浮点数据类型可以具有AUTO_INCREMENT属性。When you insert a value of NULL into an indexed AUTO_INCREMENT column, the column is set to the next sequence value. 在索引的自动递增列中插入NULL值时,该列将设置为下一个序列值。Typically this is value+1, where value is the largest value for the column currently in the table. 通常这是value+1,其中value是表中当前列的最大值。(AUTO_INCREMENT sequences begin with 1.)(自动递增AUTO_INCREMENT序列从1开始。)

Storing 0 into an AUTO_INCREMENT column has the same effect as storing NULL, unless the NO_AUTO_VALUE_ON_ZERO SQL mode is enabled.将0存储到AUTO_INCREMENT列与存储NULL具有相同的效果,除非启用了NO_AUTO_VALUE_ON_ZEROSQL模式。

Inserting NULL to generate AUTO_INCREMENT values requires that the column be declared NOT NULL. 插入NULL以生成AUTO_INCREMENT值需要将列声明为NOT NULLIf the column is declared NULL, inserting NULL stores a NULL. 如果列声明为NULL,则插入NULL将存储NULLWhen you insert any other value into an AUTO_INCREMENT column, the column is set to that value and the sequence is reset so that the next automatically generated value follows sequentially from the inserted value.AUTO_INCREMENT列中插入任何其他值时,该列将被设置为该值,并且序列将被重置,以便下一个自动生成的值从插入的值开始依次跟随。

Negative values for AUTO_INCREMENT columns are not supported.AUTO_INCREMENT(自动递增)列不支持负值。

CHECK constraints cannot refer to columns that have the AUTO_INCREMENT attribute, nor can the AUTO_INCREMENT attribute be added to existing columns that are used in CHECK constraints.CHECK约束不能引用具有AUTO_INCREMENT属性的列,也不能将AUTO_INCREMENT属性添加到CHECK约束中使用的现有列。

As of MySQL 8.0.17, AUTO_INCREMENT support is deprecated for FLOAT and DOUBLE columns; you should expect it to be removed in a future version of MySQL. 从MySQL 8.0.17开始,FLOATDOUBLE列不支持AUTO_INCREMENT;您应该期望在MySQL的未来版本中删除它。Consider removing the AUTO_INCREMENT attribute from such columns, or convert them to an integer type.考虑从这些列中删除AUTO_INCREMENT属性,或者将它们转换为整数类型。