php 外键约束失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16140200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A foreign key constraint fails
提问by Tanvir
I am relatively new in php and mysql.The problem that i am facing while i inserting value in my leavetable.My leavetable containing following column..
我是比较新的PHP和mysql.The的问题,我面对,而我在插入值leavetable.Myleave包含以下栏表..
1.lid(INT primary key)
1.lid(INT主键)
2.empname(varchar)
2.empname(varchar)
3.username(varchar)
3.用户名(varchar)
4.nod(INT)
4.点头(INT)
5.sdate(DATE)
5.sdate(DATE)
6.edate(DATE)
6.日期(日期)
7.reason(varchar)
7.原因(varchar)
8.action(varchar)
8.动作(varchar)
9.empID (INT FOREIGN KEY)
9.empID(国际外键)
here empIDis the foreign keyreferences from userstable. The problem that im facing while inserting values into the leavetable.ERROR is given below
这empID是表中的foreign key参考users。我在将值插入leavetable.ERROR 时面临的问题如下
Cannot add or update a child row: a foreign key constraint fails (db_attendance1.leave, CONSTRAINT leave_ibfk_1FOREIGN KEY (empID) REFERENCES users(empID))
无法添加或更新子行:外键约束失败 ( db_attendance1. leave, CONSTRAINT leave_ibfk_1FOREIGN 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());
回答by Digant
You can put
你可以放
SET FOREIGN_KEY_CHECKS=0;
and run your query. Once you are done again set it back to 1 by
并运行您的查询。再次完成后将其设置回 1
SET FOREIGN_KEY_CHECKS=1;
回答by Borniet
A foreign key constraint means that you one table doesn't accept inserts, updates or deletes that would 'break' the foreign key. This means, you can't update a EmpID if the new EmpID doesn't exist in the users. You can't add a new EmpID if it doesn't exist in the users table, etcetera. So to solve this issue, you need to make sure that the EmpID you're trying to add to table 'leave', first exists in table 'users'. Foreign keys can be a real powerful item, but can be a real pain too. Since the DB you're working on had foreign key constraints, I suggest you read on them a bit: http://en.wikipedia.org/wiki/Foreign_key
外键约束意味着您的一个表不接受会“破坏”外键的插入、更新或删除。这意味着,如果用户中不存在新的 EmpID,则您无法更新 EmpID。如果用户表中不存在新的 EmpID,则无法添加新的 EmpID,等等。因此,要解决此问题,您需要确保您尝试添加到表 'leave' 的 EmpID 首先存在于表 'users' 中。外键可以是一个真正强大的项目,但也可以是一个真正的痛苦。由于您正在处理的数据库有外键约束,我建议您阅读一下:http: //en.wikipedia.org/wiki/Foreign_key
回答by gabrielg
Borniet, you helped me solve my similar problem.
Borniet,你帮我解决了类似的问题。
@OP - All I had to do to fix this was create a corresponding row in the table so that the foreign key would exist. E.g. Table 1 has column Name Table 2 has column friends_name, a foreign key tied to Name in table 1. I got this error because I was trying to insert a row into table 2, where the friends_name referenced a non existing Name in table 1. So I created the name and we're off to the races :).
@OP - 我所要做的就是在表中创建一个相应的行,以便外键存在。例如,表 1 有列名称 表 2 有列friends_name,这是与表 1 中的名称相关联的外键。我收到此错误是因为我试图在表 2 中插入一行,其中friends_name 引用了表 1 中不存在的名称。所以我创建了这个名字,我们开始比赛了:)。

