mysql修改表字符集

由于建表时没注意导致数据库中有两张表的字符集变成了默认的 latin1,想把它转成utf8,于是使用以下命令:

mysql> ALTER TABLE logistics_express_trajectory CONVERT TO CHARACTER SET utf8_general_ci;

ERROR 1115 (42000): Unknown character set: 'utf8_general_ci'

转换失败,推想可能是表中有记录不允许转换,于是想重建表并指定字符集为utf8,但是由于是生产库,表里面的记录不能直接drop,于是先把数据库表结构及记录导出:

mysqldump -u logistics -p logistics logistics_express_trajectory > /home/logistics/logistics_express_trajectory.sql

然后再输入密码即可导出,接着执行drop语句:

mysql> drop table logistics_express_trajectory;

ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails

失败,原因大概是因为这个表与其它表有外建约束,于是想直接用delete语句把记录清空再改字符集吧,于是:

mysql> delete from logistics_express_trajectory;

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`logistics`.`logistics_order_trajectory`, CONSTRAINT `Reflogistics_express_trajectory11` FOREIGN KEY (`trajectory_id`) REFERENCES `logistics_express_trajectory` (`id`))

还是失败,还是因为表被其它表关联,不能删除记录。根据外键名称找到对应的表:

use INFORMATION_SCHEMA;

 select table_name, column_name from KEY_COLUMN_USAGE where CONSTRAINT_NAME = 'Reflogistics_express_trajectory11';

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

| table_name                 | column_name   |

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

| logistics_order_trajectory | trajectory_id |

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

表名为logistics_order_trajectory,正好也要修改改该表的字符集,于是干脆也导出该表的记录并重建:

mysqldump -u logistics -p logistics logistics_order_trajectory > /home/logistics/logistics_order_trajectory.sql

然后再输入密码即可导出,接着按顺序执行drop语句:

mysql> use logistics;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A


Database changed

mysql> drop table logistics_order_trajectory;

Query OK, 0 rows affected (0.04 sec)


mysql> drop table logistics_express_trajectory;

Query OK, 0 rows affected (0.04 sec)

两张表都drop成功,然后再修改导出文件中建表的字符集为utf8,再按顺序导入两张表的结构及记录即可(被依赖的表要先导噢)

source /home/logistics/logistics_express_trajectory.sql;

source /home/logistics/logistics_order_trajectory.sql;

ok,成功,再使用语句查看两张表的字符集看是否修改过来了:

mysql> SELECT table_name, table_type, engine,TABLE_COLLATION FROM information_schema.tables where table_name='logistics_express_trajectory' or table_name='logistics_order_trajectory';

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

| table_name                   | table_type | engine | TABLE_COLLATION |

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

| logistics_express_trajectory | BASE TABLE | InnoDB | utf8_general_ci |

| logistics_order_trajectory   | BASE TABLE | InnoDB | utf8_general_ci |

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

已经修改过来了,接着再补上外键约束:

ALTER TABLE logistics_express_trajectory ADD CONSTRAINT Reflogistics_member_express7 

    FOREIGN KEY (member_express_id)

    REFERENCES logistics_member_express(id)

;

mysql> ALTER TABLE logistics_order ADD CONSTRAINT Reflogistics_member_express8 

    ->     FOREIGN KEY (member_express_id)

    ->     REFERENCES logistics_member_express(id)

    -> ;

ERROR 1005 (HY000): Can't create table 'logistics.#sql-d971_11498' (errno: 121)

mysql> ALTER TABLE logistics_order_trajectory ADD CONSTRAINT Reflogistics_express_trajectory11 

    ->     FOREIGN KEY (trajectory_id)

    ->     REFERENCES logistics_express_trajectory(id)

    -> ;

ERROR 1005 (HY000): Can't create table 'logistics.#sql-d971_11498' (errno: 121)

擦,执行失败!下次再解决吧,出去陪老婆了!

好了,回来了,继续解决吧。

因为logistics_order和logistics_order_trajectory表虽然被drop掉了,但并没有把之前建表时建立的外键约束删除掉,所以怀疑是不是因为外键名称重名冲突了,于是用以下语句根据约束名查找看是否有对应的记录存在:

use INFORMATION_SCHEMA;

mysql> select table_name, column_name from KEY_COLUMN_USAGE where CONSTRAINT_NAME = 'Reflogistics_member_express8';

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

| table_name      | column_name       |

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

| logistics_order | member_express_id |

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

1 row in set (0.01 sec)

mysql> select table_name, column_name from KEY_COLUMN_USAGE where CONSTRAINT_NAME = 'Reflogistics_express_trajectory11';

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

| table_name                 | column_name   |

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

| logistics_order_trajectory | trajectory_id |

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

1 row in set (0.01 sec)

果然都存在,drop掉再重建就好了

mysql> ALTER TABLE logistics_order DROP FOREIGN KEY  Reflogistics_member_express8;

Query OK, 8 rows affected (0.27 sec)

Records: 8  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE logistics_order ADD CONSTRAINT Reflogistics_member_express8  FOREIGN KEY (member_express_id) REFERENCES logistics_member_express(id);

Query OK, 8 rows affected (0.27 sec)

Records: 8  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE logistics_order_trajectory DROP FOREIGN KEY Reflogistics_express_trajectory11;

Query OK, 5 rows affected (0.26 sec)

Records: 5  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE logistics_order_trajectory ADD CONSTRAINT Reflogistics_express_trajectory11 FOREIGN KEY (trajectory_id) REFERENCES logistics_express_trajectory(id);

Query OK, 5 rows affected (0.26 sec)

Records: 5  Duplicates: 0  Warnings: 0

OK,可以了,全部解决!

你可能感兴趣的:(mysql修改表字符集)