MySQL: 列大小限制
MySQL: 列大小限制
我目前正在使用Windows操作系统,并已安装了MySQL社区服务器5.6.30,一切正常。我有一个初始化数据库的脚本,同样一切正常。\n现在我试图在Linux环境下运行这个脚本 - 同样的MySQL版本 - 但是我得到了以下错误:\n
\nERROR 1074 (42000) at line 3: Column length too big for column\n \'txt\' (max = 21845); use BLOB or TEXT instead\n
\n脚本 -\n
DROP TABLE IF EXISTS text; CREATE TABLE `texts` ( `id` BINARY(16) NOT NULL DEFAULT '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', `txt` VARCHAR(50000) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8;
\n显然,我需要在Linux上复制我的Windows操作系统上的一些MySQL服务器配置; 有人可以分享一些想法吗?\n更新1\n在AWS的RDS上也可以运行,我很确定它只是在Linux上的一个服务,所以显然这只是一个配置问题。\n有人知道如何使用UTF8达到varchar 50k吗?我不想使用TEXT或MEDIUMTEXT或其他任何东西,只是简单的varchar(size)。\n更新2\n我很感激提出的不同解决方案,但我不是在寻找一个新的解决方案,我只是想知道为什么varchar(50k)在Windows下工作,在Linux下却不工作。\n顺便说一句,我正在使用UTF8字符集和utf8_general_ci排序规则。\n答案\n为了回答我自己的问题,这是一个SQL_MODE的问题,它被设置为STRICT_TRANS_TABLES,应该被移除。
MySQL: column size limit
MySQL中的列大小限制问题主要是由于字符集和存储引擎的不同导致的。在MySQL中,不同的字符集需要不同的字节数来存储,utf8需要3个字节,utf8mb4需要4个字节,latin1和ascii只需要1个字节。
在MySQL中,使用VARCHAR(N)来定义一个字段的长度,这个长度是指字符的个数而不是字节数。VARCHAR(N)实际上是在文本前面使用1个或2个字节来存储长度。对于utf8字符集,长度不能超过65535个字节,因为2个字节的长度最大只能表示65535。
如果你的文本只包含ascii字符或英文字符,那么可以使用CHARACTER SET latin1来定义字段的字符集,latin1只需要1个字节来存储每个字符。
在InnoDB存储引擎中,当字段的长度超过一定的限制时,部分或全部字段将存储在单独的块中。对于InnoDB存储引擎,记录中存储在一起的字段的总大小不能超过8000个字节。
如果你真的需要存储50K的utf8字符,那么可以使用MEDIUMTEXT类型的字段。MEDIUMTEXT使用3个字节的长度来存储,最大可以存储16M个字节(约5M个字符)。
大多数应用程序可以使用ascii或utf8mb4字符集。ascii字符集每个字符只需要1个字节,utf8mb4字符集每个字符需要1到4个字节。utf8mb4字符集可以支持所有语言,包括Emoji和utf8字符集无法处理的4个字节的中文字符。
至于为什么Windows和Linux在这里的行为不同,我不清楚。建议你向MySQL提交一个bug报告,并提供一个链接给他们。
MySQL中的列大小限制是指在创建表时,指定某些列的最大大小。如果超过了限制,就会引发错误。
出现这个问题的原因是因为在创建表时,指定的列大小超过了MySQL的限制。解决方法是将列类型更改为TEXT类型,因为TEXT类型没有大小限制。
以下是一个示例代码,演示如何将列类型更改为TEXT:
DROP TABLE IF EXISTS texts; CREATE TABLE `texts` ( `id` BINARY(16) NOT NULL DEFAULT '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', `txt` TEXT DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8;
如果想要了解MySQL中varchar列的最大大小限制,可以参考之前的Stack Overflow问题:stackoverflow.com/questions/13506832/…。
如果想要使用varchar而不是TEXT,可以根据字符集的最大字节长度来计算最大长度。例如,在utf8字符集下,varchar的最大长度是65535个字符(根据dev.mysql.com/doc/refman/5.6/en/column-count-limit.html)。由于utf8字符集的最大字节长度为3个字节,所以错误的最大varchar大小为65535 / 3 = 21845。
MySQL: column size limit
MySQL imposes a row-size limit of 65,535 for the combined size of all columns, even though InnoDB supports larger row sizes internally. When trying to create a table with columns that exceed this limit, an error message is displayed.
For example:
CREATE TABLE t (a VARCHAR(8000), b VARCHAR(10000), c VARCHAR(10000), d VARCHAR(10000), e VARCHAR(10000), f VARCHAR(10000), g VARCHAR(10000)) ENGINE=InnoDB;
This will result in the error message: "ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs."
The reason for this limit is due to the maximum page size in MySQL, which is 65,535 bytes. When using the utf8 encoding, which uses 1, 2, or 3 bytes per character, the maximum number of characters that can fit in a page is 21,845 characters (21,845 * 3 = 65,535).
The difference in behavior between Windows and Linux versions of MySQL is unclear. Windows seems to be more conservative in its space allocation, while Linux allows for storing strings with over 21,845 characters, depending on the characters used.
To solve this issue, there are several workarounds:
1. Use the TEXT data type instead of VARCHAR.
2. Switch to a collation that has shorter characters, if it suits the data being stored.
3. Reduce the size of the column.
For collations that support shorter characters, the "latin" collations have a maximum of one byte. Documentation on character sets and collations can be found here: [dev.mysql.com/doc/refman/5.7/en/charset-mysql.html](http://dev.mysql.com/doc/refman/5.7/en/charset-mysql.html).
It is important to note that the solution may vary depending on the specific requirements and constraints of the database and application.