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)
指定一个显示宽度为四位数的INT
。This 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
范围通常为-32768
到32767
,超出三位允许范围的值将使用三位以上的数字完整显示。
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
。
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 从MySQL8.0.17开始,数字数据类型不再使用ZEROFILL
attribute is deprecated for numeric data types, as is the display width attribute for integer data types. ZEROFILL
属性,整数数据类型也不再使用显示宽度属性。You should expect support for 您应该期望在将来的MySQL版本中删除对整型数据类型的ZEROFILL
and display widths for integer data types to be removed in a future version of 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
列是无符号的,则该列的范围大小相同,但其端点上移,从-2147483648
和2147483647
变为0
和4294967295
。
Floating-point and fixed-point types also can be 浮点和定点类型也可以是UNSIGNED
. UNSIGNED
。As 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 从MySQL 8.0.17开始,对于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. FLOAT
、DOUBLE
和DECIMAL
(以及任何同义词)类型的列,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 通常这是
, where value
+1value
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存储到0
into an AUTO_INCREMENT
column has the same effect as storing NULL
, unless the NO_AUTO_VALUE_ON_ZERO
SQL mode is enabled.AUTO_INCREMENT
列与存储NULL
具有相同的效果,除非启用了NO_AUTO_VALUE_ON_ZERO
SQL模式。
Inserting 插入NULL
to generate AUTO_INCREMENT
values requires that the column be declared NOT NULL
. NULL
以生成AUTO_INCREMENT
值需要将列声明为NOT NULL
。If the column is declared 如果列声明为NULL
, inserting NULL
stores a NULL
. NULL
,则插入NULL
将存储NULL
。When 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, 从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. FLOAT
和DOUBLE
列不支持AUTO_INCREMENT
;您应该期望在MySQL的未来版本中删除它。Consider removing the 考虑从这些列中删除AUTO_INCREMENT
attribute from such columns, or convert them to an integer type.AUTO_INCREMENT
属性,或者将它们转换为整数类型。