在mysql中int(11)的列的大小是多少?

和可以存储在这些列中的最大值?


当前回答

根据这里,int(11)将占用4个字节的空间,即2^(31)= 2147483648 max值和-2147483648min值的32位空间。一位是符号。

其他回答

虽然这个答案不太可能出现,但我认为有必要进行以下澄清:

the (n) behind an integer data type in MySQL is specifying the display width the display width does NOT limit the length of the number returned from a query the display width DOES limit the number of zeroes filled for a zero filled column so the total number matches the display width (so long as the actual number does not exceed the display width, in which case the number is shown as is) the display width is also meant as a useful tool for developers to know what length the value should be padded to

A BIT OF DETAIL the display width is, apparently, intended to provide some metadata about how many zeros to display in a zero filled number. It does NOT actually limit the length of a number returned from a query if that number goes above the display width specified. To know what length/width is actually allowed for an integer data type in MySQL see the list & link: (types: TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT); So having said the above, you can expect the display width to have no affect on the results from a standard query, unless the columns are specified as ZEROFILL columns OR in the case the data is being pulled into an application & that application is collecting the display width to use for some other sort of padding.

主要参考资料:https://blogs.oracle.com/jsmyth/entry/what_does_the_11_mean

正如其他人所说,列可以存储的最小/最大值以及以字节为单位占用多少存储空间仅由类型定义,而不是长度。

很多答案都说(11)部分只影响显示宽度,这并不完全正确,但主要是这样。

int(2)不指定零填充的定义:

仍然接受100的值 输出时仍然显示值100(不是0或00) 显示宽度将是选择查询输出的最大值的宽度。

(2)将做的唯一一件事是如果还指定了zerofill:

值1将显示为01。 在显示值时,列的宽度将始终为该列可能获得的最大值(对于整数来说是10位数字),而不是显示该列在特定选择查询中需要显示的最大值所需的最小宽度(可能要小得多)。 列仍然可以接受,并显示超过长度的值,但这些值不会以0作为前缀。

了解所有细微差别的最佳方法是运行:

CREATE TABLE `mytable` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `int1` int(10) NOT NULL,
    `int2` int(3) NOT NULL,
    `zf1` int(10) ZEROFILL NOT NULL,
    `zf2` int(3) ZEROFILL NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `mytable` 
(`int1`, `int2`, `zf1`, `zf2`) 
VALUES
(10000, 10000, 10000, 10000),
(100, 100, 100, 100);

select * from mytable;

它将输出:

+----+-------+-------+------------+-------+
| id | int1  | int2  | zf1        | zf2   |
+----+-------+-------+------------+-------+
|  1 | 10000 | 10000 | 0000010000 | 10000 |
|  2 |   100 |   100 | 0000000100 |   100 |
+----+-------+-------+------------+-------+

这个答案是在Linux的MySQL 5.7.12上测试的,在其他实现中可能有变化,也可能没有变化。

不管指定的长度是多少,INT总是4个字节。

TINYINT = 1字节(8位) SMALLINT = 2字节(16位) MEDIUMINT = 3字节(24位) INT = 4字节(32位) BIGINT = 8字节(64位)。

长度只是指定在使用mysql命令行客户端选择数据时要填充多少字符。12345存储为int(3)将仍然显示为12345,但如果它存储为int(10),它仍然显示为12345,但您可以选择填充前五位数字。例如,如果您添加了ZEROFILL,它将显示为0000012345。

... 最大值将是2147483647(有符号)或4294967295(无符号)

A good explanation for this can be found here To summarize : The number N in int(N) is often confused by the maximum size allowed for the column, as it does in the case of varchar(N). But this is not the case with Integer data types- the number N in the parentheses is not the maximum size for the column, but simply a parameter to tell MySQL what width to display the column at when the table's data is being viewed via the MySQL console (when you're using the ZEROFILL attribute). The number in brackets will tell MySQL how many zeros to pad incoming integers with. For example: If you're using ZEROFILL on a column that is set to INT(5) and the number 78 is inserted, MySQL will pad that value with zeros until the number satisfies the number in brackets. i.e. 78 will become 00078 and 127 will become 00127. To sum it up: The number in brackets is used for display purposes. In a way, the number in brackets is kind of usless unless you're using the ZEROFILL attribute. So the size for the int would remain same i.e., -2147483648 to 2147483648 for signed and 0 to 4294967295 for unsigned (~ 2.15 billions and 4.2 billions, which is one of the reasons why developers remain unaware of the story behind the Number N in parentheses, as it hardly affects the database unless it contains over 2 billions of rows), and in terms of bytes it would be 4 bytes.

关于整数类型大小/范围的更多信息,请参阅MySQL手册

根据这里,int(11)将占用4个字节的空间,即2^(31)= 2147483648 max值和-2147483648min值的32位空间。一位是符号。