2015-04-24 47 views
3

我已經設置有外鍵InnoDB表,我想默認的字符集從latin1_swedish_ci這些表更改爲utf8_general_ci,我試圖運行下面的查詢:如何使用外鍵更改InnoDB表中的字符集?

ALTER TABLE test.PAYMENT DROP FOREIGN KEY PAYMENT_ibfk_1; 

但它給我這個錯誤:

ERROR 1025 (HY000): Error on rename of './test/#sql-1fa_24f43' to './test/PAYMENT' (errno: 150) 

我試過執行此設置foreign_key_checks=0;甚至通過禁用鍵,但仍然會得到相同的錯誤。

+0

SET FOREIGN_KEY_CHECKS = 0;完全適用於我的MySQL 5.6.19b,InnoDB表,從latin1轉換爲utf8,utf8_unicode_ci –

+1

@Vajk這很可能是因爲版本的差異,因爲我在MySQL-5.5.25上做了這個。感謝您提到它在MySQL-5.6中的工作 –

回答

1

我從所有的參考表和主表

alter table test.PAYMENT drop foreign key PAYMENT_ibfk_1; 
alter table test.ORDER_DETAILS drop foreign key ordr_dets_ibfk_1; 
alter table test.TRANSACTION drop foreign key transaction_ibfk_1; 

下降外鍵再後來改變了所有表和更改字符集

alter table test.PAYMENT CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; 
alter table test.TRANSACTION CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; 
alter table test.ORDER_DETAILS CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; 

然後再應用於外鍵

alter table niclient.PAYMENT add foreign key PAYMENT_ibfk_1 (paymentTransId) references `TRANSACTION` (`transId`) ON DELETE CASCADE; 
alter table niclient.ORDER_DETAILS add foreign key ordr_dets_ibfk_1 (ordrDetTransId) REFERENCES `TRANSACTION` (`transId`) ON DELETE CASCADE; 
alter table niclient.TRANSACTION add foreign key transaction_ibfk_1 (transCompId) REFERENCES `COMPANY_DETAILS` (`compId`) ON DELETE CASCADE; 
解決它

它完美地工作得很好:-)