I searched for a solution to this problem on internet and checked the SO questions but no solution worked for my case.
I want to create a foreign key from table sira_no to metal_kod.
ALTER TABLE sira_no
ADD CONSTRAINT METAL_KODU FOREIGN KEY(METAL_KODU)
REFERENCES metal_kod(METAL_KODU)
ON DELETE SET NULL
ON UPDATE SET NULL ;
This script returns:
Error Code: 1005. Can't create table 'ebs.#sql-f48_1a3' (errno: 150)
I tried adding index to the referenced table:
CREATE INDEX METAL_KODU_INDEX ON metal_kod (METAL_KODU);
I checked METAL_KODU on both tables (charset and collation). But couldn't find a solution to this problem. Does anyone have any idea? Thanks in advance.
EDIT: Here is the metal_kod table:
METAL_KODU varchar(4) NO PRI
DURUM bit(1) NO
METAL_ISMI varchar(30) NO
AYAR_YOGUNLUK smallint(6) YES 100
It happened in my case because the name of the table being referenced in the constraint declaration isn't correct (i forgot the upper case in the table name)
I hope that helps.
I had the very same error message. Finally I figured out I misspelled the name of the table in the command:
versus
I wonder why on earth mysql cannot tell such a table does not exist...
Error Code: 1005
Hello, I am putting across this answer so that anybody facing similar issue like mine can benefit for this response. Trust me this can be overlooked) (this may have been already answered and if so please excuse me)
I had similar issue, so here are few things that I did try (not in any order except for the solution :) )
9. Finally, I saw that i had two editors open. One that in PHPStorm (jetbrains) and the other MySQL workbench. It seems that the PHPStorm / SQL workbench creates some kind of edit lock. I closed PHPStorm just to check if locking was the case (it could have been the other way around). This solved my problem. Hope this helps someone having similar issue.
In my case, it happened when one table is InnoB and other is MyISAM. Changing engine of one table, through MySQL Workbench, solves for me.
The foreign key has to have the exact same type as the primary key that it references. For the example has the type “INT UNSIGNED NOT NULL” the foreing key also have to “INT UNSIGNED NOT NULL”
I had a similar error. The problem had to do with the child and parent table not having the same charset and collation. This can be fixed by appending ENGINE = InnoDB DEFAULT CHARACTER SET = utf8;
... on the SQL statement means that there is some missing code.