Error Code: 1005. Can't create table '…

2019-01-02 17:36发布

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 

11条回答
深知你不懂我心
2楼-- · 2019-01-02 17:46

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.

查看更多
几人难应
3楼-- · 2019-01-02 17:49

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...

查看更多
像晚风撩人
4楼-- · 2019-01-02 17:54

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 :) )

  1. Changed the foreign key names(didn't work)
  2. Reduced the foreign key length
  3. Verified the datatypes (darn nothing wrong)
  4. Check indexes
  5. Check the collations (everything fine, darn again)
  6. Truncated the table, of no good use
  7. Dropped the table and re-created
  8. 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.

查看更多
萌妹纸的霸气范
5楼-- · 2019-01-02 17:54

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.

查看更多
笑指拈花
6楼-- · 2019-01-02 17:56

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”

CREATE TABLE employees(
id_empl INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id)
);
CREATE TABLE offices(
id_office INT UNSIGNED NOT NULL AUTO_INCREMENT,
id_empl INT UNSIGNED NOT NULL,
PRIMARY KEY(id),
CONSTRAINT `constraint1` FOREIGN KEY (`id_empl`) REFERENCES `employees` (`id_empl`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='my offices';
查看更多
浪荡孟婆
7楼-- · 2019-01-02 18:00

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;

CREATE TABLE IF NOT EXISTS `country` (`id` INT(11) NOT NULL AUTO_INCREMENT,...) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8;

... on the SQL statement means that there is some missing code.

查看更多
登录 后发表回答