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

后端 未结 21 2060
野性不改
野性不改 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: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.

提交回复
热议问题