如何将 MySQL 表更改为 UTF-8?

发布于 2024-09-02 10:14:26 字数 137 浏览 3 评论 0原文

我知道表和数据库的语言有很多设置。

我已经创建了数据库。我相信当我创建它时,它是默认/拉丁语。我想更改所有内容 - 我的意思是...表和数据库都更改为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怂人 2024-09-09 10:14:26
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
可可 2024-09-09 10:14:26

查看使用alter命令更改字符集

另一个有用的链接: http://dev.mysql.com/doc /refman/5.0/en/charset-table.html

一般形式是

ALTER DATABASE db_name
    [[DEFAULT] CHARACTER SET charset_name]
    [[DEFAULT] COLLATE collation_name]

和 对于表中的特定列

ALTER TABLE column COLLATE utf8_general_ci

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

ALTER DATABASE db_name
    [[DEFAULT] CHARACTER SET charset_name]
    [[DEFAULT] COLLATE collation_name]

and for a specific column in a table

ALTER TABLE column COLLATE utf8_general_ci

请远离我 2024-09-09 10:14:26

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.

魂归处 2024-09-09 10:14:26

1) 数据库默认字符集和排序规则:

SELECT @@character_set_database, @@collation_database;

通过以下方式更改:ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;

2) 表默认字符集和排序规则:

SELECT T.table_name, CCSA.character_set_name 
FROM information_schema.TABLES T, information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA 
WHERE CCSA.collation_name = T.table_collation AND T.table_schema = "YOUR_DB";`

通过以下方式更改:ALTER TABLE [table_name] CHARACTER SET utf8 COLLATE utf8_general_ci

3) 列字符集和排序规则:

SELECT c.TABLE_NAME, c.COLUMN_NAME, c.CHARACTER_SET_NAME, c.COLLATION_NAME 
FROM information_schema.COLUMNS c
WHERE c.table_schema = "YOUR_DB";`

通过以下方式更改:ALTER TABLE [table_name] CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

第三个要求您禁用数据转换的外键检查。所以把这一切放在一起:

DELIMITER //
CREATE PROCEDURE migrate_charset_to_utf8()
  BEGIN
    DECLARE done TINYINT DEFAULT 0;
    DECLARE curr_table VARCHAR(64);

    DECLARE table_cursor CURSOR FOR
    SELECT T.table_name
      FROM information_schema.TABLES T
      WHERE T.TABLE_TYPE = 'BASE TABLE' AND
        T.TABLE_SCHEMA = 'YOUR_DB';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

    OPEN table_cursor;

    table_loop: LOOP
      FETCH table_cursor INTO curr_table;
      IF done THEN
        LEAVE table_loop;
      END IF;

      # Convert table data(columns) charset
      SET @sql_str1 = CONCAT("ALTER TABLE ", curr_table, " CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
      PREPARE stmt1 FROM @sql_str1;
      EXECUTE stmt1;
      DEALLOCATE PREPARE stmt1;

      # Set table's default charset e.g for new columns added
      SET @sql_str2 = CONCAT("ALTER TABLE ", curr_table, " CHARACTER SET utf8 COLLATE utf8_general_ci");
      PREPARE stmt2 FROM @sql_str2;
      EXECUTE stmt2;
      DEALLOCATE PREPARE stmt2;

    END LOOP table_loop;

    CLOSE table_cursor;
  END//

DELIMITER ;


SET @@FOREIGN_KEY_CHECKS = 0;
CALL migrate_charset_to_utf8();
SET @@FOREIGN_KEY_CHECKS = 1;

ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;

编辑:看看 这里改为

1) Database default character set and collation:

SELECT @@character_set_database, @@collation_database;

Altered via: ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;

2) Table default character set and collation:

SELECT T.table_name, CCSA.character_set_name 
FROM information_schema.TABLES T, information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA 
WHERE CCSA.collation_name = T.table_collation AND T.table_schema = "YOUR_DB";`

Altered via: ALTER TABLE [table_name] CHARACTER SET utf8 COLLATE utf8_general_ci

3) Column character set and collation:

SELECT c.TABLE_NAME, c.COLUMN_NAME, c.CHARACTER_SET_NAME, c.COLLATION_NAME 
FROM information_schema.COLUMNS c
WHERE c.table_schema = "YOUR_DB";`

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:

DELIMITER //
CREATE PROCEDURE migrate_charset_to_utf8()
  BEGIN
    DECLARE done TINYINT DEFAULT 0;
    DECLARE curr_table VARCHAR(64);

    DECLARE table_cursor CURSOR FOR
    SELECT T.table_name
      FROM information_schema.TABLES T
      WHERE T.TABLE_TYPE = 'BASE TABLE' AND
        T.TABLE_SCHEMA = 'YOUR_DB';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

    OPEN table_cursor;

    table_loop: LOOP
      FETCH table_cursor INTO curr_table;
      IF done THEN
        LEAVE table_loop;
      END IF;

      # Convert table data(columns) charset
      SET @sql_str1 = CONCAT("ALTER TABLE ", curr_table, " CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
      PREPARE stmt1 FROM @sql_str1;
      EXECUTE stmt1;
      DEALLOCATE PREPARE stmt1;

      # Set table's default charset e.g for new columns added
      SET @sql_str2 = CONCAT("ALTER TABLE ", curr_table, " CHARACTER SET utf8 COLLATE utf8_general_ci");
      PREPARE stmt2 FROM @sql_str2;
      EXECUTE stmt2;
      DEALLOCATE PREPARE stmt2;

    END LOOP table_loop;

    CLOSE table_cursor;
  END//

DELIMITER ;


SET @@FOREIGN_KEY_CHECKS = 0;
CALL migrate_charset_to_utf8();
SET @@FOREIGN_KEY_CHECKS = 1;

ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;

EDIT: look here instead

疯了 2024-09-09 10:14:26

将以下内容添加到您的 my.cnf 中:

[mysqld]
character-set-server=utf8
default-collation=utf8_unicode_ci

并重新启动 mysqld 守护进程。

添加:

ALTER DATABASE your_base_name CHARACTER SET utf8 COLLATE utf8_unicode_ci;

和 my.cnf

SET collation_connection     = utf8_unicode_ci;
SET character_set_results    = utf8;
SET character_set_connection = utf8;
SET character_set_client     = utf8;

Add to your my.cnf this:

[mysqld]
character-set-server=utf8
default-collation=utf8_unicode_ci

And restart mysqld deamon.

ADDED:

ALTER DATABASE your_base_name CHARACTER SET utf8 COLLATE utf8_unicode_ci;

and my.cnf

SET collation_connection     = utf8_unicode_ci;
SET character_set_results    = utf8;
SET character_set_connection = utf8;
SET character_set_client     = utf8;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文