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
Answers
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 :
- The two key fields type and/or size doesn’t match exactly. For example, if one is
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. - One of the key field that you are trying to reference does not have an index and/or is not a primary key. If one of the fields in the relationship is not a primary key, you must create an index for that field.
- The foreign key name is a duplicate of an already existing key. Check that the name of your foreign key is unique within your database. Just add a few random characters to the end of your key name to test for this.
- One or both of your tables is a
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. - You have specified a cascade
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. - Make sure that the Charset and Collate options are the same both at the table level as well as individual field level for the key columns.
- You have a default value (ie default=0) on your foreign key column
- One of the fields in the relationship is part of a combination (composite) key and does not have it’s own individual index. Even though the field has an index as part of the composite key, you must create a separate index for only that key field in order to use it in a constraint.
- You have a syntax error in your
ALTER
statement or you have mistyped one of the field names in the relationship - The name of your foreign key exceeds the max length of 64 chars.
Very often it happens, when the foreign key and the reference key don't have same type or same length
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 :) )
- Changed the foreign key names(didn't work)
- Reduced the foreign key length
- Verified the datatypes (darn nothing wrong)
- Check indexes
- Check the collations (everything fine, darn again)
- Truncated the table, of no good use
- Dropped the table and re-created
- Tried to see if any circular reference is being created --- all fine
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.
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)
ALTER TABLE `Window` ADD CONSTRAINT `Windows_ibfk_1` FOREIGN KEY (`WallId`) REFERENCES `Wall` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
I hope that helps.
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.
CREATE TABLE IF NOT EXISTS `tablename` (
`key` bigint(20) NOT NULL AUTO_INCREMENT,
FOREIGN KEY `key` (`key`) REFERENCES `othertable`(`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
I had the very same error message. Finally I figured out I misspelled the name of the table in the command:
ALTER TABLE `users` ADD FOREIGN KEY (country_id) REFERENCES country (id);
versus
ALTER TABLE `users` ADD FOREIGN KEY (country_id) REFERENCES countries (id);
I wonder why on earth mysql cannot tell such a table does not exist...
0 comments:
Post a Comment