Java 指定的密钥太长-最大密钥长度为767字节
当我执行以下命令时:Java 指定的密钥太长-最大密钥长度为767字节,java,mysql,utf8mb4,Java,Mysql,Utf8mb4,当我执行以下命令时: create table assessment ( id integer not null auto_increment unique, assignment_weight_type tinyint not null, description varchar(255), end_date datetime not null, from_grade tinyint not null, f_id tinyint not null,
create table assessment (
id integer not null auto_increment unique,
assignment_weight_type tinyint not null,
description varchar(255),
end_date datetime not null,
from_grade tinyint not null,
f_id tinyint not null,
name varchar(255) not null,
start_date datetime not null,
status tinyint not null,
weight smallint,
school_id integer not null,
school_year_id integer not null,
s_id integer, primary key (id),
unique (name, school_id, school_year_id, from_grade, f_id)
) ENGINE=InnoDB;
我收到了以下错误消息:
Specified key was too long; max key length is 767 bytes
我使用的字符集是utf8mb4。那么为什么我会收到错误消息呢?
utf8mb4
每个字符最多使用四个字节,因此名称本身最多可以占用1020个字节。我也遇到了同样的问题,我在mysql中为这些变量设置了如下值,并修复了该问题
1.global.innodb\u大\u前缀=1
2.innodb_文件_格式=梭鱼
3.innodb_file_format_max=Barracuda使用此命令创建数据库
create database <DATABASE_NAME> character set latin1;
创建数据库字符集1;
可能重复的+1比我快。如果希望字段长度为255字节,而不是255个字符,请使用varbinary
field。MySQL最初默认使用拉丁字符集,它以2字节序列存储字符。如果我们使用这个,我们可以得到更多的长度来保存密钥长度。是的。但是OP询问了包含UTF8字符的列。我不认为他想跳过六年后的要求我也面临这个问题,我用这个解决了。所以我认为这也会有帮助。如果你不想使用UTF8,也没关系。但这与所要求的用例不匹配