错误更改表,添加约束外键获取错误“无法添加或更新子行” [英] error altering table, adding constraint foreign key getting error "Cannot add or update a child row"
问题描述
的MySQL>描述问题;
+ ---------- + -------------- + ------ + ----- + ------ --- + ---------------- +
|字段|类型|空|密钥|默认|额外|
+ ---------- + -------------- + ------ + ----- + ------ --- + ---------------- +
| id | int(255)| NO | PRI | NULL | auto_increment |
|问题| varchar(255)| NO | | NULL | |
|键入| char(1)|是| | NULL | |
+ ---------- + -------------- + ------ + ----- + ------ --- + ---------------- +
mysql>描述答案;
+ -------------- + -------------- + ------ + ----- + - ------- + ---------------- +
|字段|类型|空|密钥|默认|额外|
+ -------------- + -------------- + ------ + ----- + - ------- + ---------------- +
| id | int(255)| NO | PRI | NULL | auto_increment |
|回答| varchar(255)| NO | | NULL | |
| questionid | int(255)| NO | | NULL | |
| questions_id | int(255)| NO | | NULL | |
+ -------------- + -------------- + ------ + ----- + - ------- + ---------------- +
我使用这个语句:
ALTER TABLE答案ADD FOREIGN KEY(questions_id)参考题目(id);
但我得到这个错误:
错误1452(23000):Can not添加或更新子行:外键约束失败(
surveydb
。#sql-df_32
,CONSTRAINT#sql-df_32_ibfk_1
FOREIGN KEY(questions_id
)参考疑问
(<$
<您在
answers.questions_id
中至少有一个数据值不会出现在 questions.id
。 下面是我的意思的例子:
的MySQL>创建表a(id int主键);
mysql>创建表b(aid int);
mysql>插入一个值(123);
mysql>插入b值(123),(456);
mysql> alter table b add foreign key(aid)引用a(id);
错误1452(23000):无法添加或更新子行:外键约束
失败(`test`.`#sql-3dab_e5c`,CONSTRAINT`#sql-3dab_e5c_ibfk_1` FOREIGN KEY
(`aid`)参考`a`(`id`))
你可以使用这个确认有不匹配的值:
pre $ $ $ $ $ $ $ OUTER JOIN问题AS q ON a.questions_id = q.id
WHERE q.id IS NULL
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 | |
+--------------+--------------+------+-----+---------+----------------+
I am using this statement:
ALTER TABLE answers ADD FOREIGN KEY(questions_id) REFERENCES questions(id);
but i get this error:
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
) REFERENCESquestions
(id
))to your MySQL server version for the right syntax to use near 'DESCREBE questions' at line 1
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
这篇关于错误更改表,添加约束外键获取错误“无法添加或更新子行”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!