Wednesday, 14 November 2018

Mysql: How to change the default collation of a database?

Our previous programmer set the wrong collation in a table (Mysql). 
He set it up with Latin collation, when it should be UTF8, and now I have issues. 
Every record with Chinese and Japan character turn to ??? character.
Is possible to change collation and get back the detail of character?

 Answers


change database collation:
ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;
change table collation:
ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
change column collation:
ALTER TABLE <table_name> MODIFY <column_name> VARCHAR(255) 



Beware that in Mysql, the utf8 character set is only a subset of the real UTF8 character 
set. In order to save one byte of storage, the Mysql team decided to store only three bytes 
of a UTF8 characters instead of the full four-bytes. That means that some east asian
 language and emoji aren't fully supported. To make sure you can store all UTF8 characters, 
use the utf8mb4 data type, and utf8mb4_bin or utf8mb4_general_ci in Mysql.



here describes the process well. However, some of the characters that didn't fit in latin 
space are gone forever. UTF-8 is a SUPERSET of latin1. Not the reverse. Most will fit in 
single byte space, but any undefined ones will not (check a list of latin1 - not all 256 
characters are defined, depending on mysql's latin1 definition)

0 comments:

Post a Comment