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)
);
question from:
https://stackoverflow.com/questions/65926131/cant-create-foreign-key-on-mysql-database 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…