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)?
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
it would let you store 32 multi-byte chars
http://dev.mysql.com/doc/refman/5.0/en/charset-unicode.html
It is better to use "char" for high-frequent update tables because the total data length of the row will be fixed and fast. Varchar columns make row data sizes dynamic. That's not good for MyISAM, but I don't know about InnoDB and others. For example, if you have a very narrow "type" column, it may be better to use char(2) with latin1 charset to only claim minimal space.
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.
http://dev.mysql.com/doc/refman/5.0/en/string-type-overview.html
Interestingly (I hadn't thought about it) the max length of a varchar column is affected by utf8 as follows:
If you connect to the database using latin1 encoding (for example with PHP) to save an PHP UTF8 string in an MySQL UTF8 column, you will have a double UTF8 encoding.
If the UTF8 string
$s
is 32 characters long but 64 bytes long and the column isVARCHAR(32)
UTF8, the double encoding will convert the string$s
to a 64 characters long UTF8 string that will be truncated in the database to its 32 first characters corresponding to the 32 first bytes of$s
. You may end up thinking that MySQL 5 behaves like MySQL 4 but it is in fact a second cause for the same effect.32 multibytes data for
varchar(32)
with collationutf8_unicode_ci
, I just tested with XAMPP.Get truncated to:
Keep in mind that these are not regular ASCII chars.