A foreign key constraint fails

后端 未结 3 1091
抹茶落季
抹茶落季 2021-01-06 23:18

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 co

相关标签:
3条回答
  • 2021-01-06 23:43

    Borniet, you helped me solve my similar problem.

    @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 :).

    0 讨论(0)
  • 2021-01-06 23:46

    You can put

    SET FOREIGN_KEY_CHECKS=0;
    

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

    SET FOREIGN_KEY_CHECKS=1;
    
    0 讨论(0)
  • 2021-01-06 23:47

    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

    0 讨论(0)
提交回复
热议问题