mysql中修改表的默认编码和表中字段的编码

ALTER TABLE `table` DEFAULT CHARACTER SET utf8;

但是虽然修改了表的编码格式,但是字段的编码格式并没有修改过来,没有什么卵用

又发现一条语句,作用是修改字段的编码格式

ALTER TABLE `tablename` CHANGE `字段名1` `字段名2` VARCHAR(36) CHARACTER SET utf8 NOT NULL; 

 

最后找到这么一条语句

alter table `tablename` convert to character set utf8; 

 

它可以修改一张表的所有字段的编码格式,顿时方便多了

 

Ps :延伸一下,修改mysql中所有表,所有字段编码,需要用到存储过程

 

转换表字段编码的sql语句为:alter table `tableName` convert to character set utf8'

CREATE PROCEDURE `pr_convert_dbtab_utf8`(IN dbName varchar(100))

BEGIN

declare stop int default 0;

declare tabCount int default 0;

declare strSql varchar(1000);

declare name varchar(100);

declare cur CURSOR FOR select table_name from information_schema.tables where table_schema=dbName;

declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop = null;

OPEN cur;

FETCH cur INTO name;

WHILE ( stop is not null) DO

set tabCount=tabCount+1;

set strSql = concat('alter table `',name,'` convert to character set utf8');

set @sql1 = strSql;

prepare stmt_p from @sql1;

execute stmt_p;

FETCH cur INTO name;

END WHILE;

CLOSE cur;

SELECT concat('table: ', tabCount);

END;

 

输入调用存储过程的命令,参数为需要转换的数据库名

mysql> call pr_convert_dbtab_utf8('jiradb');

+-----------------------------+

| concat('table: ', tabCount) |

+-----------------------------+

| table: 245 |

+-----------------------------+

1 row in set

Query OK, 0 rows affected

mysql>

posted @ 2018-09-03 11:51  Alan-lee  阅读(7766)  评论(0编辑  收藏  举报