更改表时出错,添加约束外键获取错误“无法添加或更新子行"

error altering table, adding constraint foreign key getting error quot;Cannot add or update a child rowquot;(更改表时出错,添加约束外键获取错误“无法添加或更新子行)
本文介绍了更改表时出错,添加约束外键获取错误“无法添加或更新子行"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mysql> DESCRIBE questions;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(255)     | NO   | PRI | NULL    | auto_increment |
| question | varchar(255) | NO   |     | NULL    |                |
| type     | char(1)      | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
mysql> DESCRIBE answers;  
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(255)     | NO   | PRI | NULL    | auto_increment |
| answer       | varchar(255) | NO   |     | NULL    |                |
| questionid   | int(255)     | NO   |     | NULL    |                |
| questions_id | int(255)     | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

我正在使用这种说法:

ALTER TABLE 答案添加外键(questions_id)参考问题(id);

ALTER TABLE answers ADD FOREIGN KEY(questions_id) REFERENCES questions(id); 

但我得到这个错误:

ERROR 1452 (23000):无法添加或更新子行:外键约束失败(surveydb.#sql-df_32,CONSTRAINT #sql-df_32_ibfk_1 FOREIGN KEY (questions_id) REFERENCES questions (id)) 到您的 MySQL 服务器版本,以便在附近使用正确的语法第 1 行的描述问题"

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (surveydb.#sql-df_32, CONSTRAINT #sql-df_32_ibfk_1 FOREIGN KEY (questions_id) REFERENCES questions (id))to your MySQL server version for the right syntax to use near 'DESCREBE questions' at line 1

推荐答案

answers.questions_id 中至少有一个数据值在 questions.id 中没有出现.

You have at least one data value in answers.questions_id that does not occur in questions.id.

这是我的意思的一个例子:

Here's an example of what I mean:

mysql> create table a ( id int primary key);

mysql> create table b ( aid int );

mysql> insert into a values (123);

mysql> insert into b values (123), (456);

mysql> alter table b add foreign key (aid) references a(id);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint 
fails (`test`.`#sql-3dab_e5c`, CONSTRAINT `#sql-3dab_e5c_ibfk_1` FOREIGN KEY
(`aid`) REFERENCES `a` (`id`))

您可以使用它来确认存在不匹配的值:

You can use this to confirm that there are unmatched values:

SELECT COUNT(*)
FROM answers AS a
LEFT OUTER JOIN questions AS q ON a.questions_id = q.id
WHERE q.id IS NULL

这篇关于更改表时出错,添加约束外键获取错误“无法添加或更新子行"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)