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)?
UTF-8 is allowed in the varchar datatypes and is enabled when creating or changing an object's collation to a collation with the UTF8 suffix. This helps in minimizing character conversion issues. UTF-8 support for varchar data type provides substantial storage savings depending on the character set in use.
The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.
MySQL VARCHAR is the variable-length string whose length can be up to 65,535. MySQL stores a VARCHAR value as a 1-byte or 2-byte length prefix plus actual data. The length prefix specifies the number of bytes in the value. If a column requires less than 255 bytes, the length prefix is 1 byte.
The varchar, or variable length character field, is the most frequently used field type for storing text less than 256 characters in length.
This answer showed up at the top of my google search results but wasn't correct.
The confusion is probably due to different versions of MySQL being tested.
Here is the quote from the official MySQL 5 documentation:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With