以下create语句显示了我的一个MariaDB表的当前结构。
CREATE TABLE `councilor` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`user` BIGINT(20) NOT NULL,
`council` INT(11) NOT NULL,
`role` CHAR(50) NOT NULL DEFAULT 'member' COLLATE 'utf8_persian_ci',
`cDate` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`uDate` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`startDate` DATETIME NULL DEFAULT NULL,
`endDate` DATETIME NULL DEFAULT NULL,
`responsibility` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_persian_ci',
`details` TEXT NULL COLLATE 'utf8_persian_ci',
PRIMARY KEY (`id`),
UNIQUE INDEX `userId_councilId` (`user`, `council`),
INDEX `council_user_council_foreign` (`council`)
)
COLLATE='utf8_persian_ci'
ENGINE=InnoDB
ROW_FORMAT=COMPACT
AUTO_INCREMENT=11;
我需要删除唯一索引,但是尝试删除两个键中的任何一个都会失败。
ALTER TABLE `councilor`
DROP INDEX `userId_councilId`;
/ * SQL错误(1553):无法删除索引'userId_councilId':外键约束中需要* /
ALTER TABLE `councilor`
DROP INDEX `council_user_council_foreign`;
/ * SQL错误(1553):无法删除索引'council_user_council_foreign':外键约束中需要* /
删除所有表的行也无济于事。但是,如果上述脚本创建了新表,则不会发生此问题。
答案 0 :(得分:6)
18:18:46 ALTER TABLE location DROP INDEX unique_name_to_supplier错误 代码:1553。无法删除索引'unique_name_to_supplier':a中需要 外键约束0.063秒
这意味着您正在尝试删除用于FK的索引。
MySQL要求外键和引用键上的索引 外键检查可以很快,不需要表扫描。在里面 引用表时,必须有一个索引所在的外键 列以相同顺序列为第一列。这样的 如果不是,则会自动在引用表上创建索引 存在。如果您创建,可能会稍后以静默方式删除此索引 另一个可用于强制执行外键约束的索引。 如果给定,则使用index_name,如前所述。
http://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html
因此需要修复:
1.取消FK
2.删除索引本身
3.再次创建FK
以下是命令:
#removing FK
ALTER TABLE `location` DROP FOREIGN KEY `FK_SUPPLIER`;
#droping unique Index
ALTER TABLE `location` DROP INDEX `unique_name_to_supplier` ;
#creating new FK with proper Index
ALTER TABLE `location`
ADD INDEX `FK_SUPPLIER_idx` (`supplier_id` ASC);
ALTER TABLE `location`
ADD CONSTRAINT `FK_SUPPLIER`
FOREIGN KEY (`supplier_id`)
REFERENCES `supplier` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
完整的SQL日志:
mysql>
mysql> CREATE TABLE supplier (
-> id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.38 sec)
mysql> CREATE TABLE location (
-> id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> name VARCHAR(10) NOT NULL,
-> supplier_id INT UNSIGNED NOT NULL
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.32 sec)
mysql> ALTER TABLE location ADD CONSTRAINT FK_SUPPLIER FOREIGN KEY FK_SUPPLIER (supplier_id)
-> REFERENCES supplier (id) ON DELETE RESTRICT ON UPDATE RESTRICT;
Query OK, 0 rows affected (0.41 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE location ADD UNIQUE INDEX unique_name_to_supplier (supplier_id,name);
Query OK, 0 rows affected (0.24 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table location;
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| location | CREATE TABLE `location` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL,
`supplier_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_name_to_supplier` (`supplier_id`,`name`),
CONSTRAINT `FK_SUPPLIER` FOREIGN KEY (`supplier_id`) REFERENCES `supplier` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
mysql>
mysql>
mysql>
mysql> ALTER TABLE `location` DROP FOREIGN KEY `FK_SUPPLIER`;
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> ALTER TABLE `location` DROP INDEX `unique_name_to_supplier` ;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> ALTER TABLE `location`
-> ADD INDEX `FK_SUPPLIER_idx` (`supplier_id` ASC);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE `location`
-> ADD CONSTRAINT `FK_SUPPLIER`
-> FOREIGN KEY (`supplier_id`)
-> REFERENCES `supplier` (`id`)
-> ON DELETE NO ACTION
-> ON UPDATE NO ACTION;
Query OK, 0 rows affected (0.68 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table location
-> ;
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| location | CREATE TABLE `location` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL,
`supplier_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_SUPPLIER_idx` (`supplier_id`),
CONSTRAINT `FK_SUPPLIER` FOREIGN KEY (`supplier_id`) REFERENCES `supplier` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
答案 1 :(得分:5)
首先必须删除FOREIGN KEY约束,然后删除索引并重新创建FOREIGN键。
答案 2 :(得分:4)
您必须首先删除外键约束
ALTER TABLE mytable DROP FOREIGN KEY [Foreign_key_constraint_name];
并且可以删除索引