Index column size too large. The maximum column size is 767 bytes.

在MySQL数据库上建立索引时,出现错误Index column size too large. The maximum column size is 767 bytes.
由于 MySQL Innodb 引擎表索引字段长度的限制为 767 字节,因此对于多字节字符集的大字段(或者多字段组合索引),创建索引会出现上面的错误。

解决方案

Step 1 :设置参数 innodb_large_prefix 为 ON

set global innodb_file_format = BARRACUDA;
set global innodb_large_prefix = ON;

Step 2:创建表的时候指定表的 row format 格式为 Dynamic 或者 Compressed
1
2
3
4
5
create table tb_name
(
……
)
ROW_FORMAT=DYNAMIC ENGINE=InnoDB DEFAULT CHARSET=utf8;
Step 3:对于已经创建的表修改表的row_format
1
2
alter table <table_name> row_format=dynamic;
alter table <table_name> row_format=compressed;

经过上述步骤之后,再继续创建索引就可以成功了。

1
alter table <table_name> add unique index(column_name);