Can't create Foreign key on mysql database - mysql

I'm cannot create any foreign key in my mysql database. I used foreign key before, but recently I had to reformat and reinstall everything, and after a restored the mysql server, the foreign keys aren't working anymore.
I tried to create the simple tables bellow:
create table `order` (
order_id int not null AUTO_INCREMENT primary key,
customer varchar(100)
);
create table `order_item` (
order_item_id int not null AUTO_INCREMENT primary key,
order_id int not null,
product varchar(100),
foreign key (order_id) references `order`
);
The order tables is created successfully, but when i try to create the order_item table, i get the message Error Code: 1005. Can't create table mydb.order_item (errno: 150 "Foreign key constraint is incorrectly formed"). I've checked my code multiple times, and I'm not able to find a error. Do someone knows what could be wrong?
I'm using XAMPP to install and start both apache and mysql.
Edit:
#LV98 's answer solved the error, but when i added a second Foreign key, got the same message:
create table `order` (
order_id int not null AUTO_INCREMENT primary key,
customer varchar(100)
);
create table product (
product_id int not null auto_increment primary key,
name varchar(100)
);
create table `order_item` (
order_item_id int not null AUTO_INCREMENT primary key,
order_id int not null,
product_id int not null,
product varchar(100),
foreign key (order_id) references `order`(order_id),
foreign key (product_id) references product (product_id)
);

You forgot to reference a column.
foreign key (order_id) references `order`(order_id)

Related

Failed to add the foreign key constraint in MySQL: error 3780

I am getting the error:
Error Code: 3780. Referencing column 'category' and referenced column 'category_id' in foreign key constraint 'product_ibfk_1' are incompatible.
drop table if exists Provider;
drop table if exists Category;
drop table if exists Product;
create table Provider
(
privider_id serial not null primary key,
login_password varchar(20) not null
constraint passrule3 check(login_password sounds like '[A-Za-z0-9]{6,20}'),
fathersname varchar(20) not null,
name_of_contact_face varchar(10) not null,
surname varchar(15),
e_mail varchar(25) unique
constraint emailrule2 check(e_mail sounds like '[A-Za-z0-9]{10,10})\#gmail.com\s?')
);
create table Category
(
title varchar(20),
category_id serial not null primary key
);
create table Product
(
barecode serial not null primary key,
provider_id bigint not null,
manufacturer varchar(25) not null,
category_id bigint not null,
dimensions varchar(10) not null,
amount int not null,
date_of_registration datetime not null,
#constraint 'provider_for_product'
foreign key (provider_id) references Provider (provider_id) on delete restrict on update cascade,
foreign key (category_id) references Category (category_id) on delete restrict on update cascade
);
The datatypes of the two columns referenced in a foreign key constraint need to match
In MySQL, SERIAL is an alias for BIGINT UNSIGNED AUTO_INCREMENT.
To make a foreign key that references this column, it must be BIGINT UNSIGNED, not a signed BIGINT.
You might like to view a checklist of foreign key mistakes I contributed to: https://stackoverflow.com/a/4673775/20860
I also cover foreign key mistakes in more detail in a chapter of my book, SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming.

mysql error 1215(hy000) cannot add foreign key constraint

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

Getting error 1064 while running query for table creation?

I already have created customers and products table.I am getting this error while running following command in phpmyadmin.
CREATE TABLE orders ( id INT NOT NULL AUTO_INCREMENT,
orderNumber INT,
productId INT,
customerId INT,
orderDate DATETIME default CURRENT_TIMESTAMP,
PRIMARY KEY(id),
PRIMARY KEY (customerId) REFERENCES customers(id) ,
FOREIGN KEY (productId) REFERENCES products(id)
);
1064 error
Can not create more than one primary keys.
Try this code.
CREATE TABLE orders (
id INT NOT NULL AUTO_INCREMENT,
orderNumber INT,
productId INT,
customerId INT,
orderDate DATETIME default CURRENT_TIMESTAMP,
PRIMARY KEY(id),
FOREIGN KEY (customerId) REFERENCES customers(id) ,
FOREIGN KEY (productId) REFERENCES products(id));

Cannot add foreign key constraint MySQL Workbench

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));

errno 150 mySQL foreign key

This SQL is giving me Errno 150 when i'm trying to create the second table with the foreign key on UserID
Can anyone please advice me what am i doing wrong?
CREATE DATABASE IF NOT EXISTS OTA;
USE OTA;
CREATE TABLE IF NOT EXISTS Users
(
UserID int AUTO_INCREMENT NOT NULL PRIMARY KEY,
UserName varchar(255) NOT NULL,
Email varchar(255) UNIQUE ,
PW varchar(255),
PN varchar(255),
Admin BIT
);
CREATE TABLE IF NOT EXISTS Notes
(
UID int AUTO_INCREMENT NOT NULL PRIMARY KEY,
Note varchar (255) NOT NULL,
c_Date Date NOT NULL,
c_text varchar (255) NOT NULL,
FOREIGN KEY (UID) REFERENCES Persons(UserID)
);
Sorry for being a duplicate question but i can't find my answer between the related ones.
The problem is that you are trying to reference Persons(UserID) when your first table is called Users. Try this for your key:
FOREIGN KEY (UID) REFERENCES Users(UserID)
However, you should have a separate column for the note ID.
Try declaring the primary key after:
CREATE TABLE Orders
(O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id))
Also, you auto-incremented both UID and UserID. This is fine if they are the same length, but if you try to reference a foreign key value that doesn't exist, you will get an error.