create table order_details(
Order_id int PRIMARY KEY AUTO_INCREMENT,
Book_Id int,
Cust_Name varchar(50),
Phone_No int,
Address varchar(100),
Order_Date DATE,
Quantity int,
FOREIGN KEY(Book_Id) REFERENCES book(Book_Id));
Error (150) is being thrown because mysql is unable to reference the specified foreign key. This can be caused for a number of reasons however, I recommend taking the following steps to troubleshoot this.
A common issue that causes this error to become evasive is when you have not performed USE my_database_name;
before executing the query. It fails because the context is either wrong or absent.
1.) Revise your query by adding in the name of the database in the reference
- Example:
FOREIGN KEY(`Book_Id`) REFERENCES `my_database_name`.`book`(`Book_Id`));
2.) Take a look at the book
table and make sure `Book_Id`
is the correct type (int)
and is named exactly as you reference it. Perform the following queries and you may find your answer:
SELECT `b`.`Book_Id` FROM `book` AS `b`;
EXPLAIN `book`;
Selecting Book_Id
from the referenced table will rule out typos in field naming. Explain will reveal the value type & key information that should help ensure consistency between foreign relations when investigating issues like this.