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