How to change the default charset of a MySQL table?

自闭症网瘾萝莉.ら 提交于 2019-11-27 10:50:17

If you want to change the table default character set and all character columns to a new character set, use a statement like this:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;

So query will be:

ALTER TABLE etape_prospection CONVERT TO CHARACTER SET utf8;

Change table's default charset:

ALTER TABLE etape_prospection
  CHARACTER SET utf8,
  COLLATE utf8_general_ci;

To change string column charset exceute this query:

ALTER TABLE etape_prospection
  CHANGE COLUMN etape_prosp_comment etape_prosp_comment TEXT CHARACTER SET utf8 COLLATE utf8_general_ci;

The ALTER TABLE MySQL command should do the trick. The following command will change the default character set of your table and the character set of all its columns to UTF8.

ALTER TABLE etape_prospection CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

This command will convert all text-like columns in the table to the new character set. Character sets use different amounts of data per character, so MySQL will convert the type of some columns to ensure there's enough room to fit the same number of characters as the old column type.

I recommend you read the ALTER TABLE MySQL documentation before modifying any live data.

Joni

You can change the default with an alter table set default charset but that won't change the charset of the existing columns. To change that you need to use a alter table modify column.

Changing the charset of a column only means that it will be able to store a wider range of characters. Your application talks to the db using the mysql client so you may need to change the client encoding as well.

If someone is searching for a complete solution for changing default charset for all database tables and converting the data, this could be one:

DELIMITER $$

CREATE PROCEDURE change_character_set(in charset VARCHAR(64), in collation VARCHAR(64))
BEGIN
DECLARE done BOOLEAN DEFAULT false;
DECLARE tab_name VARCHAR(64);
DECLARE charset_cursor CURSOR FOR 
    SELECT table_name FROM information_schema.tables
    WHERE table_schema = DATABASE();
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

SET foreign_key_checks = 0;
OPEN charset_cursor;
change_loop: LOOP
FETCH charset_cursor INTO tab_name;
IF done THEN
    LEAVE change_loop;
END IF;
SET @alter = concat('ALTER TABLE ', tab_name,
                    ' CONVERT TO CHARACTER SET ', charset,
                    ' COLLATE ', collation , ';');
SELECT @alter;
PREPARE stmt FROM @alter;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP change_loop;
CLOSE charset_cursor;
SET foreign_key_checks = 1;
END$$

DELIMITER ;

You can place this code inside the file e.g. chg_char_set.sql and execute it e.g. by calling it from MySQL terminal:

mysql> source ~/path-to-the-file/chg_char_set.sql

Then call defined procedure with desired input parameters e.g.

mysql> change_character_set('utf8mb4', 'utf8mb4_general_ci');
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!