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
MyISAM has been just mentioned. Simply try adding ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; at the end of a statement, assuming that your other tables were created with MyISAM.
Error Code: 1005 -- there is a wrong primary key reference in your code
usually it's due to a reference FK field not exist. might be you have typo mistake,or check case it should be same, or there's a field-type mismatch. FK-linked fields must match definitions exactly.
Some Known causes may be :
INT(10)
the key field needs to beINT(10)
as well and notINT(11)
orTINYINT
. You may want to confirm the field size usingSHOW
CREATE
TABLE
because Query Browser will sometimes visually show justINTEGER
for bothINT(10)
andINT(11)
. You should also check that one is notSIGNED
and the other isUNSIGNED
. They both need to be exactly the same.MyISAM
table. In order to use foreign keys, the tables must both beInnoDB
. (Actually, if both tables areMyISAM
then you won’t get an error message - it just won’t create the key.) In Query Browser, you can specify the table type.ON
DELETE
SET
NULL
, but the relevant key field is set toNOT
NULL
. You can fix this by either changing your cascade or setting the field to allowNULL
values.ALTER
statement or you have mistyped one of the field names in the relationshipfor more details refer : MySQL Error Number 1005 Can’t create table
I know this is little late answer but I thought this could be helpful to someone.
Sometimes it is due to the master table is dropped (maybe by disabling foreign_key_checks) but the foreign key CONSTRAINT still exist in other tables. In My case I had dropped the table and tried to recreate it but it was throwing the same error for me.
So try dropping all the foreign key CONSTRAINT from all the tables if there are any and then update or create the table.
This could also happen when exporting your database from one server to another and the tables are listed in alphabetical order by default.
So, your first table could have a foreign key of another table that is yet to be created. In such cases, disable foreign_key_checks and create the database.
Just add the following to your script:
and it shall work.
Very often it happens, when the foreign key and the reference key don't have same type or same length