MySql varchar change from Latin1 to UTF8

感情迁移 提交于 2019-12-04 16:10:16

MySQL handles this nicely:

CREATE TEMPORARY TABLE t1 (
  c VARCHAR(10)
) CHARACTER SET ="latin1";

INSERT INTO t1 VALUES ("æøå");
SELECT * FROM t1; # 'æøå'

ALTER TABLE t1 CHARACTER SET = "utf8";
SELECT * FROM t1; # 'æøå'

DROP TEMPORARY TABLE t1;

EDIT: And there are no latin-1 characters that cannot be stored as utf-8, so you shouldn't get any dataloss

There shouldn't be any problems on the database front. MySQL will handle the conversion of data between encodings at the time you make the change.

There are other things you need to be aware of when you add support for UTF-8 to your website.

  • For best results, you should make sure that connections to the database are using UTF-8 as the connection character set. You can do this by issuing SET NAMES utf8 when you make the connection.
  • You need to make sure that your web pages themselves are declared to the browser as encoded in UTF-8, by changing the meta tag.
  • Security considerations: You should check user input to make sure it is valid UTF-8, to avoid the possibility of cross-site scripting (XSS) attacks
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!