I am trying to import a .sql file and its failing on creating tables.
Here\'s the query that fails:
CREATE TABLE `data` (
`id` int(10) unsigned NOT NUL
This error can occur if two tables have a reference, for example, one table is Student and another table is Education, and we want the Education table to have a foreign key reference of Student table. In this instance the column data type for both tables should be same, otherwise it will generate an error.
Error 150 means you have a problem with your foreign key. Possibly the key on the foreign table isn't the exact same type?
After cruising through the answers above, and experimenting a bit, this is an effective way to solve Foreign Key errors in MySQL (1005 - error 150).
For the foreign key to be properly created, all MySQL asks for is:
Satisfy these requirements and all will be well.
I faced this kind of issue while creating DB from the textfile.
mysql -uroot -padmin < E:\important\sampdb\createdb.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\create_student.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\create_absence.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\insert_student.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\insert_absence.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\load_student.sql
mysql -uroot -padmin sampdb < E:\important\sampdb\load_absence.sql
I just wrote the above lines in Create.bat
and run the bat file.
My mistake is in the sequence order of execution in my sql files. I tried to create table with primary key and also foreign key. While its running it will search for the reference table but tables are not there. So it will return those kind of error.
If you creating tables with foreign key then check the reference tables were present or not. And also check the name of the reference tables and fields.
I experienced this error when have ported Windows application to Linux. In Windows, database table names are case-insensitive, and in Linux they are case-sensitive, probably because of file system difference. So, on Windows table Table1
is the same as table1
, and in REFERENCES
both table1
and Table1
works. On Linux, when application used table1
instead of Table1
when it created database structure I saw error #150; when I made correct character case in Table1
references, it started to work on Linux too. So, if nothing else helps, make you sure that in REFERENCES
you use correct character case in table name when you on Linux.
I had the same error. In my case the reason for the error was that I had a ON DELETE SET NULL statement in the constraint while the field on which I put the constraint in its definition had a NOT NULL statement. Allowing NULL in the field solved the problem.