Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL VARCHAR Lengths and UTF-8

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)?

like image 823
Alix Axel Avatar asked Jan 04 '10 04:01

Alix Axel


People also ask

Can VARCHAR store UTF-8?

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.

What should be VARCHAR length in MySQL?

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.

Does VARCHAR need length MySQL?

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.

Is VARCHAR a 255?

The varchar, or variable length character field, is the most frequently used field type for storing text less than 256 characters in length.


1 Answers

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.

  • Version 4 counts bytes
  • Version 5 counts characters

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.

like image 66
M Brown Avatar answered Sep 19 '22 17:09

M Brown