外键约束失败 [英] A foreign key constraint fails

查看:144
本文介绍了外键约束失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php和mysql中相对较新.我在leave表中插入值时遇到的问题.我的leave表包含以下列..

I am relatively new in php and mysql.The problem that i am facing while i inserting value in my leave table.My leave table containing following column..

1.lid(INT主键)

1.lid(INT primary key)

2.empname(varchar)

2.empname(varchar)

3.username(varchar)

3.username(varchar)

4.nod(INT)

4.nod(INT)

5.sdate(DATE)

5.sdate(DATE)

6.edate(DATE)

6.edate(DATE)

7.reason(varchar)

7.reason(varchar)

8.action(varchar)

8.action(varchar)

9.empID(INT外键)

9.empID (INT FOREIGN KEY)

此处empIDusers表中的foreign key引用.在将值插入到leave表中时,im遇到的问题.错误在下面给出

here empID is the foreign key references from users table. The problem that im facing while inserting values into the leave table.ERROR is given below

无法添加或更新子行:外键约束失败(db_attendance1.leave,CONSTRAINT leave_ibfk_1外键(empID)引用users(empID))

Cannot add or update a child row: a foreign key constraint fails (db_attendance1.leave, CONSTRAINT leave_ibfk_1 FOREIGN KEY (empID) REFERENCES users (empID))

这里我只是发送查询,就在这里.

here i just send the query and here it is..

    mysql_query("INSERT INTO `leave` 
           (`empname`, `username`,
            `nod`, `sdate`, `edate`,
            `reason`,`action`) 
            VALUES ('$empname', '$username', 
            '$nod', '$sdate', 
            '$edate', '$reason','');",
            $dbCon) or die(mysql_error());

推荐答案

您可以放

SET FOREIGN_KEY_CHECKS=0;

并运行您的查询.再次完成操作后,将其设置回1,

and run your query. Once you are done again set it back to 1 by

SET FOREIGN_KEY_CHECKS=1;

这篇关于外键约束失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆