将5.x版本的字符集转为8.0版本默认的utf8mb4_0900_ai_ci


说明:

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();

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM