在mysql中int(11)的列的大小是多少?
和可以存储在这些列中的最大值?
在mysql中int(11)的列的大小是多少?
和可以存储在这些列中的最大值?
不管指定的长度是多少,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(无符号)
在mysql中int(11)的列的大小是多少?
(11) - int数据类型的这个属性与列的大小无关。它只是整数数据类型的显示宽度。从11.1.4.5。属性:
MySQL支持可选地指定显示的扩展 base关键字后面括号中的整数数据类型的宽度 对于类型。例如,INT(4)指定带显示的INT 四位数字的宽度。
INT(x)只会在显示方面有所不同,即以x位数显示数字,而不限于11。您使用ZEROFILL对它进行配对,它将在0的前面,直到它匹配您的长度。
对于INT(x)中的任意个数的x
如果存储值的数字小于x,则ZEROFILL将在0的前面。
INT(5)存储值为32的ZEROFILL将显示00032 存储值为32的INT(5)将显示32 INT的存储值为32将显示32
如果存储值的数字比x多,它将按原样显示。
INT(3)存储值为250000的ZEROFILL将显示250000 存储值为250000的INT(3)将显示250000 存储值为250000的INT将显示250000
存储在数据库中的实际值不受影响,大小仍然相同,任何计算都将正常进行。
这也适用于BIGINT, MEDIUMINT, SMALLINT和TINYINT。
在MySQL中,整数int(11)的大小为4字节,等于32位。
带符号的值是-2的32-1次方到0到2的32-1次方-1 = -2147483648到0到2147483647
无符号值是:0到2^32-1 = 0 ~ 4294967295
虽然这个答案不太可能出现,但我认为有必要进行以下澄清:
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
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手册
正如其他人所说,列可以存储的最小/最大值以及以字节为单位占用多少存储空间仅由类型定义,而不是长度。
很多答案都说(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上测试的,在其他实现中可能有变化,也可能没有变化。
根据这本书:
MySQL允许您为整数类型指定一个“宽度”,例如INT(11)。 对于大多数应用程序来说,这是没有意义的:它没有限制 合法的值范围,但只是指定字符的数量 MySQL的交互工具将保留用于显示目的。为 为了存储和计算目的,INT(1)与INT(20)是相同的。