# \[42000\]\[1071\] Specified key was too long; max key length is 767 bytes MySQL创建唯一索引时报错

**URL:** <https://www.shiqidu.com/t/topic/430>\
**Category:** 开发调优\
**Tags:** MySQL\
**Created:** [May 28, 2018, 8:57am UTC](https://www.shiqidu.com/t/topic/430 "2018-05-28T08:57:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
**Post date:** [May 28, 2018, 8:57am UTC](https://www.shiqidu.com/t/topic/430/1 "2018-05-28T08:57:59Z")

</div>

> [42000][1071] Specified key was too long; max key length is 767 bytes

MySQL在创建一个字符串类型的唯一索引时报错如上。

已知MySQL引擎是innodb，编码是utf8mb4。

上边的报错信息很明显 指定的key太长，最大的长度为767个字节。

utf8mb4编码下一个字符占4个字节长度。

所以字段的长度不能超过767/4=191。
