开发者

MySQL Error throw while delete the index of the foreign key.

开发者 https://www.devze.com 2023-03-23 23:05 出处:网络
I have create two tables with reference with another table: I like this: Table1: CREATE TABLE species ( id TINYINT NOT NULL AUTO_INCREMENT,

I have create two tables with reference with another table:

I like this:

Table1:

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

Table2 (Reference of the above table)

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;

Than its automatically create an index for the FOREIGN KEY for FK_species in zoo table.

Now I am try to delete the Index of the zoo table:

ALTER TABLE zoo DROP INDEX FK_species;

Its showing the following MySQL error.

Error on rename of '开发者_运维技巧.\test\#sql-1ec_9d' to '.\test\zoo' (errno: 150)


From 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.


You need to get the contraints name first.

Example:

SHOW CREATE TABLE zoo;

-> .... 
   CONSTRAINT `zoo_ibfk_1` FOREIGN KEY (`FK_species`) REFERENCES `species` (`id`)

...and then...

ALTER TABLE zoo DROP FOREIGN KEY zoo_ibfk_1;

Read more about this here: http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

0

精彩评论

暂无评论...
验证码 换一张
取 消