ERROR 1452: Cannot add or update a child row: a foreign key constraint fails

后端 未结 21 1736
野性不改
野性不改 2020-11-22 07:01

I have created tables in MySQL Workbench as shown below :

ORDRE table:

CREATE TABLE Ordre (
  OrdreID   INT NOT NULL,
  OrdreDato DA         


        
相关标签:
21条回答
  • 2020-11-22 07:46

    This error generally occurs because we have some values in the referencing field of the child table, which do not exist in the referenced/candidate field of the parent table.

    Sometimes, we may get this error when we are applying Foreign Key constraints to existing table(s), having data in them already. Some of the other answers are suggesting to delete the data completely from child table, and then apply the constraint. However, this is not an option when we already have working/production data in the child table. In most scenarios, we will need to update the data in the child table (instead of deleting them).

    Now, we can utilize Left Join to find all those rows in the child table, which does not have matching values in the parent table. Following query would be helpful to fetch those non-matching rows:

    SELECT child_table.* 
    FROM child_table 
    LEFT JOIN parent_table 
      ON parent_table.referenced_column = child_table.referencing_column 
    WHERE parent_table.referenced_column IS NULL
    

    Now, you can generally do one (or more) of the following steps to fix the data.

    1. Based on your "business logic", you will need to update/match these unmatching value(s), with the existing values in the parent table. You may sometimes need to set them null as well.
    2. Delete these rows having unmatching values.
    3. Add new rows in your parent table, corresponding to the unmatching values in the child table.

    Once the data is fixed, we can apply the Foreign key constraint using ALTER TABLE syntax.

    0 讨论(0)
  • 2020-11-22 07:48

    Taken from Using FOREIGN KEY Constraints

    Foreign key relationships involve a parent table that holds the central data values, and a child table with identical values pointing back to its parent. The FOREIGN KEY clause is specified in the child table.

    It will reject any INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table.

    So your error Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails essentially means that, you are trying to add a row to your Ordrelinje table for which no matching row (OrderID) is present in Ordre table.

    You must first insert the row to your Ordre table.

    0 讨论(0)
  • 2020-11-22 07:48

    I was getting this issue even though my parent table had all the values I was referencing in my child table. The issue seemed to be that I could not add multiple child references to a single foreign key. In other words if I had five rows of data referenced the same foreign key, MySQL was only allowing me to upload the first row and giving me the error 1452.

    What worked for me was typing the code "SET GLOBAL FOREIGN_KEY_CHECKS=0". After that I closed out of MySQL and then restarted it and I was able to upload all of my data with no errors. I then typed "SET GLOBAL FOREIGN_KEY_CHECKS=1" to set the system back to normal although I'm not entirely sure what FOREIGN_KEY_CHECKS does. Hope this helps!

    0 讨论(0)
  • 2020-11-22 07:50

    This helped me out after reading @Mr-Faizan's and other answers.

    Untick the 'Enable foreign key checks'

    in phpMyAdmin and hit the query. I don't know about WorkBench but the other answers might help you out.

    0 讨论(0)
  • 2020-11-22 07:51

    you should add data from REFERENCES KEY in PRIMARY TABLE to FOREIGN KEY in CHILD TABLE
    it means do not add random data to foreign key ، just use data from primary key that is accessable

    description of data in foreign key

    0 讨论(0)
  • 2020-11-22 07:51

    The problem occurs because you set the foreign key in child table after you insert some data in the child table.

    Try removing all the data from child table, then set the foreign key and afterwards add/insert the data in table, it will work.

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