MySQL 删除外键索引时抛出错误。

标签 mysql indexing foreign-keys

我创建了两个表并引用另一个表:

我喜欢这个:

表1:

CREATE TABLE species 
(
  id TINYINT NOT NULL AUTO_INCREMENT, 
  name VARCHAR(50) NOT NULL, 
  PRIMARY KEY(id)
) ENGINE=INNODB;

表2(上表引用)

CREATE TABLE zoo 
(
  id INT(4) NOT NULL, 
  name VARCHAR(50) NOT NULL,
  FK_species TINYINT(4) NOT NULL, 
  INDEX (FK_species), 
  FOREIGN KEY (FK_species) REFERENCES species (id), 
  PRIMARY KEY(id)
) ENGINE=INNODB;

它会自动为 zoo 表中的 FK_species 的 FOREIGN KEY 创建索引。

现在我尝试删除动物园表的索引:

ALTER TABLE zoo DROP INDEX FK_species;

它显示以下 MySQL 错误。

Error on rename of '.\test\#sql-1ec_9d' to '.\test\zoo' (errno: 150)

最佳答案

来自FOREIGN KEY Constraints @ dev.mysql.com :

InnoDB supports the use of ALTER TABLE to drop foreign keys:

ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;

If the FOREIGN KEY clause included a CONSTRAINT name when you created the foreign key, you can refer to that name to drop the foreign key. Otherwise, the fk_symbol value is internally generated by InnoDB when the foreign key is created. To find out the symbol value when you want to drop a foreign key, use the SHOW CREATE TABLE statement.

关于MySQL 删除外键索引时抛出错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6871075/

相关文章:

php - 增加发票 ID 的最佳方法是什么?

postgresql - 多列 BRIN 列顺序重要吗?

python - SQLAlchemy 外键找不到表

mysql - 如何将一条记录插入到以用户 ID 作为外键的表中

mysql - 是否应该在用户可以选择多个选项的结构中使用外键?如果是这样,怎么会这样?

MySQL/Eloquent 查询优化

php - Mysql ORDER BY 使用日期数据行

mysql - 我需要为表中的字段设置什么数据类型,以限制一组选项的值?

sql - 在 Postgresql 中使用重复索引有什么坏处吗?

mysql - 为什么 Index 只在强制使用时使用,默认不使用?