说明:
5.7
使用的默认为utf8mb4_unicode_ci
创建存储过程
CREATE DEFINER=`root`@`localhost` PROCEDURE `alter_table_character`() begin declare f_name varchar(100); declare b int default 0; /*是否达到记录的末尾控制变量*/ -- 注意修改下面的数据库名称 populac declare table_name cursor for SELECT TABLE_NAME FROM information_schema.TABLES where TABLE_SCHEMA = 'populac' and TABLE_NAME like '%' AND TABLE_COLLATION = 'utf8mb4_unicode_ci'; DECLARE CONTINUE HANDLER FOR NOT FOUND SET b = 1; OPEN table_name; REPEAT FETCH table_name INTO f_name; /*获取第一条记录*/ SET @STMT :=CONCAT("ALTER TABLE ",f_name," CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;"); PREPARE STMT FROM @STMT; EXECUTE STMT; -- INSERT into TestTable(name) VALUES (f_name); -- ALTER TABLE f_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; UNTIL b = 1 END REPEAT; close table_name; end
执行存储过程:
call alter_table_character();