MySQL VARCHAR Lengths and UTF-8

Alix Axel picture Alix Axel · Jan 4, 2010 · Viewed 58.7k times · Source

In MySQL, if I create a new VARCHAR(32) field in a UTF-8 table does it means I can store 32 bytes of data in that field or 32 chars (multi-byte)?

Answer

M Brown picture M Brown · Sep 18, 2010

This answer showed up at the top of my google search results but wasn't correct so:

The confusion is probably due to different versions of mysql being tested.

  • Version 4 counts bytes
  • Version 5 counts characters

http://dev.mysql.com/doc/refman/5.0/en/string-type-overview.html

MySQL interprets length specifications in character column definitions in character units. (Before MySQL 4.1, column lengths were interpreted in bytes.) This applies to CHAR, VARCHAR, and the TEXT types.

Interestingly (I hadn't thought about it) the max length of a varchar column is affected by utf8 as follows:

The effective maximum length of a VARCHAR in MySQL 5.0.3 and later is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. For example, utf8 characters can require up to three bytes per character, so a VARCHAR column that uses the utf8 character set can be declared to be a maximum of 21,844 characters.