今天在使用mysql时,又遇到了ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes问题,特记录。
该问题是由键值字段长度过长导致。mysql支持数据库表单一键值的最大长度不能超过767字节,超出这个长度即报错。一般情况下,不会有键值字段的长度会超出该长度。但是需要注意的是,随着全球化进程的加快,数据库表采用UTF-8格式的趋势是越来越明显,这导致varchar类型字段的长度成倍增长,一不小心就容易出现以上问题。
假设存在以下表定义:
create table test ( name varchar(256) not null primary key, age int unsigned not null ) engine = InnoDB;
当使用UTF-8格式建立该表时,就会出现如标题所示问题。关键原因是UTF-8使用变长编码,最多可能使用4个字节表示表示1个符号。所以对于上述表中的name字段,其实际长度超出767字节,达到了768字节,问题出现。如果把长度改为191(191*4=764<767)则没有问题,或者不使用utf-8格式也没有问题。
当然,根据如下比例
latin1 = 1 byte = 1 character uft8 = 4 byte = 1 character gbk = 2 byte = 1 character
可以推算并难证gbk、latin1字符集的最大key长度。
相关推荐
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_...
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes ``` 这个错误通常发生在创建或修改具有较大数据长度的索引时,尤其是在使用了InnoDB引擎的情况...
第一个错误是“索引太长”,具体表现为SQLSTATE[42000]:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes。这个错误通常发生在为字段创建索引时,特别是当MySQL...
String Keys are UTF8 encoded and limited to 60 bytes if not specified otherwise (maximum is 255 chars). Support for long string Keys with the RaptorDBString class. Duplicate keys are stored as a WAH ...
On production servers, 4096 bytes is a good setting for performance ; reasons. ; Note: Output buffering can also be controlled via Output Buffering Control ; functions. ; Possible Values: ; On = ...
- When x86-64 support is compiled in, you could enable/disable long mode 1G pages support without recompile using new CPUID option in .bochsrc. Configure options: --enable-mmx, --enable-sse, --...