简体   繁体   English

外键约束失败

[英]A foreign key constraint fails

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.. 我在php和mysql中比较新。当我在leave表中插入值时,我面临的问题。我leave包含以下列的表..

1.lid(INT primary key) 1.lid(INT主键)

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 FOREIGN KEY) 9.empID(INT FOREIGN KEY)

here empID is the foreign key references from users table. 这里empID是来自users表的foreign key引用。 The problem that im facing while inserting values into the leave table.ERROR is given below 将值插入到leave表中时所面临的问题.ERROR如下所示

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 )) 不能添加或更新子行,外键约束失败( db_attendance1leave ,约束leave_ibfk_1外键( empID )参考文献usersempID ))

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());

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;

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. 这意味着,如果用户中不存在新的EmpID,则无法更新EmpID。 You can't add a new EmpID if it doesn't exist in the users table, etcetera. 如果在users表等中不存在新的EmpID,则无法添加新的EmpID。 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'. 因此,要解决此问题,您需要确保您尝试添加到表'leave'的EmpID首先存在于表'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 由于您正在处理的数据库有外键约束,我建议您稍微阅读它们: http//en.wikipedia.org/wiki/Foreign_key

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. @OP - 我必须做的就是在表中创建一个相应的行,以便存在外键。 Eg 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 :). 例如,表1有列名称表2有列的friends_name,一个外键与表1中的Name相关联。我收到此错误是因为我试图在表2中插入一行,其中friends_name引用了表1中的一个不存在的名称。所以我创造了这个名字,我们即将参加比赛:)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM