When to use utf-8 and when to use latin1 in MySQL?

后端 未结 8 1672
暖寄归人
暖寄归人 2021-02-13 21:56

I know that MySQL has default of latin1 encoding and apparently it takes 1 byte to store a character in latin1 and 3 bytes to store a character in

8条回答
  •  独厮守ぢ
    2021-02-13 22:10

    Current best practice is to never use MySQL's utf8 character set. Use utf8mb4 instead, which is a proper implementation of the standard.

    See Adam Hooper's Explanation for more detail.

    Note that in utf8mb4, characters have a variable number of bytes. As the name implies, characters are up to four bytes. For characters in the the latin character set, encoded as utf8mb4, they still occupy only one byte. Other characters, including those with accents, Kanji, and emoji's require two, three, or four bytes to store.

    The Specified key was too long; max key length is 1000 bytes error occurs when an index contains columns in utf8mb4 because the index may be over this limit. You'll need to shorten the column length of some character columns or shorten the length of the index on the columns using this syntax to ensure that it is shorter than the limit.

    ALTER TABLE.. ADD INDEX `myIndex` ( column1(15), column2(200) );

提交回复
热议问题