31

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.

0

5 Answers 5

60
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
2
  • I don't think this answer is complete. From my understanding, this will attempt to convert all the columns to the given character set, but may or may not change the table default. It also doesn't address the database default. I'll post my approach as a separate answer
    – Roly
    Dec 1, 2017 at 14:27
  • 2
    Just to clarify, this command does change the table default but does not affect the database default.
    – Mike
    Jul 31, 2018 at 8:12
6

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

1
  • Developer-Sid 's answer, plus this answer, form a complete answer
    – chiliNUT
    Apr 23, 2018 at 22:18
4

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.

3
  • 3
    what exactly do you mean by "via binary"? Nov 25, 2016 at 15:48
  • it means converting to something like BLOB first then converting to UTF-8 (or whatever) because changing to binary doesn't do any kind of conversion
    – Roly
    Dec 1, 2017 at 14:13
  • After I converted my database through binary now in phpmyadmin all "client name" fields show as "[blob 11b]" instead of the client name. Even for those records that are plain ascii. Now what?
    – rew
    Sep 4, 2020 at 14:38
2

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

-2

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;
6
  • This won't change character set of the existing databases or tables May 25, 2010 at 9:18
  • Yes, but it's at least he solved the character problem at all. Change db/table chars he could simply with phpMyAdmin...
    – mosg
    May 25, 2010 at 9:22
  • provided he has php/httpd/phpMyAdmin installed.
    – aioobe
    May 25, 2010 at 9:24
  • Nope none of that stuff. I just have a basic database and tables. What do I type? It should be pretty simple, right?
    – TIMEX
    May 25, 2010 at 9:29
  • @Alexander I just gave one of the solutions, you are not... Maybe it's not the best one, but still one of the answers :)
    – mosg
    May 25, 2010 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.