gpt4 book ai didi

MySQL : add constraint gives:ERROR 1452 (23000): Cannot add or update a child row?

转载 作者:行者123 更新时间:2023-11-29 01:10:26 24 4
gpt4 key购买 nike

我有以下内容:

mysql> show create table rc_profile_table \G;
*************************** 1. row ***************************
Table: rc_profile_table
Create Table: CREATE TABLE `rc_profile_table` (
`id` int(11) NOT NULL auto_increment,
`created_at` datetime default NULL,
`name` varchar(32) NOT NULL,
`surname` varchar(32) NOT NULL,
`password` varchar(128) NOT NULL,
`unique_code` varchar(16) NOT NULL,
`msisdn` varchar(32) NOT NULL,
`current_location` varchar(64) NOT NULL,
`profile_pic` varchar(255) default NULL,
`email` varchar(128) NOT NULL,
`age` int(11) NOT NULL,
`city_of_birth` varchar(64) NOT NULL,
`personality` varchar(128) NOT NULL,
`country_id` int(11) NOT NULL,
`religious_id` int(11) NOT NULL,
`relationship_id` int(11) NOT NULL,
`wants_and_needs` varchar(512) default NULL,
`hopes_and_aspirations` varchar(512) default NULL,
`profession` varchar(128) default NULL,
`hobbies_and_interests` varchar(512) default NULL,
`skills_and_talents` varchar(512) default NULL,
`open_comment` varchar(512) default NULL,
`food_and_drinks` varchar(512) default NULL,
`activated` int(11) default NULL,
`mood_updated_at` datetime default NULL,
`mood_color` varchar(32) default NULL,
`mood_desc` varchar(64) default NULL,
`login_count` int(10) unsigned default '0',
`campus_id` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `rc_profile_table_FI_2` (`country_id`),
KEY `rc_profile_table_FI_3` (`religious_id`),
KEY `rc_profile_table_FI_4` (`relationship_id`),
KEY `rc_profile_table_FI_5` (`campus_id`),
CONSTRAINT `rc_profile_table_FK_2` FOREIGN KEY (`country_id`) REFERENCES `rc_country_table` (`id`) ON DELETE CASCADE,
CONSTRAINT `rc_profile_table_FK_3` FOREIGN KEY (`religious_id`) REFERENCES `rc_religious_type_table` (`id`) ON DELETE CASCADE,
CONSTRAINT `rc_profile_table_FK_4` FOREIGN KEY (`relationship_id`) REFERENCES `rc_relationship_type_table` (`id`) ON DELETE CASCADE,
CONSTRAINT `rc_profile_table_FK_5` FOREIGN KEY (`campus_id`) REFERENCES `rc_campus_table` (`id`) ON DELETE CASCADE
)
ENGINE=InnoDB AUTO_INCREMENT=159 DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

还有:

mysql> show create table rc_sent_items_table \G;
*************************** 1. row ***************************
Table: rc_sent_items_table
Create Table: CREATE TABLE `rc_sent_items_table` (
`profile_id_from` int(11) NOT NULL,
`profile_id_to` int(11) NOT NULL,
`message` varchar(512) default NULL,
`subject` varchar(255) default NULL,
`opened_once` int(11) default NULL,
`message_type_id` int(11) default NULL,
`id` int(11) NOT NULL auto_increment,
`created_at` datetime default NULL,
PRIMARY KEY (`id`),
KEY `rc_sent_items_table_FI_1` (`profile_id_from`),
KEY `rc_sent_items_table_FI_2` (`profile_id_to`),
KEY `rc_sent_items_table_FI_3` (`message_type_id`),
CONSTRAINT `rc_sent_items_table_FK_3` FOREIGN KEY (`message_type_id`) REFERENCES `rc_message_type_table` (`id`) ON DELETE CASCADE
)
ENGINE=InnoDB AUTO_INCREMENT=159 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

所以当我尝试这样做时:

ALTER TABLE rc_sent_items_table ADD CONSTRAINT `rc_sent_items_table_FK_1`
FOREIGN KEY (`profile_id_to`) REFERENCES `rc_profile_table` (`id`) ON DELETE CASCADE;

我收到这个错误:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`traffic2/#sql-1122_5cf`, CONSTRAINT `rc_sent_items_table_FK_1` FOREIGN KEY
(`profile_id_to`) REFERENCES `rc_profile_table` (`id`) ON DELETE CASCADE)

请问我做错了什么?谢谢

最佳答案

出了什么问题?

你要求 mysql 在两个表之间强制建立一个它以前不知道的关系。不幸的是,两个表的状态表明关系已经中断。那只是运气不好。现在您在 rc_sent_items_table 中有一些记录,其 profile_ids 在 rc_profile_table 中不存在。


我现在要做什么

您只需要决定 rc_sent_items_table 中的记录是否有效,或者您是否应该删除它们。那是只有您可以调用的电话。很可能是您需要它们,然后必须将相应的记录添加到 rc_profile_table。


我需要修复哪些记录?

你可以找到哪些记录不能应用约束,即你需要通过运行这样的东西来修复...

select * from rc_sent_items_table 
where profile_id not in (select profile_id from rc_profile_table)

这将交叉检查“损坏”表中的 message_type_id 及其引用的表。


使用 innodb 状态

您还可以通过检查 innodb 的想法来获得一些有用的指示。如果您在运行命令时使用“\G”标志,您应该得到类似于下面示例的内容,并在前面列出了一些警告。

mysql> show innodb status \G
*************************** 1. row ***************************
Type: InnoDB
Name:
Status:
=====================================
110627 16:06:50 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 11 seconds
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 5, signal count 5
Mutex spin waits 0, rounds 0, OS waits 0
RW-shared spins 4, OS waits 2; RW-excl spins 4, OS waits 3
------------
TRANSACTIONS
------------
Trx id counter 0 167168
Purge done for trx's n:o < 0 166758 undo n:o < 0 0
History list length 13
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 0, not started, process no 996, OS thread id 3013684080
MySQL thread id 34, query id 99 localhost root
show innodb status

关于MySQL : add constraint gives:ERROR 1452 (23000): Cannot add or update a child row?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6494357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com