When MySQL can't create a table due to a foreign key constraint problem, it returns the following error
Can't create table 'dbname.table_name' (errno: 150)
Is there anyway to have MySQL tell you (or to automatically lookup/analyze) why it couldn't create the table. For example, if you attempt to create a table with 10 foreign keys, and one key is invalid, is there a way to have MySQL tell you which one is the problematic key? Or do you need to rely on human intelligence and debugging to figure this out for yourself?
Best Answer
Unfortunately, I would have to go with
Here is why:
Suppose you have the following table
MySQL would have to look for a table called
Staff
. If it finds the tableStaff
, MySQL has to find a column in that table calledStaffID
.For a table with 10 foreign keys, this algorithm would be followed 10 times. Sadly, MySQL does not generate a error stating which table or column name caused the CREATE TABLE to fail. You could just remove one of the foreign keys and try it with 9, then 8, and so on.
Here is a strange case that require intelligence:
There is a case sensitivity issue. The foreign key references may fail due to this.
I have discussed this back in March 2014 : creating table result in errno 150