Search code examples
sqlmysqlmysql-error-1025

Weird mysql error


Hello I have a mysql database with in it an addresses table.

This is the way it's created:

CREATE TABLE IF NOT EXISTS `addresses` (
  `adr_id` int(11) NOT NULL AUTO_INCREMENT,
  `per_id` int(11) NOT NULL,
  `adr_street` varchar(50) NOT NULL,
  `adr_houseno` int(11) DEFAULT NULL,
  `adr_housenoadd` varchar(10) DEFAULT NULL,
  `adr_postcode` varchar(25) NOT NULL,
  `adr_city` varchar(20) NOT NULL,
  `adr_type` varchar(45) DEFAULT NULL,
  `cnt_id` int(11) NOT NULL,
  `adr_date` date DEFAULT NULL,
  `sys-mut-dt` timestamp NULL DEFAULT NULL,
  `sys-mut-user` varchar(20) DEFAULT NULL,
  `sys-mut-id` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`adr_id`),
  KEY `per_id` (`per_id`),
  KEY `cnt_id` (`cnt_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;


    --
    ALTER TABLE `addresses`
      ADD CONSTRAINT `cnt_id` FOREIGN KEY (`cnt_id`) REFERENCES `countries` (`cnt_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
      ADD CONSTRAINT `per_id` FOREIGN KEY (`per_id`) REFERENCES `persons` (`per_id`) ON DELETE NO ACTION ON UPDATE NO ACTION;

Now i wanted to delete the constraint per_id since I decided it is not nessecary. So i deleted it and see here the error:

SQL query:

ALTER TABLE `addresses` DROP INDEX `per_id`

MySQL said: Documentation
#1025 - Error on rename of './ladosa/#sql-521_d2' to './ladosa/addresses' (errno: 150) 

Then i decided to drop the table and created again without the constraint but when i did a drop table i get:

#1217 - Cannot delete or update a parent row: a foreign key constraint fails

so how can i get out of this problem?


Solution

  • An index is not the same as a foreign key. Try this instead:

    ALTER TABLE `addresses` DROP FOREIGN KEY `per_id`