Search code examples
mysqlsqlmysql-error-1025

It is not letting me drop a foreign key


I am trying to remove a foreign key from a table but I am getting this error below in mysql:

1025 - Error on rename of './mobile_app/Question' to './mobile_app/#sql2-4517-15515' (errno: 152)

What does this error mean and how can it be fixed?

Below is the code where I am trying to drop the foreign key:

alter table Question drop foreign key FK_Option_Table;

Below is the Question Table details (no rows in this table):

CREATE TABLE `Question` (
 `SessionId` varchar(10) NOT NULL DEFAULT '',
 `QuestionId` int(5) NOT NULL,
 `QuestionContent` varchar(5000) NOT NULL,
 `NoofAnswers` int(2) NOT NULL,
 `AnswerId` int(10) NOT NULL AUTO_INCREMENT,
 `ReplyId` varchar(2) NOT NULL,
 `QuestionMarks` int(4) NOT NULL,
 `OptionId` varchar(3) NOT NULL,
 PRIMARY KEY (`SessionId`,`QuestionId`),
 KEY `FK_Option_Table` (`OptionId`),
 KEY `FK_IndividualQuestion` (`QuestionId`),
 KEY `FK_Reply` (`ReplyId`),
 KEY `FK_AnswerId` (`AnswerId`)
) ENGINE=InnoDB AUTO_INCREMENT=76 DEFAULT CHARSET=utf8

Below is the Option_Table details:

CREATE TABLE `Option_Table` (
 `OptionId` varchar(3) NOT NULL,
 `OptionType` varchar(20) NOT NULL,
 PRIMARY KEY (`OptionId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

There are 24 rows in this table and below is a sample:

OptionId OptionType
O1       A-C
O2       A-D
O3       A-E
O4       A-F
...

Solution

  • Those are index's not foreign keys you have. I see no foreign keys in your create statements.

    Example to create foreign key.

    ALTER TABLE `Question` 
      ADD CONSTRAINT `SessionId`
      FOREIGN KEY (`SessionId` )
      REFERENCES `Option_Table` (`OptionId` )
      ON DELETE NO ACTION
      ON UPDATE CASCADE
    , ADD INDEX `test_idx` (`SessionId` ASC) ;
    
    ALTER TABLE `Question` DROP FOREIGN KEY `SessionId` ;