Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL删除外键错误152_Mysql_Foreign Keys_Alter Table - Fatal编程技术网

MySQL删除外键错误152

MySQL删除外键错误152,mysql,foreign-keys,alter-table,Mysql,Foreign Keys,Alter Table,我正在尝试使用以下方法删除多个外键: 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 ENGI

我正在尝试使用以下方法删除多个外键:

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.
显示创建表格“表格”
输出:

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查看结构时,它列出了具有相同名称的外键。在放下外键之前,是否需要执行其他操作?

没有外键。指的是

KEY is normally a synonym for INDEX.

所以,基本上在表中创建的是索引,而不是外键

首先删除外键,然后删除列

更改表'table name'drop foreign key'约束id

如果您不知道约束id,请在中创建数据库转储,该约束id在转储文件中可用


然后删除列。

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

您需要临时删除约束,以便删除它

设置外键检查=0

然后在放下外键后再次打开:


设置外键检查=1

请发布
SHOW CREATE TABLE`TABLE`的输出
@eggyal用outputI编辑了我的文章,我看不到任何外键约束……感谢您指出这一点。我们使用了一个迁移脚本,看起来它没有添加外键,这是一个很小但可能很好的外键:我遇到了这个问题。使用这个答案,我仔细检查了自己,发现我试图删除约束所需的键。“drop”语句中的一个小补丁,用于将名称从键名称更改为约束名称-还有Viola!您的意思可能是最后一行中的
=1