Cannot add or update a child row: a foreign key constraint fails(无法添加或更新子行:外键约束失败)
问题描述
表 #1:
字段 | 类型 | 空 | 键 | 默认 | 额外 |
---|---|---|---|---|---|
用户名 | int(11) | 没有 | PRI | NULL | 自动增量 |
密码 | varchar(20) | 没有 | |||
用户名 | varchar(25) | 没有 | |||
电子邮件 | varchar(60) | 没有 |
表 #2:
字段 | 类型 | 空 | 键 | 默认 | 额外 |
---|---|---|---|---|---|
用户名 | int(11) | 没有 | 多 | ||
帖子ID | int(11) | 没有 | PRI | NULL | 自动增量 |
标题 | varchar(50) | 没有 | |||
总结 | varchar(500) | 没有 |
错误:
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(myapp/table2
, CONSTRAINT table2_ibfk_1
外键 (UserID
)参考 table1
(UserID
))
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (
myapp/table2
, CONSTRAINTtable2_ibfk_1
FOREIGN KEY (UserID
) REFERENCEStable1
(UserID
))
我做错了什么?我阅读了 http://www.w3schools.com/Sql/sql_foreignkey.asp 并且我不知道有什么问题.
What have I done wrong? I read http://www.w3schools.com/Sql/sql_foreignkey.asp and I don't see what's wrong.
推荐答案
您收到此错误是因为您尝试向 table2
添加/更新没有有效值的行UserID
字段基于当前存储在 table1
中的值.如果您发布更多代码,我可以帮助您诊断具体原因.
You're getting this error because you're trying to add/update a row to table2
that does not have a valid value for the UserID
field based on the values currently stored in table1
. If you post some more code I can help you diagnose the specific cause.
这篇关于无法添加或更新子行:外键约束失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法添加或更新子行:外键约束失败


基础教程推荐
- 在多列上分布任意行 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01