标签:
This error occurs in MySQL when you are trying to create an index on a column that exceeds the maximum index length allowed.
To resolve this issue, you can do one of the following:
-
Use a Prefix Index: If you are creating an index on a VARCHAR column, you can specify a length for the index to reduce the key length. For example, instead of indexing the entire VARCHAR column, you can index the first few characters by specifying a length in the index definition.
CREATE TABLE example_table ( column_name VARCHAR(255) ); CREATE INDEX index_name ON example_table(column_name(191));
SQLIn this example, the index on the
column_name
is created with a maximum length of 191 characters (to fit within the 767-byte limit). -
Change the Character Set and Collation: If you are using a multi-byte character set like utf8mb4, you can reduce the maximum key length by changing to a single-byte character set like latin1.
ALTER DATABASE database_name CHARACTER SET = latin1;
SQLChanging the character set to latin1 will allow for longer index keys within the 767-byte limit.
-
Use InnoDB File Format Barracuda: If you are using InnoDB storage engine, you can change the innodb_large_prefix setting to allow longer index key lengths.
SET GLOBAL innodb_file_format=Barracuda; SET GLOBAL innodb_large_prefix=ON;
SQLThis will enable support for large index key prefixes in InnoDB tables.
Choose the method that best fits your requirements and system configuration to resolve the "Specified key was too long; max key length is 767 bytes" error in MySQL.
标签: 来源:
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。