warhammer
warhammer

Reputation: 23

SQL Fiddle - cannot add foreign key constraint

I am having issues with building the schema for my salesorderslineitem table. Even when I try to build the schema by itself in fiddle. I get an error msg cannot add foreign key constraint... Here my tables together trying to get the new salesorderslineitem table working. Thanks everyone!

CREATE TABLE salesorders
( donut_order_id             INT(10) NOT NULL AUTO_INCREMENT,
  customer_id                INT,
  date                       DATETIME,
  special_handling_note      TEXT,
  PRIMARY KEY (donut_order_id),
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

INSERT INTO salesorders
 (donut_order_id,date,special_handling_note)

VALUES( 0000001,"2018-04-22","Please include plates and napkins");

CREATE TABLE salesorderslineitem
(
  donut_order_id     INT(10) NOT NULL, 
  donut_id           INT(10) NOT NULL,
  qty                INT(10) NOT NULL,
  PRIMARY KEY (donut_order_id, donut_id),
  FOREIGN KEY (donut_order_id) REFERENCES salesorders(donut_order_id),
  FOREIGN KEY (donut_id) REFERENCES donuts(donut_id)
 );

INSERT INTO salesorderslineitem
 (qty)

VALUES (10);

CREATE TABLE donuts
 (donut_id     INT(10) NOT NULL AUTO_INCREMENT,
  name         VARCHAR(30),
  description  VARCHAR(50),
  unit_price   DECIMAL(8,3),
  PRIMARY KEY (donut_id)
 );

INSERT INTO donuts
 (name,description,unit_price)

Upvotes: 0

Views: 680

Answers (1)

Shiva
Shiva

Reputation: 20935

I've fixed all your issues.

Here's a working SQLFiddle. http://sqlfiddle.com/#!9/5071d0

The first issue is that haven't defined the CREATE Statement for the Customers table and are trying to add a FOREIGN KEY to this non-existent table in the CREATE TABLE salesorders statement at the following line

FOREIGN KEY (customer_id) REFERENCES customers(customer_id)

To fix this, I wrote the CREATE TABLE Customers section before the SalesOrders CREATE Statement.

That's just 1 issue. You are also creating the SalesOrders table before the Donuts table and trying to add a FOREIGN KEY Constraint.

Finally, you didn't have any record in the Donuts table, so I created a Jelly Donut for you :)

SQLFiddle code posted below for completeness.

CREATE TABLE customers ( 
  customer_id INT(10) NOT NULL AUTO_INCREMENT, 
  customer_name VARCHAR(100) NOT NULL,
  PRIMARY KEY (customer_id)
  );

CREATE TABLE donuts (
  donut_id INT(10) NOT NULL AUTO_INCREMENT, 
  name VARCHAR(30), 
  description VARCHAR(50), 
  unit_price DECIMAL(8,3), 
  PRIMARY KEY (donut_id) );

INSERT INTO donuts (donut_id,name,description,unit_price)  
VALUES (1,"Jelly Donut","Delicious Donut filled with Raspberry Jelly inside",1.99);

CREATE TABLE salesorders ( 
  donut_order_id INT(10) NOT NULL AUTO_INCREMENT, 
  customer_id INT, date DATETIME, 
  special_handling_note TEXT, 
  PRIMARY KEY (donut_order_id), 
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id) );

INSERT INTO salesorders ( donut_order_id,date,special_handling_note )
VALUES( 0000001,"2018-04-22","Please include plates and napkins" );

CREATE TABLE salesorderslineitem ( 
  donut_order_id INT(10) NOT NULL, 
  donut_id INT(10) NOT NULL, qty INT(10) NOT NULL, 
  PRIMARY KEY (donut_order_id, donut_id),
  FOREIGN KEY (donut_order_id) 
  REFERENCES salesorders(donut_order_id), 
  FOREIGN KEY (donut_id) REFERENCES donuts(donut_id) );

INSERT INTO salesorderslineitem (donut_order_id,donut_id, qty) VALUES (0000001,1, 12);

Upvotes: 0

Related Questions