mysql修改表字符集

因爲建表時沒注意致使數據庫中有兩張表的字符集變成了默認的 latin1,想把它轉成utf8,因而使用如下命令:mysql

mysql> ALTER TABLE logistics_express_trajectory CONVERT TO CHARACTER SET utf8_general_ci;sql

ERROR 1115 (42000): Unknown character set: 'utf8_general_ci'數據庫

轉換失敗,推想多是表中有記錄不容許轉換,因而想重建表並指定字符集爲utf8,可是因爲是生產庫,表裏面的記錄不能直接drop,因而先把數據庫表結構及記錄導出:express

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

而後再輸入密碼便可導出,接着執行drop語句:this

mysql> drop table logistics_express_trajectory;spa

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

失敗,緣由大概是由於這個表與其它表有外建約束,因而想直接用delete語句把記錄清空再改字符集吧,因而:ci

mysql> delete from logistics_express_trajectory;get

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,能夠了,所有解決!

相關文章
相關標籤/搜索