如何将 MySQL 表更改为 UTF-8?
我知道表和数据库的语言有很多设置。
我已经创建了数据库。我相信当我创建它时,它是默认/拉丁语。我想更改所有内容 - 我的意思是...表和数据库都更改为 UTF-8。
我怎样才能做到这一点?谢谢。
I know there are many settings for a language for a table and a database.
I already created the database. I believe when I created it, it was default/LATIN. I want to change everything-I mean...both the table and the database, to UTF-8.
How can I do that? thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
查看使用alter命令更改字符集。
另一个有用的链接: http://dev.mysql.com/doc /refman/5.0/en/charset-table.html
一般形式是
和 对于表中的特定列
Have a look at Using alter command to change character set.
Another useful link: http://dev.mysql.com/doc/refman/5.0/en/charset-table.html
The general form is
and for a specific column in a table
aioobe 的答案告诉我们如何更改数据库、表或列的字符集。您应该记住,
设置表的字符集只是指定该表中新列的默认字符集。它不会更改预先存在的列的字符集;您必须单独执行这些列,或者如果您想将表中的每个字符串类型列更改为相同的字符集,您可以使用一个命令来执行此操作:“alter table ...转换为字符集”( http://dev.mysql.com/doc/refman/ 5.1/en/alter-table.html)
如果您已经在列中存储了错误编码的数据,那么使用“alter table ...修改”来更改列并不能完全解决问题问题。例如,如果您将 UTF-8 数据存储在 Latin1 列中,并且将字符集直接从 Latin1 更改为 UTF-8,那么之后它仍然会被错误编码。这可以通过通过二进制从 Latin-1 转换为 UTF-8 来解决。
aioobe's answer tells how to change the character set of a database, table or column. You should keep in mind that
setting the character set for a table just specifies the default character set for new columns in that table. It doesn't change the character set for preexisting columns; you have to do those columns individually, OR if you want to change every single string-type column in the table to the same character set there's a command you can use to do that: "alter table ... convert to character set" ( http://dev.mysql.com/doc/refman/5.1/en/alter-table.html )
if you already have data that is stored mis-encoded in a column, then using "alter table ... modify" to change the column will not quite solve the problem. For example, if you're been storing UTF-8 data in a Latin1 column and you change the character set directly from Latin1 to UTF-8, it'll still be mis-encoded afterwards. This can be worked around by converting from Latin-1 to UTF-8 via binary.
1) 数据库默认字符集和排序规则:
通过以下方式更改:
ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;
2) 表默认字符集和排序规则:
通过以下方式更改:
ALTER TABLE [table_name] CHARACTER SET utf8 COLLATE utf8_general_ci
3) 列字符集和排序规则:
通过以下方式更改:
ALTER TABLE [table_name] CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
第三个要求您禁用数据转换的外键检查。所以把这一切放在一起:
编辑:看看 这里改为
1) Database default character set and collation:
Altered via:
ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;
2) Table default character set and collation:
Altered via:
ALTER TABLE [table_name] CHARACTER SET utf8 COLLATE utf8_general_ci
3) Column character set and collation:
Altered via:
ALTER TABLE [table_name] CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
The third one requires that you disable foreign key checks for the data conversion. So putting this all together:
EDIT: look here instead
将以下内容添加到您的 my.cnf 中:
并重新启动 mysqld 守护进程。
添加:
和 my.cnf
Add to your my.cnf this:
And restart mysqld deamon.
ADDED:
and my.cnf