DROP CONSTRAINT无法找到约束

时间:2019-02-18 18:08:06

标签: mysql sql mariadb

我尝试放下约束:

USE `mydb`;

BEGIN;
ALTER TABLE `mydb` DROP CONSTRAINT `myconstraint`;
COMMIT;

它回复:

ERROR 1091 (42000) at line 6: Can't DROP CONSTRAINT `myconstraint`; check that it exists

但是约束存在:

MariaDB [(mydb)]> select * from information_schema.table_constraints WHERE table_name = 'mytable';
+--------------------+-------------------+---------------------------------------------------------------+--------------+-----------------+-----------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME                                               | TABLE_SCHEMA | TABLE_NAME      | CONSTRAINT_TYPE |
+--------------------+-------------------+---------------------------------------------------------------+--------------+-----------------+-----------------+
| def                | mydb              | PRIMARY                                                       | mydb         | mytable         | PRIMARY KEY     |
| def                | mydb              | name                                                          | mydb         | mytable         | UNIQUE          |
| def                | mydb              | myconstraint                                                  | mydb         | mytable         | FOREIGN KEY     |
+--------------------+-------------------+---------------------------------------------------------------+--------------+-----------------+-----------------+

我在做什么错了?

3 个答案:

答案 0 :(得分:1)

我认为您混淆了“模式”(又名“数据库”)和“表”):ALTER TABLE mytable ...

答案 1 :(得分:0)

问题是MariaDB(MySql?)不允许删除语法为DROP CONSTRAINT的外键约束。必须这样删除它:

ALTER TABLE `mytable` DROP FOREIGN KEY `myconstraint`;

答案 2 :(得分:0)

此问题记录在https://jira.mariadb.org/browse/MDEV-17599中。它已在版本10.2.22和10.3.13(11-02-2019)中修复。 我建议更新MariaDB版本,或者像@volingas一样写正则表达式:

ALTER TABLE `mytable` DROP FOREIGN KEY `myconstraint`;