I am trying to figure out what is wrong with my syntax here. I feel like I have matched the examples I have found over the internet... but something must be amiss. Here is the code I am using. I keep getting the error code "cannot add Foreign Key constraint" when trying to run the build the schema.
CREATE TABLE Employe (
employee_id INT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
hire_date DATE,
job_title VARCHAR(30),
shop_id INT,
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id)
);
CREATE TABLE Coffee_Shop (
shop_id INT PRIMARY KEY,
shop_name VARCHAR(50),
state CHAR(2),
);
CREATE TABLE Coffee (
coffee_id INT PRIMARY KEY,
shop_id INT,
supplier_id INT,
coffee_name VARCHAR(30),
price_per_pound NUMERIC(5,2),
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id),
FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id)
);
CREATE TABLE Supplier (
supplier_id INT PRIMARY KEY,
company_name VARCHAR(50),
country VARCHAR(30),
sales_contact_name VARCHAR(60),
email VARCHAR(50) NOT NULL,
);
I just need to figure out what part of my foreign key is incorrectly defined. I know it normally results from an improperly defined primary key, but cannot seem to find where I messed up.
Thanks for your help
You are referencing a KEY that is not set up yet. SQL is linear, so the code gets executed line-by-line and cannot reference future lines (in most cases)
Create your tables in this order:
CREATE TABLE Coffee_Shop (
shop_id INT PRIMARY KEY,
shop_name VARCHAR(50),
state CHAR(2),
);
CREATE TABLE Supplier (
supplier_id INT PRIMARY KEY,
company_name VARCHAR(50),
country VARCHAR(30),
sales_contact_name VARCHAR(60),
email VARCHAR(50) NOT NULL,
);
CREATE TABLE Employe (
employee_id INT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
hire_date DATE,
job_title VARCHAR(30),
shop_id INT,
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id)
);
CREATE TABLE Coffee (
coffee_id INT PRIMARY KEY,
shop_id INT,
supplier_id INT,
coffee_name VARCHAR(30),
price_per_pound NUMERIC(5,2),
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id),
FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id)
);
Related
CREATE TABLE EMPLOYEE (
EMPLOYEE_ID INT,
FIRST_NAME VARCHAR(30),
LAST_NAME VARCHAR(30),
HIRE_DATE DATE,
JOB_TITLE VARCHAR(30),
SHOP_ID INT,
PRIMARY KEY(EMPLOYEE_ID),
FOREIGN KEY (EMPLOYEE_ID) REFERENCES COFFEE_SHOP(SHOP_ID)
);
CREATE TABLE COFFEE_SHOP(
SHOP_ID INT AUTO_INCREMENT,
SHOP_NAME VARCHAR(50),
CITY VARCHAR(50),
STATE CHAR(2),
PRIMARY KEY(SHOP_ID)
);
CREATE TABLE COFFEE (
COFFEE_ID INT AUTO_INCREMENT,
SHOP_ID INT,
SUPPLIER_ID INT,
COFFEE_NAME VARCHAR(30),
PRICE_PER_POUND NUMERIC(2,5),
PRIMARY KEY(COFFEE_ID),
FOREIGN KEY (SHOP_ID) REFERENCES COFFEE_SHOP(SHOP_ID),
FOREIGN KEY (SUPPLIER_ID) REFERENCES SUPPLIER(SUPPLIER_ID)
);
CREATE TABLE SUPPLIER (
SUPPLIER_ID INT AUTO_INCREMENT,
COMPANY_NAME VARCHAR(50),
COUNTRY VARCHAR(30),
SALES_CONTACT_NAME VARCHAR(60),
EMAIL VARCHAR(50) NOT NULL,
PRIMARY KEY(SUPPLIER_ID)
);
ERROR: relation "coffee_shop" does not exist
When you add a reference to another table, that other table must exist. In this case the error can be solved by reordering the creation of tables.
For example:
CREATE TABLE COFFEE_SHOP(
SHOP_ID INT AUTO_INCREMENT,
SHOP_NAME VARCHAR(50),
CITY VARCHAR(50),
STATE CHAR(2),
PRIMARY KEY(SHOP_ID)
);
CREATE TABLE EMPLOYEE (
EMPLOYEE_ID INT,
FIRST_NAME VARCHAR(30),
LAST_NAME VARCHAR(30),
HIRE_DATE DATE,
JOB_TITLE VARCHAR(30),
SHOP_ID INT,
PRIMARY KEY(EMPLOYEE_ID),
FOREIGN KEY (EMPLOYEE_ID) REFERENCES COFFEE_SHOP(SHOP_ID)
);
CREATE TABLE SUPPLIER (
SUPPLIER_ID INT AUTO_INCREMENT,
COMPANY_NAME VARCHAR(50),
COUNTRY VARCHAR(30),
SALES_CONTACT_NAME VARCHAR(60),
EMAIL VARCHAR(50) NOT NULL,
PRIMARY KEY(SUPPLIER_ID)
);
CREATE TABLE COFFEE (
COFFEE_ID INT AUTO_INCREMENT,
SHOP_ID INT,
SUPPLIER_ID INT,
COFFEE_NAME VARCHAR(30),
PRICE_PER_POUND NUMERIC(5,2),
PRIMARY KEY(COFFEE_ID),
FOREIGN KEY (SHOP_ID) REFERENCES COFFEE_SHOP(SHOP_ID),
FOREIGN KEY (SUPPLIER_ID) REFERENCES SUPPLIER(SUPPLIER_ID)
);
See running example at DB Fiddle.
These are the two tables that I have created, but I get an error in the table issues:
I have searched up the internet but was unable to find any solution. Can you please help me in this.
CREATE TABLE Issues
(
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(100),
description TEXT,
type VARCHAR(50),
priority VARCHAR(50),
status VARCHAR(50),
assigned_to INT,
created_by INT,
created DATETIME,
updated DATETIME,
FOREIGN KEY (assigned_to, created_by) REFERENCES Users(id, id)
) ENGINE=InnoDB;
CREATE TABLE Users
(
id INT AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30),
lastname VARCHAR(30),
password VARCHAR(30),
email VARCHAR(30),
date_joined DATETIME
) ENGINE=InnoDB;
You want two foreign keys, not one - and you need to create the parent table before the child table:
-- parent table
create table users (
id int auto_increment primary key,
firstname varchar(30),
lastname varchar(30),
password varchar(30),
email varchar(30),
date_joined datetime
) engine=innodb;
-- child table
create table issues (
id int auto_increment primary key,
title varchar(100),
description text,
type varchar(50),
priority varchar(50),
status varchar(50),
assigned_to int,
created_by int,
created datetime,
updated datetime,
foreign key (assigned_to) references users(id), -- first FK
foreign key (created_by) references users(id) -- second FK
) engine=innodb;
I have this code to create some tables:
CREATE TABLE Appelations (
No int AUTO_INCREMENT,
Appelation varchar(35),
County varchar(20),
State varchar(15),
Area varchar(25),
IsAVA varchar(3),
PRIMARY KEY (No)
);
CREATE TABLE Grapes (
ID int AUTO_INCREMENT,
Grape varchar(25),
Color varchar(10),
PRIMARY KEY (ID)
);
CREATE TABLE Wine (
No int AUTO_INCREMENT,
Grape varchar(25),
Winery varchar(40),
Appelation varchar(35),
Name varchar(40),
Year year,
Price int,
Score int,
Cases int,
PRIMARY KEY (No),
FOREIGN KEY (Grape) REFERENCES Grapes(Grape),
FOREIGN KEY (Appelation) REFERENCES Appelations(Appelation)
);
But when I run it, I get this error:
ERROR 1005 (HY000): Can't create table 'db.Wine' (errno: 150)
Does anyone know how to fix this? Thanks.
You need indexes on your foreign keys: "MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan."
Example here: http://sqlfiddle.com/#!9/d228b8
CREATE TABLE Appelations (
No int AUTO_INCREMENT,
Appelation varchar(35),
County varchar(20),
State varchar(15),
Area varchar(25),
IsAVA varchar(3),
INDEX appelation_ind (Appelation), /*<---*/
PRIMARY KEY (No)
);
CREATE TABLE Grapes (
ID int AUTO_INCREMENT,
Grape varchar(25),
Color varchar(10),
INDEX grape_ind (Grape), /*<---*/
PRIMARY KEY (ID)
);
CREATE TABLE Wine (
No int AUTO_INCREMENT,
Grape varchar(25),
Winery varchar(40),
Appelation varchar(35),
Name varchar(40),
Year year,
Price int,
Score int,
Cases int,
PRIMARY KEY (No),
FOREIGN KEY (Grape) REFERENCES Grapes(Grape),
FOREIGN KEY (Appelation) REFERENCES Appelations(Appelation)
);
That's because the FOREIGH KEY columns in the table are referencing the columns that are neither primary keys nor have indexes on them (in parent tables). You can change the type and make them point to ID and No columns of Grapes and Appelations tables respectively, e.g.:
CREATE TABLE Wine (
No int AUTO_INCREMENT,
Grape_id int,
Winery varchar(40),
Appelation_no int,
Name varchar(40),
Year year,
Price int,
Score int,
Cases int,
PRIMARY KEY (No),
FOREIGN KEY (Grape_id) REFERENCES Grapes(id),
FOREIGN KEY (Appelation_no) REFERENCES Appelations(No)
);
Here's the SQL Fiddle.
I have been reading through this forum trying to find the right things to solve my question but I cannot find any answers. I have tweaked my code so much to make sure that the issue is really with adding a foreign key.
My tables are created before I made the foreign key and they are not referencing each out. What is going on?
CREATE TABLE Customer (
CustomerID integer NOT NULL PRIMARY KEY,
LName varchar(200),
FName varchar(200),
StreetAdd varchar(200),
City varchar(100),
State varchar(100),
Zip integer,
Phone bigint
);
CREATE TABLE Donut (
DonutID integer NOT NULL PRIMARY KEY,
Name varchar(100),
Description varchar(200),
UnitPrice float(2)
);
CREATE TABLE Invoice(
Quantity smallint,
OrderID integer,
DonutID integer,
FOREIGN KEY (OrderID) REFERENCES DOrder(OrderID),
FOREIGN KEY (DonutID) REFERENCES Donut(DonutID)
);
CREATE TABLE DOrder (
OrderID integer NOT NULL,
OrderDat integer,
Notes varchar(400),
CustomerID integer,
PRIMARY KEY(OrderID),
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
);
CREATE TABLE postcodes (
postcode_ID INT NOT NULL,
location VARCHAR(50),
PRIMARY KEY (postcode_ID)
);
CREATE TABLE countries (
country_ID INT NOT NULL AUTO_INCREMENT,
country_name VARCHAR(50),
PRIMARY KEY (country_ID)
);
CREATE TABLE suppliers (
supplier_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
supplier_name VARCHAR(50),
supplier_forename VARCHAR(50),
supplier_phonenumber VARCHAR(20),
supplier_address VARCHAR(50),
supplier_postcode INT,
supplier_country INT,
FOREIGN KEY (supplier_postcode) REFERENCES postcode(postcode_ID),
FOREIGN KEY (supplier_country) REFERENCES countries(country_ID)
) ;
Typo:
CREATE TABLE postcodes (
^----plural
FOREIGN KEY (supplier_postcode) REFERENCES postcode(postcode_ID),
^----no S, singular
You're trying to reference a table which doesn't exist.