MySQL: Error 1071 (42000): specified key was too long; max key length is 767 bytes

Source: Internet
Author: User

During the design of MySQL database fields today, the prompt is always prompted during table creation.MySQL:
Error 1071 (42000): specified key was too long; max key length is 767 bytes.At first, I didn't figure out why, because by default, MySQL restricts the length of the varchar type to between 0 and 65535. Later, I checked other attributes of this field carefully, if the unique attribute is removed, no such error message is returned, which complies with the 65535-byte length limit described in the official document.

I personally suspect that after the field is set to unique, MySQL will automatically establish the unique index on this field, and there will be System overhead for maintaining the unique index, so this problem will occur, on the one hand, a unique index is provided to avoid overhead issues caused by long index maintenance.

Another problem in MySQL is that UTF-8 occupies three bytes by default and the GBK encoding method occupies two bytes, therefore, if your character set is encoded in UTF-8 format, 767/3 = 255 characters can only be up to 255 characters, not 767 characters.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.