1. 程式人生 > >MySQL不能刪除外來鍵,抱錯Error on rename of './ruler/test2child' to './ruler/#sql2-298-92' (errno: 152)

MySQL不能刪除外來鍵,抱錯Error on rename of './ruler/test2child' to './ruler/#sql2-298-92' (errno: 152)

曾經這是MySQL的一個bug,但是現在只是MySQL的一個不友好的地方。
例子如下:

mysql> ALTER TABLE ruler.test2child DROP FOREIGN KEY test;
ERROR 1025 : Error on rename of './ruler/test2child' to './ruler/#sql2-298-8f' (errno:
152)

    這裡並不是不能刪除外來鍵,而是使用了錯誤的外來鍵名稱,但是MySQL報的錯實在離譜。
正確的錯誤資訊要如下查詢才能知道:
mysql> show innodb status;
------------------------
LATEST FOREIGN KEY ERROR
------------------------
071222 20:48:26 Error in dropping of a foreign key constraint of table
"ruler"."test2child",
in SQL command
ALTER TABLE ruler.test2child DROP FOREIGN KEY test
Cannot find a constraint with the given id "test".

    另外外來鍵的其他操作也有類似情況:
比如外來鍵 新建報錯:
Can't create table 'ruler.#sql-298_92' (errno: 150)

show innodb status;
------------------------
LATEST FOREIGN KEY ERROR
------------------------
071222 20:18:57 Error in foreign key constraint of table ruler/#sql-298_8d:
 FOREIGN KEY (test2code) REFERENCES ruler.test2 (test) on delete set null:
You have defined a SET NULL condition though some of the
columns are defined as NOT NULL.



測試資料庫版本為:

5.2.0-falcon-alpha-community-nt