Search code examples
sqlinnodbsqlfiddle

SQL - Cannot add foreign key constraint


I am completely new to writing SQL code and I am attempting to run a simple table creation, however I cannot find where the error in my programming is, and as I am completely new I am struggling with this creation.

This is a school project that I am working on, and hoping anyone can help.

The error I am receiving in 'SQLFiddle' is "Cannot add foreign key constraint" on the following code:

CREATE TABLE invoice(
  invoice_id INT NOT NULL,
  customer_id INT NOT NULL,
  order_date DATE NULL,
  spec_order_note VARCHAR(45) NULL,
  PRIMARY KEY(invoice_id, customer_id),
  FOREIGN KEY (customer_id)
  REFERENCES customer.customer_id
  ON DELETE CASCADE
  ON UPDATE CASCADE
);

CREATE TABLE line_item (
  invoice_id INT NOT NULL,
  donut_id INT NOT NULL,
  quantity INT NULL
  CONSTRAINT donut_invoice
  FOREIGN KEY invoice_id
  REFERENCES invoice.invoice_id
  ON DELETE RESTRICT
  ON UPDATE RESTRICT
)

CREATE TABLE donut (
  donut_id INT NOT NULL,
  donut_name VARCHAR(15) NULL,
  description VARCHAR(30) NULL,
  unit_price INT NULL
  PRIMARY KEY(donut_id),
)

CREATE TABLE customer (
  customer_id INT NOT NULL,
  last_name VARCHAR(15) NULL,
  first_name VARCHAR(10) NULL,
  street_add VARCHAR(20) NULL,
  apt_num INT NULL,
  city VARCHAR(20) NULL,
  state VARCHAR(15) NULL,
  zip_code INT NULL,
  home_phone VARCHAR(10) NULL,
  mobile_phone VARCHAR(10) NULL,
  other_phone VARCHAR(10) NULL,
  customer_notes VARCHAR(45) NULL
  PRIMARY KEY(customer_id),
)

Any help is greatly appreciated.


Solution

  • You can only reference existing tables and columns in foreign constraints. So if you want to reference customer table in invoice's foreign key, you need to either create customer before invoice or add the foreign key constrain additionally using ALTER TABLE.

    Apart of that, there's couple syntax errors in your code like missing semicolons and misplaced (missing and additional) commas.

    A working code:

    CREATE TABLE customer (
      customer_id INT NOT NULL,
      last_name VARCHAR(15) NULL,
      first_name VARCHAR(10) NULL,
      street_add VARCHAR(20) NULL,
      apt_num INT NULL,
      city VARCHAR(20) NULL,
      state VARCHAR(15) NULL,
      zip_code INT NULL,
      home_phone VARCHAR(10) NULL,
      mobile_phone VARCHAR(10) NULL,
      other_phone VARCHAR(10) NULL,
      customer_notes VARCHAR(45) NULL,
      PRIMARY KEY(customer_id)
    );
    
    CREATE TABLE invoice(
      invoice_id INT NOT NULL,
      customer_id INT NOT NULL,
      order_date DATE NULL,
      spec_order_note VARCHAR(45) NULL,
      PRIMARY KEY(invoice_id, customer_id),
      FOREIGN KEY (customer_id)
      REFERENCES customer (customer_id)
      ON DELETE CASCADE
      ON UPDATE CASCADE
    );
    
    CREATE TABLE line_item (
      invoice_id INT NOT NULL,
      donut_id INT NOT NULL,
      quantity INT NULL,
      CONSTRAINT donut_invoice
      FOREIGN KEY (invoice_id)
      REFERENCES invoice (invoice_id)
      ON DELETE RESTRICT
      ON UPDATE RESTRICT
    );
    
    CREATE TABLE donut (
      donut_id INT NOT NULL,
      donut_name VARCHAR(15) NULL,
      description VARCHAR(30) NULL,
      unit_price INT NULL,
      PRIMARY KEY(donut_id)
    );
    

    http://sqlfiddle.com/#!9/36b044