令人困惑的mysql问题

发布于 2024-07-17 14:32:49 字数 2158 浏览 10 评论 0原文

我遇到了两个表的问题:

 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?" */

I have got a problem with two tables:

 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; 

The problem is, when i try to delete a record from address table I get the following error

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) ; 

The weird thing is that there is no record with the same per_id or adr_id in the events table.

So what's going on over here?

How Can I solve this? ;-)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦初醒的夏天 2024-07-24 14:32:49

这可能听起来很愚蠢,但是您绝对确定没有子行吗?

你能运行这个吗:

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

It may sound stupid, but are you absolutely sure there are no child rows?

Could you please run this:

SELECT  e.*
FROM    addresses a
JOIN    events e
ON      e.adr_id = a.adr_id
WHERE   a.per_id = 45
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文