简体   繁体   English

MySQL:插入和更新时出错:无法添加或更新子行:外键约束失败

[英]MySQL: Error when insert and update: Cannot add or update a child row: a foreign key constraint fails

I've been reading some questions with the same problem I have but have not found the solution 我一直在阅读与我有相同问题的一些问题,但尚未找到解决方案

Database Estructure 数据库结构

CREATE TABLE material (
  cve_mat varchar(12),
  type_mat varchar(20),
  author_mat varchar(40),
  status varchar(2),
  primary key(cve_mat)   
) ENGINE = INNODB;

CREATE TABLE users (
  cve_user varchar(8),
  name_user varchar(25),
  lastn_user varchar(25),
  email_user varchar(25),
  primary key(cve_user)
)ENGINE = INNODB;

CREATE TABLE material_user (
  cve_mat varchar(8),
  cve_user varchar(8),
  start_mat date,
  end_mat date,
  foreign key(cve_mat) references material(cve_mat)
    ON UPDATE CASCADE
    ON DELETE CASCADE,
  foreign key(cve_user) references users(cve_user)
    ON UPDATE CASCADE
    ON DELETE CASCADE
) ENGINE = INNODB;

And when execute these instructions: 当执行这些指令时:

......
$query = "INSERT INTO material_user VALUES ('$cvemat','$ncontrol',NOW(),DATE_ADD(current_date, interval '$days' day))";
$result = mysql_query($query) or die(mysql_error());

if ($result) {
$query = "UPDATE material SET status = 'YES' WHERE material.cve_mat = '$cvemat'";
$result = mysql_query($query) or die(mysql_error());

if ($result){
    return true;
} else {
    return false;
}
} else {
return false;
}

And display this error: 并显示此错误:

Cannot add or update a child row: a foreign key constraint fails (`biblioteca/material_prestamo`, CONSTRAINT `material_prestamo_ibfk_1` FOREIGN KEY (`cve_mat`) REFERENCES `material` (`cve_mat`) ON DELETE CASCADE ON UPDATE CASCADE)

I would greatly appreciate your help! 非常感谢您的帮助!

You have foreign key constrain so you need to execute your second query first then your fisrt query second. 您具有外键约束,因此您需要先执行第二个查询,然后再执行fisrt查询。 It may be help you 可能对您有帮助

$query = "UPDATE material SET status = 'YES' WHERE material.cve_mat = '$cvemat'";
$result = mysql_query($query) or die(mysql_error());

if ($result) {
   $query = "INSERT INTO material_user VALUES  ('$cvemat','$ncontrol',NOW(),DATE_ADD(current_date, interval '$days' day))";
   $result = mysql_query($query) or die(mysql_error());

First, the column types for the foreign key should match the referencing tables: 首先,外键的列类型应与引用表匹配:

material_user material_user

cve_mat varchar(8),

material 材料

cve_mat varchar(12),

Those two column types should be equal. 这两个列类型应相等。

Before your insert statement you can perform some debugging: 在插入语句之前,您可以执行一些调试:

SELECT * FROM material WHERE cve_mat='$cvemat'

If all seems okay, you can run this to find out more details: 如果一切正常,您可以运行此命令以查找更多详细信息:

SHOW INNODB STATUS;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL:无法添加或更新子行:在 Codeigniter 中将我的数据插入数据库时​​,外键约束失败 - MySQL : Cannot add or update a child row: a foreign key constraint fails when insert my data to database in Codeigniter Mysql:无法添加或更新子行:外键约束失败 - Mysql:Cannot add or update a child row: a foreign key constraint fails 错误:无法添加或更新子行:外键约束失败? - Error: Cannot add or update a child row: a foreign key constraint fails? 无法添加或更新子行:外键约束失败错误 - Cannot add or update a child row: a foreign key constraint fails Error 添加外键约束时无法更新子表:错误:无法添加或更新子行:外键约束失败 - unable to update a child table when a foreign key constraint is added: error :Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行,外键约束失败,mysql错误号:1452 - cannot add or update a child row a foreign key constraint fails mysql Error Number: 1452 无法添加或更新子行:外键约束失败PHP或MySQL错误? - Cannot add or update a child row: a foreign key constraint fails Error in PHP or MySQL? MySQL 错误:无法添加或更新子行:外键约束失败 - MySQL Error: Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败(mysql中的外键问题) - Cannot add or update a child row: a foreign key constraint fails(Foreign key issue in mysql) 无法添加或更新子行:外键约束失败(Mysql和外键) - Cannot add or update a child row: a foreign key constraint fails (Mysql and Foreign key)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM