For integer data types, 对于整数数据类型,M
indicates the maximum display width. M
表示最大显示宽度。The maximum display width is 255. 最大显示宽度为255。Display width is unrelated to the range of values a type can store, as described in Section 11.1.6, “Numeric Type Attributes”.显示宽度与类型可以存储的值范围无关,如第11.1.6节,“数字类型属性”所述。
For floating-point and fixed-point data types, 对于浮点和定点数据类型,M
is the total number of digits that can be stored.M
是可以存储的总位数。
As of MySQL 8.0.17, the display width attribute is deprecated for integer data types; you should expect support for it to be removed in a future version of MySQL.从MySQL8.0.17开始,整数数据类型不再使用display width属性;您应该期望在MySQL的未来版本中删除对它的支持。
If you specify 如果为数字列指定ZEROFILL
for a numeric column, MySQL automatically adds the UNSIGNED
attribute to the column.ZEROFILL
,MySQL会自动向该列添加UNSIGNED
属性。
As of MySQL 8.0.17, the 从MySQL8.0.17开始,数字数据类型不推荐使用ZEROFILL
attribute is deprecated for numeric data types; you should expect support for it to be removed in a future version of MySQL. ZEROFILL
属性;您应该期望在MySQL的未来版本中删除对它的支持。Consider using an alternative means of producing the effect of this attribute. 考虑使用另一种方法来产生此属性的效果。For example, applications could use the 例如,应用程序可以使用LPAD()
function to zero-pad numbers up to the desired width, or they could store the formatted numbers in CHAR
columns.LPAD()
函数将填充数字归零到所需的宽度,也可以将格式化的数字存储在CHAR
列中。
Numeric data types that permit the 允许UNSIGNED
attribute also permit SIGNED
. UNSIGNED
属性的数字数据类型也允许SIGNED
属性。However, these data types are signed by default, so the 但是,这些数据类型在默认情况下是有符号的,因此SIGNED
attribute has no effect.SIGNED
属性无效。
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); 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
约束。
SERIAL
is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
.SERIAL
是BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
的别名。
SERIAL DEFAULT VALUE
in the definition of an integer column is an alias for NOT NULL AUTO_INCREMENT UNIQUE
.SERIAL DEFAULT VALUE
在整型数列的定义中是NOT NULL AUTO_INCREMENT UNIQUE
的别名。
When you use subtraction between integer values where one is of type 如果在整型值之间使用减法,其中一个值的类型为UNSIGNED
, the result is unsigned unless the NO_UNSIGNED_SUBTRACTION
SQL mode is enabled. UNSIGNED
,则结果为无符号的,除非启用了NO_UNSIGNED_SUBTRACTION
SQL模式。See Section 12.11, “Cast Functions and Operators”.请参见第12.11节,“Cast函数和运算符”。
A bit-value type. 位值类型。M
indicates the number of bits per value, from 1 to 64. M
表示每个值的位数,从1到64。The default is 1 if 如果省略M
is omitted.M
,则默认值为1。
TINYINT[(
M
)] [UNSIGNED] [ZEROFILL]
A very small integer. 一个非常小的整数。The signed range is 有符号范围是-128
to 127
. -128
到127
。The unsigned range is 无符号范围是0
to 255
.0
到255
。
These types are synonyms for 这些类型是TINYINT(1)
. TINYINT(1)
的同义词。A value of zero is considered false. 零值被认为是false
。Nonzero values are considered true:非零值被认为是true
:
mysql>SELECT IF(0, 'true', 'false');
+------------------------+ | IF(0, 'true', 'false') | +------------------------+ | false | +------------------------+ mysql>SELECT IF(1, 'true', 'false');
+------------------------+ | IF(1, 'true', 'false') | +------------------------+ | true | +------------------------+ mysql>SELECT IF(2, 'true', 'false');
+------------------------+ | IF(2, 'true', 'false') | +------------------------+ | true | +------------------------+
However, the values 但是,值TRUE
and FALSE
are merely aliases for 1
and 0
, respectively, as shown here:TRUE
和FALSE
分别只是1
和0
的别名,如下所示:
mysql>SELECT IF(0 = FALSE, 'true', 'false');
+--------------------------------+ | IF(0 = FALSE, 'true', 'false') | +--------------------------------+ | true | +--------------------------------+ mysql>SELECT IF(1 = TRUE, 'true', 'false');
+-------------------------------+ | IF(1 = TRUE, 'true', 'false') | +-------------------------------+ | true | +-------------------------------+ mysql>SELECT IF(2 = TRUE, 'true', 'false');
+-------------------------------+ | IF(2 = TRUE, 'true', 'false') | +-------------------------------+ | false | +-------------------------------+ mysql>SELECT IF(2 = FALSE, 'true', 'false');
+--------------------------------+ | IF(2 = FALSE, 'true', 'false') | +--------------------------------+ | false | +--------------------------------+
The last two statements display the results shown because 最后两个语句显示所显示的结果,因为2
is equal to neither 1
nor 0
.2
既不等于1
也不等于0
。
SMALLINT[(
M
)] [UNSIGNED] [ZEROFILL]
A small integer. 一个小整数。The signed range is 带符号范围是-32768
to 32767
. -32768
到32767
。The unsigned range is 无符号范围是0
to 65535
.0
到65535
。
MEDIUMINT[(
M
)] [UNSIGNED] [ZEROFILL]
A medium-sized integer. 中等大小的整数。The signed range is 带符号范围是-8388608
to 8388607
. -8388608
到8388607
。The unsigned range is 无符号范围是0
to 16777215
.0
到16777215
。
INT[(
M
)] [UNSIGNED] [ZEROFILL]
A normal-size integer. 正常大小的整数。The signed range is 带符号的范围是-2147483648
to 2147483647
. -2147483648
到2147483647
。The unsigned range is 无符号范围是0
to 4294967295
.0
到4294967295
。
INTEGER[(
M
)] [UNSIGNED] [ZEROFILL]
This type is a synonym for 此类型是INT
.INT
的同义词。
BIGINT[(
M
)] [UNSIGNED] [ZEROFILL]
A large integer. 一个大整数。The signed range is 带符号的范围是-9223372036854775808
to 9223372036854775807
. -9223372036854775808
到9223372036854775807
。The unsigned range is 无符号范围是0
to 18446744073709551615
.0
到18446744073709551615
。
SERIAL
is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
.SERIAL
是BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
的别名。
Some things you should be aware of with respect to 关于BIGINT
columns:BIGINT
列,您应该注意以下几点:
All arithmetic is done using signed 所有的算术都是使用有符号的BIGINT
or DOUBLE
values, so you should not use unsigned big integers larger than 9223372036854775807
(63 bits) except with bit functions! BIGINT
或DOUBLE
值来完成的,所以除了位函数外,不应该使用大于9223372036854775807
(63位)的无符号大整数!If you do that, some of the last digits in the result may be wrong because of rounding errors when converting a 如果这样做,结果中的一些最后的数字可能是错误的,因为在将BIGINT
value to a DOUBLE
.BIGINT
值转换为DOUBLE
时存在舍入错误。
MySQL can handle MySQL可以在以下情况下处理BIGINT
in the following cases:BIGINT
:
When using integers to store large unsigned values in a 在BIGINT
column.BIGINT
列中使用整数存储大的无符号值时。
In 在MIN(
or col_name
)MAX(
, where col_name
)col_name
refers to a BIGINT
column.MIN(col_name)
或MAX(col_name)
中,col_name
表示BIGINT
列。
When using operators (当使用运算符(+
, -
, *
, and so on) where both operands are integers.+
、-
、*
等)时,其中两个操作数都是整数。
You can always store an exact integer value in a 通过使用字符串存储整数值,可以始终将整数值存储在BIGINT
column by storing it using a string. BIGINT
列中。In this case, MySQL performs a string-to-number conversion that involves no intermediate double-precision representation.在本例中,MySQL执行字符串到数字的转换,不涉及中间的双精度表示。
The 当两个操作数都是整数值时,-
, +
, and *
operators use BIGINT
arithmetic when both operands are integer values. -
、+
和*
运算符使用BIGINT
算术。This means that if you multiply two big integers (or results from functions that return integers), you may get unexpected results when the result is larger than 这意味着,如果将两个大整数(或返回整数的函数的结果)相乘,当结果大于9223372036854775807
.9223372036854775807
时,可能会得到意外的结果。
DECIMAL[(
M
[,D
])] [UNSIGNED] [ZEROFILL]
A packed “exact” fixed-point number. 一个压缩的“精确”定点数。M
is the total number of digits (the precision) and D
is the number of digits after the decimal point (the scale). M
是总位数(精度),D
是小数点后的位数(刻度)。The decimal point and (for negative numbers) the 小数点和(对于负数)-
sign are not counted in M
. -
符号未计入M
。If 如果D
is 0, values have no decimal point or fractional part. D
为0,则值没有小数点或小数部分。The maximum number of digits (M
) for DECIMAL
is 65. DECIMAL
的最大位数(M
)为65。The maximum number of supported decimals (支持的最大小数位数(D
) is 30. D
)为30。If 如果省略D
is omitted, the default is 0. D
,则默认值为0。If 如果省略M
is omitted, the default is 10. M
,则默认值为10。(There is also a limit on how long the text of (DECIMAL
literals can be; see Section 12.25.3, “Expression Handling”.)DECIMAL
字面量的文本长度也有限制;请参见第12.25.3节,“表达式处理”。)
如果指定了UNSIGNED
, if specified, disallows negative values. UNSIGNED
,则不允许负值。As of MySQL 8.0.17, the 从MySQL 8.0.17开始,对于UNSIGNED
attribute is deprecated for columns of type DECIMAL
(and any synonyms); you should expect support for it to be removed in a future version of MySQL. DECIMAL
类型的列(以及任何同义词)不推荐使用UNSIGNED
属性;您应该期望在MySQL的未来版本中删除对它的支持。Consider using a simple 考虑对此类列使用简单的CHECK
constraint instead for such columns.CHECK
约束。
All basic calculations (所有带+, -, *, /
) with DECIMAL
columns are done with a precision of 65 digits.DECIMAL
列的基本计算(+
、-
、*
、/
)的精度为65位。
DEC[(
, M
[,D
])] [UNSIGNED] [ZEROFILL]NUMERIC[(
, M
[,D
])] [UNSIGNED] [ZEROFILL]FIXED[(
M
[,D
])] [UNSIGNED] [ZEROFILL]
These types are synonyms for 这些类型是DECIMAL
. DECIMAL
的同义词。The FIXED
synonym is available for compatibility with other database systems.FIXED
同义词可用于与其他数据库系统兼容。
FLOAT[(
M
,D
)] [UNSIGNED] [ZEROFILL]
A small (single-precision) floating-point number. 一个小的(单精度)浮点数。Permissible values are 允许值为-3.402823466E+38
to -1.175494351E-38
, 0
, and 1.175494351E-38
to 3.402823466E+38
. -3.4028234666E+38
至-1.175494351E-38
、0
和1.175494351E-38
至3.4028234666E+38
。These are the theoretical limits, based on the IEEE standard. 这些是基于IEEE标准的理论极限。The actual range might be slightly smaller depending on your hardware or operating system.根据硬件或操作系统的不同,实际范围可能会稍微小一些。
M
is the total number of digits and D
is the number of digits following the decimal point. M
是总位数,D
是小数点后的位数。If 如果省略M
and D
are omitted, values are stored to the limits permitted by the hardware. M
和D
,则将值存储到硬件允许的限制范围内。A single-precision floating-point number is accurate to approximately 7 decimal places.单精度浮点数精确到大约7位小数。
FLOAT(
M
,D
) is a nonstandard MySQL extension. 是一个非标准的MySQL扩展。As of MySQL 8.0.17, this syntax is deprecated, and you should expect support for it to be removed in a future version of MySQL.从MySQL8.0.17开始,这种语法就不受欢迎了,您应该希望在将来的MySQL版本中删除对它的支持。
如果指定了UNSIGNED
, if specified, disallows negative values. UNSIGNED
,则不允许负值。As of MySQL 8.0.17, the 从MySQL 8.0.17开始,UNSIGNED
attribute is deprecated for columns of type FLOAT
(and any synonyms) and you should expect support for it to be removed in a future version of MySQL. UNSIGNED
属性对于FLOAT
类型的列(以及任何同义词)都是不推荐的,您应该希望在MySQL的未来版本中删除对它的支持。Consider using a simple 考虑对此类列使用简单的CHECK
constraint instead for such columns.CHECK
约束。
Using 使用FLOAT
might give you some unexpected problems because all calculations in MySQL are done with double precision. FLOAT
可能会给您带来一些意想不到的问题,因为MySQL中的所有计算都是以双精度完成的。See Section B.3.4.7, “Solving Problems with No Matching Rows”.请参见第B.3.4.7节,“解决没有匹配行的问题”。
FLOAT(
p
) [UNSIGNED] [ZEROFILL]
A floating-point number. 浮点数。p
represents the precision in bits, but MySQL uses this value only to determine whether to use FLOAT
or DOUBLE
for the resulting data type. p
表示以位为单位的精度,但是MySQL使用这个值只是为了确定对结果数据类型使用FLOAT
还是DOUBLE
。If 如果p
is from 0 to 24, the data type becomes FLOAT
with no M
or D
values. p
从0到24,则数据类型将变为无M
或D
值的FLOAT
。If 如果p
is from 25 to 53, the data type becomes DOUBLE
with no M
or D
values. p
是从25到53,则数据类型变为DOUBLE
,没有M
或D
值。The range of the resulting column is the same as for the single-precision 结果列的范围与本节前面介绍的单精度FLOAT
or double-precision DOUBLE
data types described earlier in this section.FLOAT
或双精度DOUBLE
数据类型的范围相同。
如果指定了UNSIGNED
, if specified, disallows negative values. UNSIGNED
,则不允许负值。As of MySQL 8.0.17, the 从MySQL 8.0.17开始,UNSIGNED
attribute is deprecated for columns of type FLOAT
(and any synonyms) and you should expect support for it to be removed in a future version of MySQL. UNSIGNED
属性对于FLOAT
类型的列(以及任何同义词)都是不推荐的,您应该希望在MySQL的未来版本中删除对它的支持。Consider using a simple 考虑对此类列使用简单的CHECK
constraint instead for such columns.CHECK
约束。
为ODBC兼容性提供了FLOAT(
syntax is provided for ODBC compatibility.p
)FLOAT(
语法。p
)
DOUBLE[(
M
,D
)] [UNSIGNED] [ZEROFILL]
A normal-size (double-precision) floating-point number. 一个正常大小(双精度)的浮点数。Permissible values are 允许值为-1.7976931348623157E+308
to -2.2250738585072014E-308
, 0
, and 2.2250738585072014E-308
to 1.7976931348623157E+308
. -1.7976931348623157E+308
至-2.2250738585072014E-308
、0
和2.2250738585072014E-308
至1.7976931348623157E+308
。These are the theoretical limits, based on the IEEE standard. 这些是基于IEEE标准的理论极限。The actual range might be slightly smaller depending on your hardware or operating system.根据硬件或操作系统的不同,实际范围可能会稍微小一些。
M
is the total number of digits and D
is the number of digits following the decimal point. M
是总位数,D
是小数点后的位数。If 如果省略M
and D
are omitted, values are stored to the limits permitted by the hardware. M
和D
,则将值存储到硬件允许的限制范围内。A double-precision floating-point number is accurate to approximately 15 decimal places.双精度浮点数精确到大约15位小数。
DOUBLE(
M
,D
) is a nonstandard MySQL extension. 是一个非标准的MySQL扩展。As of MySQL 8.0.17, this syntax is deprecated and you should expect support for it to be removed in a future version of MySQL.从MySQL8.0.17开始,这种语法已经被弃用,您应该期望在MySQL8的未来版本中删除对它的支持。
如果指定了UNSIGNED
, if specified, disallows negative values. UNSIGNED
,则不允许负值。As of MySQL 8.0.17, the 从MySQL 8.0.17开始,UNSIGNED
attribute is deprecated for columns of type DOUBLE
(and any synonyms) and you should expect support for it to be removed in a future version of MySQL. UNSIGNED
属性对于DOUBLE
类型的列(以及任何同义词)是不受欢迎的,您应该希望在将来的MySQL版本中删除对它的支持。Consider using a simple 考虑对此类列使用简单的CHECK
constraint instead for such columns.CHECK
约束。
DOUBLE PRECISION[(
, M
,D
)] [UNSIGNED] [ZEROFILL]REAL[(
M
,D
)] [UNSIGNED] [ZEROFILL]
These types are synonyms for 这些类型是DOUBLE
. DOUBLE
的同义词。Exception: If the 例外:如果启用了REAL_AS_FLOAT
SQL mode is enabled, REAL
is a synonym for FLOAT
rather than DOUBLE
.REAL_AS_FLOAT
SQL模式,则REAL
是FLOAT
的同义词,而不是DOUBLE
的同义词。