2009-12-08 102 views
0

你好,我有一个mysql数据库,在它的地址表。奇怪的mysql错误

这是它的创建方式:

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; 

现在我想删除约束per_id因为我决定它是不是nessecary。所以我删除它,看看这里的错误:

SQL查询:

ALTER TABLE `addresses` DROP INDEX `per_id` 

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

于是我决定删除该表并没有约束再次创建,但是当我做了降表,我得到:

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

那么我该如何摆脱这个问题呢?

回答

5

索引与外键不一样。试试这个:

ALTER TABLE `addresses` DROP FOREIGN KEY `per_id`