I'm building a really basic database for a school project and am getting a "cannot add foreign key constraint" error in MySQL. I've been scratching my head on this one for the past day, read all of the related posts and haven't been able to figure it out.
Here are the first two tables of my project:
CREATE TABLE CUSTOMER (
CUST_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
CUST_LNAME VARCHAR(25) NOT NULL,
CUST_FNAME VARCHAR(25) NOT NULL,
CUST_INITIAL CHAR(1),
CUST_STREET_NO VARCHAR(6),
CUST_STREET_NAME VARCHAR(25),
CUST_APT_NO VARCHAR(10),
CUST_CITY VARCHAR(25),
CUST_STATE CHAR(2),
CUST_ZIP_CODE CHAR(5),
CUST_HOME_AC CHAR(3),
CUST_HOME_PHONE CHAR(8),
PRIMARY KEY (CUST_ID))
ENGINE = InnoDB;
CREATE TABLE INVOICE (
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
CUST_ID INTEGER NOT NULL,
INV_DATE DATE NOT NULL,
SPECIAL_HANDLING VARCHAR(35),
PRIMARY KEY (INVOICE_ID),
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER ON UPDATE CASCADE)
ENGINE = InnoDB;
I'm sure it's something super easy that I'm missing. Any ideas?
You are missing the column to be referred in your foreign key constraint def.
Use:
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER(CUST_ID) ON UPDATE CASCADE)
instead of
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER ON UPDATE CASCADE)
So, create table becomes:
CREATE TABLE INVOICE (
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
CUST_ID INTEGER NOT NULL,
INV_DATE DATE NOT NULL,
SPECIAL_HANDLING VARCHAR(35),
PRIMARY KEY (INVOICE_ID),
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER(CUST_ID) ON UPDATE CASCADE)
ENGINE = InnoDB;
try to declare the name of the foreign key, and then declare the column of the current table and the table and the reference column
CONSTRAINT fk_customer FOREIGN KEY (INVOICE_ID)
REFERENCES customer(CUST_ID)
Related
I was creating some tables in a database, everything works fine except for when I try to create the last table 'gatt_descriptors' I get the warning
gatt_profiles (primary key 'version')
CREATE TABLE gatt_profiles(
version INT NOT NULL PRIMARY KEY,
profile_name VARCHAR(25),
value VARCHAR(25));
gatt_services (primary key 'service_name')
CREATE TABLE gatt_services(
service_name VARCHAR(25) NOT NULL,
uuid VARCHAR(36),
id VARCHAR(36),
declaration_type VARCHAR(10),
advertise BIT,
version INT NOT NULL,
CONSTRAINT ver_val FOREIGN KEY (version) REFERENCES gatt_profiles(version) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT pk_verser PRIMARY KEY (service_name, version));
gatt_characteristics (primary key 'char_name')
CREATE TABLE gatt_characteristics(
char_name VARCHAR(25) PRIMARY KEY NOT NULL,
uuid VARCHAR(36),
id VARCHAR(36),
val_const BIT,
init_val VARCHAR(25),
var_length BIT,
val_length INT,
read_val VARCHAR(7),
write_val VARCHAR(7),
write_wo_response VARCHAR(7),
reliable_write VARCHAR(7),
notify VARCHAR(7),
indicate VARCHAR(7),
version INT NOT NULL,
service_name VARCHAR(25) NOT NULL,
FOREIGN KEY (version) REFERENCES gatt_profiles (version) ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (service_name) REFERENCES gatt_services (service_name) ON DELETE RESTRICT ON UPDATE CASCADE);
gatt_descriptors (primary key 'descriptor_name')
CREATE TABLE gatt_descriptors(
descriptor_name VARCHAR(25) NOT NULL,
uuid VARCHAR(36),
id VARCHAR(36),
val_const BIT,
init_val VARCHAR(25),
var_length BIT,
val_length INT,
read_val VARCHAR(7),
write_val VARCHAR(7),
version INT NOT NULL,
service_name VARCHAR(25) NOT NULL,
char_name VARCHAR(25),
FOREIGN KEY (char_name, service_name, version) REFERENCES gatt_characteristics (char_name, service_name, version) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT pk_verserchardescr PRIMARY KEY (descriptor_name, char_name, service_name, version));
Your help is greatly appreciated. Thank you.
gatt_characteristics has a primary key consisting of the single column char_name.
The foreign key in gatt_descriptors is three columns, the first of which is char_name.
When you define a foreign key, it should have exactly the same columns as the primary key of the table it references.
I keep getting this error, I used InnoDB for all tables, menuID, and customerID, are primary keys in their respective tables, and the datatypes appear to be the same.
ERROR 1215 (HY000): Cannot add foreign key constraint
Sorry if I am missing something simple, I am new to mySQL.
create table customers(
customerID int not null auto_increment primary key,
LastName varchar(255) not null,
FirstName varchar(255) not null,
email varchar(255) not null,
password varchar(255),
phone varchar(255),
creditCard varchar(255),
address varchar(255),
time timestamp)
Engine=InnoDB;
create table menu(
menuID int not null auto_increment primary key,
typeID int,
itemName varchar(255),
price varchar(255))
Engine=InnoDB;
create table orders(
orderID int not null,
customerID int not null,
menuID int not null,
PRIMARY KEY (orderID, customerID, menuID),
FOREIGN KEY (customerID) REFERENCES customers(customerID) on delete set null on update cascade,
foreign key (menuID) references menu(menuID) on delete set null on update cascade )
Engine=InnoDB;
You can't use ON DELETE SET NULL for a foreign key column that you declared NOT NULL.
See this answer for a long checklist of things to check as possible causes of foreign key errors: MySQL Creating tables with Foreign Keys giving errno: 150
I'm getting "Error Code: 1215. Cannot add foreign key constraint" when trying to create the "orders" table.
credit_cards table:
CREATE TABLE credit_cards (
customer VARCHAR(30),
card_no CHAR(16),
PRIMARY KEY (customer, card_no),
FOREIGN KEY (customer) REFERENCES customers(username));
orders table:
CREATE TABLE orders (
order_no INT AUTO_INCREMENT,
customer VARCHAR(30) NOT NULL,
date_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
credit_card CHAR(16) NOT NULL,
PRIMARY KEY (order_no),
FOREIGN KEY (customer)
REFERENCES customers (username),
FOREIGN KEY (credit_card)
REFERENCES credit_cards (card_no));
The report from SHOW ENGINE INNODB STATUS says that the problem is FOREIGN KEY (credit_card) REFERENCES credit_cards(card_no))
I've read a bunch of resolved questions and still can't figure it out. I'm using MySQL Workbench. Thanks.
You need to reference all the keys in a primary key (or unique key) relationship. I would recommend:
CREATE TABLE credit_cards (
credit_card_id int auto_increment primary key,
customer VARCHAR(30),
card_no CHAR(16),
UNIQUE KEY (customer, card_no),
FOREIGN KEY (customer) REFERENCES customers(username));
orders table:
CREATE TABLE orders (
order_no INT AUTO_INCREMENT,
customer VARCHAR(30) NOT NULL,
date_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
credit_card_id int NOT NULL,
PRIMARY KEY (order_no),
FOREIGN KEY (customer)
REFERENCES customers (username),
FOREIGN KEY (credit_card_id)
REFERENCES credit_cards (credit_card_id));
I am quite new at SQL. I have created a file full of complete table requests, all are working except one. The error relates to the ProductPrice foreign Key in the Orders_Info table. I can not see or find what I have done wrong. Anyone able to help give me a pointer please?
Create table Orders
(
Order_Number INT (11) NOT NULL AUTO_INCREMENT,
Date Date, Account_Number INT(11),
FOREIGN KEY (Account_Number) REFERENCES User_Accounts(Account_Number) ON DELETE CASCADE,
Address_ID INT(11),
FOREIGN KEY (Address_ID) REFERENCES Address_Book(Address_ID) ON DELETE CASCADE,
Order_Status VARCHAR(30) NOT NULL, PRIMARY KEY (Order_Number)
)ENGINE=InnoDB;
Create table Order_Info
(
Order_Number INT (11),
FOREIGN KEY (Order_Number) REFERENCES Orders(Order_Number) ON DELETE CASCADE,
Product_Number INT (11),
FOREIGN KEY (Product_Number) REFERENCES Products(Product_ID) ON DELETE CASCADE,
Product_Quantity INT (11), ProductPrice Decimal(10,2) NOT NULL,
FOREIGN KEY (ProductPrice) REFERENCES Products(ProductPrice) ON DELETE CASCADE
)ENGINE=InnoDB;
I have a study case where three tables has to be created. there creation statements are as following.
create table COMMUNITY(
c_id varchar(10) primary key,
name varchar(30) not null,
longitude float,
latitude float,
post_code varchar(15) not null,
key(c_id))
create table UNIT(
c_id varchar(10) not null,
u_id int not null,
name varchar(20) not null,
key(c_id, u_id),
primary key(c_id, u_id),
constraint unique(c_id, u_id),
constraint FK_UNIT foreign key(c_id) references COMMUNITY(c_id)
on delete cascade on update cascade)
create table ROOM(
r_id int not null,
u_id int not null,
c_id varchar(10) not null,
name varchar(20),
primary key(c_id, u_id, r_id),
constraint FK_ROOM_UID foreign key(u_id) references UNIT(u_id)
on delete cascade on update cascade,
constraint FK_ROOM_CID foreign key(c_id) references UNIT(c_id)
on delete cascade on update cascade)
the Community and unit tables are created successfully, but when I try to create room, mysql gives me Error Code: 1215. Cannot add foreign key constraint
I wonder what's going here and How I can create them? (I knew InnoDB can solve this problem, but is there any other way I can do that?)
Thanks
This looks like the culprit of the actual error you're seeing:
constraint FK_ROOM_UID foreign key(u_id) references UNIT(u_id)
constraint FK_ROOM_UID foreign key(c_id) references UNIT(c_id)
It should be:
constraint FK_ROOM_UID foreign key(c_id, u_id) references UNIT(c_id, u_id)
Referencing the double-column key in the UNIT table
You'll also need to use InnoDB to actually create the foreign key indexes, so:
CREATE TABLE UNIT(
....
) ENGINE=InnoDB;
I finally fugured this out, according to the MySQL log
"Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint."
All I have to do is to create primary key at the first column of a table and make sure the type matches. so I changed my statements as following
create table UNIT(
primary key(c_id, u_id),
c_id varchar(10) not null,
u_id int not null,
name varchar(20) not null,
constraint unique(c_id, u_id),
constraint FK_UNIT foreign key(c_id) references COMMUNITY(c_id)
on delete cascade on update cascade)
create table ROOM(
primary key(c_id, u_id, r_id),
r_id int not null,
u_id int not null,
c_id varchar(10) not null,
name varchar(20),
constraint FK_ROOM_UID foreign key(c_id, u_id)
references UNIT(c_id, u_id) on delete cascade on update cascade)
Now I can create tables properly. I have been struggling on this problem since yesterday.