sqlfiddle

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


**Im new to sql, I'm having issues with the Cannot add or update a child row: a foreign key constraint fails If any can point out the error, please do, I have no idea what it is. Thanks! sql Fiddle link http://www.sqlfiddle.com/#!9/3b724/2

CREATE TABLE donut
(
  donutid      INT(5) NOT NULL,
  name         VARCHAR(20),
  descriptioin VARCHAR(60),
  price        DECIMAL(8,3),
  PRIMARY KEY (donutid)
);
CREATE TABLE customer
(
  customerid INT(5)NOT NULL,
  firstname  VARCHAR(30),
  lastname   VARCHAR(30),
  addres     VARCHAR(100),
  apt        VARCHAR(20),
  city       VARCHAR(30),
  state      VARCHAR(20),
  zip        VARCHAR(8),
  homephone  VARCHAR(15),
  cellphone  VARCHAR(15),
  otherphone VARCHAR(15),
  PRIMARY KEY (customerid)
);
CREATE TABLE customer_order_donut
(
  orderid    INT(10) NOT NULL,
  customerid INT(10),
  donutid    INT(10),
  orderdate  DATETIME,
  donutqty   INT(5),
  specialhandling TEXT,
  PRIMARY KEY (orderid),
  FOREIGN KEY (customerid) REFERENCES customer(customerid),
  FOREIGN KEY (donutid) REFERENCES donut(donutid)
);

INSERT INTO customer (customerid, firstname, lastname, addres, apt, city, state, zip, homephone,
cellphone, otherphone)VALUES ('786','Alfreds','John','1417 Gali Gondni','2T', 
'Lawrenceville','GA','30046','14235678','12345678','14563287');

INSERT INTO customer (customerid, firstname, lastname, addres, apt, city, state, zip, homephone,
cellphone, otherphone)VALUES ('829','Thomas','Jawad','1917 Gondni St','3PG', 
'Jackville','FL','32046','1344235678','14563287','345934522');

INSERT INTO customer (customerid, firstname, lastname, addres, apt, city, state, zip, homephone,
cellphone, otherphone)VALUES ('779','Jawad','Peter','19337 Kalan CT','34PD', 
'Snailville','VA','45612','345934522','4525852','1455445452');

INSERT INTO donut (donutid, name, descriptioin, price) VALUES ('101','Yeast',' Light Airy ','1.20');
INSERT INTO donut (donutid, name, descriptioin, price) VALUES ('111','Cake ',' Bit Crustier ','2.40');
INSERT INTO donut (donutid, name, descriptioin, price) VALUES ('121','Old-Fashioned',' More Crunch','3.80');

CREATE INDEX Donut_name_feild
ON donut (name);

INSERT INTO customer_order_donut (orderid, customerid, donutid, orderdate,donutqty,specialhandling) VALUES ('141','101','251','2010-10-11','14','In Rush');
INSERT INTO customer_order_donut (orderid, customerid, donutid, orderdate,donutqty,specialhandling) VALUES ('151','111','326','2010-10-61','12','No Coffe');
INSERT INTO customer_order_donut (orderid, customerid, donutid, orderdate,donutqty,specialhandling) VALUES ('161','121','235','2010-10-91','15','In No Hurry');

Solution

  • You are missing a key for customer_id 101 in the customer table.

    INSERT INTO customer_order_donut (orderid, customerid, donutid, orderdate,donutqty,specialhandling) VALUES ('141','101','251','2010-10-11','14','In Rush');