Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

character encoding - Is this a safe way to convert MySQL tables from latin1 to utf-8?

I need to change all the tables in one of my databases from latin1 to utf-8 (with utf8_bin collation).

I have dumped the database, created a test database from it, and run the following without any errors or warnings for each table:

ALTER TABLE tablename CONVERT TO CHARSET utf8 COLLATION utf8_bin

Is it safe for me to repeat this on the real database? The data seems fine by inspection...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are 3 different cases to consider:

The values are indeed encoded using Latin1

This is the consistent case: declared charset and content encoding match. This was the only case I covered in my initial answer.

Use the command you suggested:

ALTER TABLE tablename CONVERT TO CHARSET utf8 COLLATE utf8_bin

Note that the CONVERT TO CHARACTER SET command only appeared in MySQL 4.1.2, so anyone using a database installed before 2005 had to use an export/import trick. This is why there are so many legacy scripts and document on Internet doing it the old way.

The values are already encoded using utf8

In this case, you don't want mysql to convert any data, you only need to change the column's metadata.

For this, you have to change the type to BLOB first, then to TEXT utf8 for each column, so that there are no value conversions:

ALTER TABLE t1 CHANGE c1 c1 BLOB;
ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8

This is the recommended way, and it is explicitely documented in Alter Table Syntax Documentation.

The values use in a different encoding

The default encoding was Latin1 for several years on a some Linux distributions. In this case, you have to use a combination of the two techniques:

  • Fix the table meta-data, using the BLOB type trick
  • Convert the values using CONVERT TO.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...