I know there have been plenty of questions about this, but I think my math is right.
- MySQL reserves 3 bytes per UTF8 character.
- MyISAM allows keys of length 1000 bytes.
- My UTF8 VARCHAR(255) should be
255 * 3 = 765
bytes
Unless UNQUE requires an extra 200+ bytes per entry, why doesn't this work?
mysql> ALTER TABLE entry ADD UNIQUE INDEX (name(255));
ERROR 1071 (42000): Specified key was too long; max key length is 1000 bytes
Is there anything I can do about this?
EDIT:
It turns out the limit is 250. It seems chars count as 4 bytes for unique indices, but I don't know why.
EDIT 2:
Thanks Vladislav Vaintroub, the charset is indeed utf8mb4. That solves the mystery. I hadn't seen any documentation on this change.
I'm guessing it builds the non unique index by implicitly truncating the field, which is unacceptable for unique indices so it refuses.
If you re-enter your comment as an answer I'd be happy to accept it.
Solution: Specify utf8, not utf8mb4 (MySQL Admin doesn't allow this, so create the table manually)