不同整数 mysql 类型的含义是什么

What is the meaning of different integer mysql types

例如,TINYINT 和 INT(1) 或者 TINYINT(3) 和 MEDIUMINT 之间有什么区别?

MySQL 支持 SQL 标准整数类型 INTEGER(或 INT)和 SMALLINT。作为标准的扩展,MySQL 还支持整数类型 TINYINT、MEDIUMINT 和 BIGINT。以下 table 显示了每种整数类型所需的存储空间和范围。

参考他们的主要 website

INT - 可以有符号或无符号的正常大小的整数。如果有符号,允许的范围是-2147483648到2147483647。如果没有符号,允许的范围是0到4294967295。您可以指定最多11位的宽度。

TINYINT - 一个非常小的整数,可以有符号或无符号。如果有符号,允许的范围是 -128 到 127。如果没有符号,允许的范围是 0 到 255。您可以指定最多 4 位的宽度。

SMALLINT - 可以有符号或无符号的小整数。如果有符号,允许的范围是-32768到32767。如果没有符号,允许的范围是0到65535。你可以指定最多5位的宽度。

MEDIUMINT - 可以有符号或无符号的中型整数。如果有符号,允许的范围是-8388608到8388607。如果没有符号,允许的范围是0到16777215。你可以指定最多9位的宽度。

BIGINT - 可以有符号或无符号的大整数。如果有符号,允许的范围是从-9223372036854775808到9223372036854775807。如果没有符号,允许的范围是从0到18446744073709551615。您可以指定最多20位数字的宽度。

FLOAT(M,D) - 不能无符号的浮点数。您可以定义显示长度 (M) 和小数位数 (D)。这不是必需的,默认为 10,2,其中 2 是小数位数,10 是总位数(包括小数)。一个FLOAT的小数精度可以到24位。

DOUBLE(M,D) - 不能无符号的双精度浮点数。您可以定义显示长度 (M) 和小数位数 (D)。这不是必需的,默认为 16,4,其中 4 是小数位数。 DOUBLE 的小数精度可以达到 53 位。 REAL 是 DOUBLE 的同义词。

DECIMAL(M,D) - 无法无符号的解压缩浮点数。在未压缩的小数中,每个小数对应一个字节。需要定义显示长度 (M) 和小数位数 (D)。 NUMERIC 是 DECIMAL 的同义词。

http://www.tutorialspoint.com/mysql/mysql-data-types.htm

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 or not 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.

https://dev.mysql.com/doc/refman/5.6/en/numeric-type-attributes.html

区别在于存储。

TINYINT 是一个 1 字节。 MEDIUMINT 是 3 个字节。 INT 是 4 个字节。 TINYINT(3)这里的3是显示宽度。显示宽度与类型可以包含的值的范围无关。

因此 TINYINTTINYINT(3) 占用相同的存储空间,但呈现方式不同。