MySQL 支持一个扩展,可以选择在整数数据类型的基础关键字后面的括号中指定显示宽度。例如,INT(4) 指定一个显示宽度为四位数字的 INT。这个可选的显示宽度可以被应用程序用来显示宽度小于列指定宽度的整数值,通过在前面补空格来实现。(也就是说,这个宽度会出现在结果集返回的元数据中,是否使用取决于应用程序。)MySQL supports an extension for optionally specifying the
display width of integer data types in parentheses following the
base keyword for the type. For example,
INT(4) specifies an
INT with a display width of four
digits. 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.)
显示宽度不会限制该列可以存储的值范围。也不会阻止比列显示宽度更宽的值被正确显示。例如,一个指定为 SMALLINT(3) 的列具有通常的 SMALLINT 范围,从 -32768 到 32767,超出三位数字允许的范围的值将使用超过三位数字的完整形式显示。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.
当与可选的(非标准)ZEROFILL 属性一起使用时,默认的空格填充将被零替代。例如,对于声明为 INT(4) ZEROFILL 的列,一个值为 5 的数据将被检索为 0005。When used in conjunction with the optional (nonstandard)
ZEROFILL attribute, the default padding of
spaces is replaced with zeros. For example, for a column
declared as INT(4) ZEROFILL, a
value of 5 is retrieved as
0005.
ZEROFILL 属性在参与表达式或 UNION 查询的列中将被忽略。The ZEROFILL attribute is ignored for
columns involved in expressions or
UNION queries.
如果你在具有 ZEROFILL 属性的整数列中存储大于显示宽度的值,在 MySQL 为某些复杂连接生成临时表时,可能会遇到问题。在这些情况下,MySQL 假定数据值适合列的显示宽度。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. In these cases,
MySQL assumes that the data values fit within the column
display width.
ZEROFILL 属性在数值数据类型中已被弃用,整数数据类型的显示宽度属性也是如此。你应当预计在未来版本的 MySQL 中,整数数据类型的 ZEROFILL 和显示宽度支持将被移除。考虑使用其他方法来实现这些属性的效果。例如,应用程序可以使用 LPAD() 函数将数字填充到所需宽度的零,或者可以将格式化后的数字存储在 CHAR 列中。The ZEROFILL attribute is deprecated for
numeric data types, as is the display width attribute for
integer data types. You should expect support for
ZEROFILL and display widths for integer data
types to be removed in a future version of MySQL. 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.
所有整数类型都可以有一个可选的(非标准的)UNSIGNED属性。可以使用无符号类型来允许列中只出现非负数,或者当你需要该列有更大的上限数值范围时使用。例如,如果一个INT列是UNSIGNED,该列的范围大小相同,但其端点上移,从-2147483648和2147483647变为0和4294967295。All integer types can have an optional (nonstandard)
UNSIGNED attribute. 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.
浮点类型和定点类型也可以是 UNSIGNED(UNSIGNED)。与整数类型一样,这个属性可以防止在列中存储负值。与整数类型不同的是,列值的上限范围保持不变。对于 FLOAT、DOUBLE 和 DECIMAL(及其任何同义词)类型的列,UNSIGNED 已被弃用,并且你应该预期在 MySQL 的未来版本中将不再支持它。对于此类列,考虑改用简单的 CHECK 约束。Floating-point and fixed-point types also can be
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. UNSIGNED 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. Consider using a simple
CHECK constraint instead for such columns.
如果你为数字列指定 ZEROFILL,MySQL 会自动添加 UNSIGNED 属性。If you specify ZEROFILL for a numeric column,
MySQL automatically adds the UNSIGNED
attribute.
整数或浮点数据类型可以具有 AUTO_INCREMENT 属性。当你向一个索引了 AUTO_INCREMENT 的列插入 NULL 值时,该列会被设置为下一个序列值。通常,这个值是 value+1,其中 value 是表中该列当前的最大值。(AUTO_INCREMENT 序列从 1 开始。)Integer or floating-point data types can have the
AUTO_INCREMENT attribute. When you insert a
value of NULL into an indexed
AUTO_INCREMENT column, the column is set to
the next sequence value. Typically this is
, where
value+1value is the largest value for the
column currently in the table.
(AUTO_INCREMENT sequences begin with
1.)
将 0 存入 AUTO_INCREMENT 列与存入 NULL 的效果相同,除非启用了 NO_AUTO_VALUE_ON_ZERO SQL 模式。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.
插入 NULL 以生成 AUTO_INCREMENT 值要求该列声明为 NOT NULL。如果该列声明为 NULL,插入 NULL 会存储一个 NULL。当你将任何其他值插入 AUTO_INCREMENT 列时,该列被设置为该值,并且序列会重置,以便下一个自动生成的值顺序地跟随插入的值。Inserting NULL to generate
AUTO_INCREMENT values requires that the
column be declared NOT NULL. If the column is
declared NULL, inserting
NULL stores a 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.
CHECK 约束不能引用具有 AUTO_INCREMENT 属性的列,也不能将 AUTO_INCREMENT 属性添加到已用于 CHECK 约束的现有列上。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.
AUTO_INCREMENT 不支持用于 FLOAT 和 DOUBLE 列,从 MySQL 8.4.0 起生效。要将早期版本升级到 MySQL 8.4.0 或更高版本,必须从这些列中移除 AUTO_INCREMENT 属性以避免兼容性问题,或者将其转换为整数类型。AUTO_INCREMENT for
FLOAT and
DOUBLE columns is not supported,
as of MySQL 8.4.0. To upgrade an earlier version to MySQL 8.4.0,
or higher, you must remove the AUTO_INCREMENT
attribute from such columns to avoid compatibility issues, or
convert them to an integer type.