2009-04-24 72 views
0

我有一個問題的兩個表:混亂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_postalcode` 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=utf8 ; 
-- 
-- RELATIES VOOR TABEL `addresses`: 
-- `cnt_id` 
--  `countries` -> `cnt_id` 
-- `per_id` 
--  `persons` -> `per_id` 
-- 

CREATE TABLE `events` ( 
    `evt_id` int(11) NOT NULL auto_increment, 
    `evt_name` varchar(50) NOT NULL, 
    `evt_description` varchar(100) default NULL, 
    `evt_startdate` date NOT NULL, 
    `evt_enddate` date default NULL, 
    `evt_starttime` time default NULL, 
    `evt_endtime` time default NULL, 
    `evt_amtpersons` int(11) default NULL, 
    `sts_id` int(11) NOT NULL, 
    `adr_id` int(11) default NULL, 
    `evt_amtPersonsSubs` tinyint(4) NOT NULL default '0', 
    `evt_photo` varchar(50) default NULL, 
    `sys-mut-dt` timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 
    `sys-mut-user` varchar(20) default NULL, 
    `sys-mut-id` int(11) NOT NULL default '0', 
    PRIMARY KEY (`evt_id`), 
    KEY `sts_id` (`sts_id`), 
    KEY `adr_id` (`adr_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

的問題是,當我嘗試刪除地址表中的記錄,我得到以下錯誤

DELETE 
FROM addresses 
WHERE per_id = 45 

1451 - Cannot delete or update a parent row: a foreign key constraint fails (`site/events`, CONSTRAINT `adr_id` FOREIGN KEY (`adr_id`) REFERENCES `addresses` (`adr_id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ; 

怪異事情是在事件表中沒有與per_id或adr_id相同的記錄。

那麼這裏發生了什麼?

我該如何解決這個問題? ;-)

/* I think it means "How can I solve this?" */ 

回答

3

這可能聽起來很愚蠢,但你確定沒有子行嗎?

能否請您運行這個命令:

SELECT e.* 
FROM addresses a 
JOIN events e 
ON  e.adr_id = a.adr_id 
WHERE a.per_id = 45