程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> MySQL刪除外鍵問題小結

MySQL刪除外鍵問題小結

編輯:關於MYSQL數據庫

MySQL: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

刪除外鍵的做法:

//先刪除外鍵名,再刪外鍵字段(mysql官方提供)

例:

alter table task drop foreign key FK_Reference_7;
alter table task drop column `assigned`;

以上所述是小編給大家介紹的MySQL刪除外鍵問題小結,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網站的支持!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved