title: MySQL Error 1452 (23000): Cannot add or update a child row
body: <p>I need a help with this problem. I don't understand how to resolve this problem. I will be happy if you find out why this problem happens. Here is my SQL:</p>
  
  <pre><code>Create database Order_Purchase;
  Use Order_Purchase;
  
  Create table Customer (
  customerID      int(10),
  cname           varchar(30),
  city            varchar(30),
  Constraint customer_PK Primary Key (customerID))
  Engine = innodb;
  
  Create table Item (
  itemID          int(10),
  description     varchar(30),    
  price           double,
  Constraint item_PK Primary Key (itemID))
  Engine = innodb;
  
  Create table Order_ (
  orderID         int(10),
  customerID      int(10),
  orderDate       date,
  Constraint order_PK Primary Key (orderID),
  Constraint order_FK1 Foreign Key (customerID) references Customer(customerID))
  Engine = innodb;
  
  Create table Order_Item (
  orderID         int(10) null default null,
  itemID          int(10) null default null,
  quantity        int,
  Constraint order_item_PK Primary Key (orderID, itemID),
  Constraint order_item_FK1 Foreign Key (orderID) references Order_(orderID),
  Constraint order_item_FK2 Foreign Key (itemID) references Item(itemID))
  Engine = innodb;
  
  Create table Warehouse (
  warehouseID     int(10),
  city            varchar(30),
  Constraint warehouse_PK Primary Key (warehouseID))
  Engine = innodb;
  
  Create table Shipment (
  orderID         int(10), 
  warehouseID     int(10),
  shipDate        date,
  Constraint shipment_PK Primary Key (orderID, warehouseID),
  Constraint shipment_FK1 Foreign Key (orderID) references Order_(orderID),
  Constraint shipment_FK2 Foreign Key (warehouseID) references Warehouse(warehouseID))
  ENGINE = innodb;
  </code></pre>
  
  <hr>
  
  <p>My code for inserting:</p>
  
  <pre><code>Insert into Order_Item (orderID, itemID, quantity) values (10000, 105472, 1);
  
  Insert into Order_Item (orderID, itemID, quantity) values (10001, 105472, 4);
  
  Insert into Order_Item (orderID, itemID, quantity) values (10002, 104375, 1);
  
  Insert into Order_Item (orderID, itemID, quantity) values (10003, 100870, 2);
  
  Insert into Order_Item (orderID, itemID, quantity) values (10003, 103798, 1);
  
  Insert into Order_Item (orderID, itemID, quantity) values (10004, 103798, 1);
  
  Insert into Order_Item (orderID, itemID, quantity) values (10004, 105472, 2);
  
  Insert into Order_Item (orderID, itemID, quantity) values (10005, 100475, 1);
  
  Insert into Order_Item (orderID, itemID, quantity) values (10006, 100870, 1);
  
  Insert into Order_Item (orderID, itemID, quantity) values (10006, 105472, 1);
  
  Insert into Order_Item (orderID, itemID, quantity) values (10006, 103798, 1);
  </code></pre>
  
  <p>The result:</p>
  
  <pre><code>mysql&gt; Insert into Order_Item (orderID, itemID, quantity) values (10000, 105472, 1);
  Query OK, 1 row affected (0.05 sec)
  
  mysql&gt; Insert into Order_Item (orderID, itemID, quantity) values (10001, 105472, 4);
  Query OK, 1 row affected (0.01 sec)
  
  mysql&gt; Insert into Order_Item (orderID, itemID, quantity) values (10002, 104375, 1);
  Query OK, 1 row affected (0.01 sec)
  
  mysql&gt; Insert into Order_Item (orderID, itemID, quantity) values (10003, 100870, 2);
  Query OK, 1 row affected (0.00 sec)
  
  mysql&gt; Insert into Order_Item (orderID, itemID, quantity) values (10003, 103798, 1);
  ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`order_purchase`.`order_item`, CONSTRAINT `o
  rder_item_FK2` FOREIGN KEY (`itemID`) REFERENCES `item` (`itemID`))
  mysql&gt; Insert into Order_Item (orderID, itemID, quantity) values (10004, 103798, 1);
  ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`order_purchase`.`order_item`, CONSTRAINT `o
  rder_item_FK2` FOREIGN KEY (`itemID`) REFERENCES `item` (`itemID`))
  mysql&gt; Insert into Order_Item (orderID, itemID, quantity) values (10004, 105472, 2);
  Query OK, 1 row affected (0.01 sec)
  </code></pre>