MySQL Error #1071 - Specified key was too long; max key length is 767 bytes

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for MySQL Error #1071 - Specified key was too long; max key length is 767 bytes

šŸ” MySQL Error #1071 - Key Too Long

šŸ” Common Issues: When working with MySQL, you may encounter the error message "#1071 - Specified key was too long; max key length is 767 bytes". This error often occurs when trying to add a unique key to a table using columns that have a combined length greater than the maximum allowed key length.

šŸ¤” Problem Explanation: Let's take a closer look at your specific scenario. You tried to execute the following command:

ALTER TABLE `mytable` ADD UNIQUE (
    `column1`,
    `column2`
);

The error message you received was:

#1071 - Specified key was too long; max key length is 767 bytes

You mentioned that column1 has a varchar length of 20 and column2 has a varchar length of 500. Considering the encoding used (utf8_general_ci), it seems like the combined length of these two columns should be 521 bytes, which is below the 767 bytes limit. So why did you encounter the error?

šŸ’” Solution: The key to understanding this issue lies in the encoding used by MySQL. When working with UTF-8 encoded characters, MySQL reserves more space for certain characters. The utf8_general_ci encoding allows for up to 3 bytes per character. Therefore, when calculating the total length of your columns, you need to account for the possibility of 3 bytes per character.

In your case, even though column1 has a varchar length of 20 (which should be 21 bytes), and column2 has a varchar length of 500 (which should be 501 bytes), the MySQL calculation takes into account the possibility of 3 bytes per character for each column.

Let's recalculate:

  • column1 can have 20 characters, equivalent to 20 * 3 = 60 bytes (less than the limit).

  • column2 can have 500 characters, equivalent to 500 * 3 = 1500 bytes (greater than the limit).

As a result, the combined length of your columns actually exceeds the maximum key length of 767 bytes.

To overcome this issue, you have a few possible solutions:

1ļøāƒ£ Solution 1: Reduce the Length of Columns If it's feasible for your use case, you can reduce the length of one or both columns (column1 and column2) so that the combined length fits within the 767 bytes limit. For example, you can change column2 to have a varchar length of 255.

2ļøāƒ£ Solution 2: Switch to a Different Encoding If reducing the length of columns is not an option, you can switch to a different character encoding that requires less space per character. For instance, you could use utf8mb4 encoding, which allows for up to 4 bytes per character. Keep in mind that this change can impact the storage requirements and performance of your database.

3ļøāƒ£ Solution 3: Use Hash Keys Instead If it's not critical to have a combined unique key for these two columns, you can consider using a hash key instead. This involves creating an additional column that holds the hash value of the concatenated values from column1 and column2. You can then create a unique index on this hash column.

šŸ”” Call-to-Action: If you found this blog post helpful, consider sharing it with your fellow developers who might be facing the same MySQL error. Leave a comment below if you have any questions or other MySQL-related topics you'd like us to cover.

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my