2012-07-25 39 views
11

我想用放弃一些外国键外键错误:的MySQL降152

ALTER TABLE `table` DROP FOREIGN KEY `fk_table_users1` , DROP FOREIGN KEY `fk_table_accounts1` , DROP FOREIGN KEY `fk_table_data1` ; 

但它返回的错误:

Error on rename of './db/table' to './db/#sql2-179c-288289' (errno: 152) 

我已经运行SHOW ENGINE INNODB STATUS它说:

120725 12:38:37 Error in dropping of a foreign key constraint of table db/table, 
in SQL command 
ALTER TABLE `table` DROP FOREIGN KEY `fk_table_users1` , DROP FOREIGN KEY `fk_table_accounts1` , DROP FOREIGN KEY `fk_table_data1` 
Cannot find a constraint with the given id fk_table_users1. 

SHOW CREATE TABLE 'table'输出:

CREATE TABLE `table` (
`id` int(11) NOT NULL auto_increment, 
`data_id` int(11) NOT NULL, 
`account_id` int(11) NOT NULL, 
`status` enum('pending','complete') NOT NULL default 'pending', 
`created_at` datetime NOT NULL, 
`created_by` int(11) NOT NULL, 
PRIMARY KEY (`id`), 
KEY `fk_orders_users1` (`created_by`), 
KEY `fk_orders_data1` (`data_id`), 
KEY `fk_orders_accounts1` (`account_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

但是,当我通过phpmyadmin查看结构时,它列出了具有相同名称的外键。在我可以放弃外键之前,我是否需要做其他事情?

+0

请寄出SHOW CREATE TABLE'table'的输出;''。 – eggyal 2012-07-25 12:09:09

+0

@eggyal编辑我的帖子与输出 – xylar 2012-07-25 12:14:38

+3

我没有看到*任何*外键约束那里... – eggyal 2012-07-25 12:20:35

回答

10

没有外键。请参阅MySQL documentation它说

KEY is normally a synonym for INDEX. 

所以基本上你已经创建了索引,没有外键的表。 For Foreign Key info, Click here

+0

小但可能很好:我有这个问题。使用这个答案,我仔细检查了自己,发现我试图删除约束所需的KEY。 'drop'语句中的一个小修复,将名称从键名更改为约束名 - 以及violla! – 2015-10-28 12:46:24

0

首次下降外键,然后删除列

ALTER TABLE '表名' 下拉外键“约束ID;

,如果你不知道约束ID在约束ID创建数据库转储是转储文件可利用的。

然后删除列..

0

索引名和约束名称不能相同。您应该首先使用代码删除约束:ALTER TABLE tablename DROP FOREIGN KEY constraintname

2

您需要临时删除约束,以便可以将其删除。

SET FOREIGN_KEY_CHECKS = 0;

,然后再开启你删除外键后:

SET FOREIGN_KEY_CHECKS = 0;

+0

你可能在最后一行代表'= 1';) – BusyAnt 2018-02-07 11:28:18