MySQL Error Number 1005 Can’t create table ‘.\mydb\#sql-328_45.frm’ (errno: 150)

MySQL Error Number 1005
Can’t create table ‘.\mydb\#sql-328_45.frm’ (errno: 150)

If you get this error while trying to create a foreign key, it can be pretty frustrating. The error about not being able to create a .frm file seems like it would be some kind of OS file permission error or something but this is not the case. This error has been reported as a bug on the MySQL developer list for ages, but it is actually just a misleading error message.

In every case this is due to something about the relationship that MySQL doesn’t like. Unfortunately it doesn’t specify what the exact issue is.

First Steps:

If you have admin permission on the server, you may want to start by running the MySQL command “SHOW INNODB STATUS” (or MySQL 5.5 “SHOW ENGINE INNODB STATUS”) immediately after receiving the error. This command displays log info and error details. (Thanks Jonathan for the tip)

If your script runs fine on one server, but gives an error when you try to run it on a different server, then there is a good chance that #6 is the problem.  Different versions of MySQL have different default charset setting and you may have unknowingly assigned different charsets on the different servers.

Known Causes:

Below is a running list of known causes that people have reported for the dreaded errno 150:

  1. The two key fields type and/or size is not an exact match. For example, if one is INT(10) the key field needs to be INT(10) as well and not INT(11) or TINYINT. You may want to confirm the field size using SHOW CREATE TABLE because Query Browser will sometimes visually show just INTEGER for both INT(10) and INT(11). You should also check that one is not SIGNED and the other is UNSIGNED. They both need to be exactly the same. (More about signed vs unsigned here).
  2. 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. (thanks to Venkatesh and Erichero and Terminally Incoherent for this tip)
  3. 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. (Thanks to Niels for this tip)
  4. One or both of your tables is a MyISAM table. In order to use foreign keys, the tables must both be InnoDB. (Actually, if both tables are MyISAM then you won’t get an error message – it just won’t create the key.) In Query Browser, you can specify the table type.
  5. You have specified a cascade ON DELETE SET NULL, but the relevant key field is set to NOT NULL.  You can fix this by either changing your cascade or setting the field to allow NULL values. (Thanks to Sammy and J Jammin)
  6. 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. (Thanks to FRR for this tip)
  7. You have a default value (ie default=0) on your foreign key column (Thanks to Omar for the tip)
  8. 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. (Thanks to Alex for this tip)
  9. You have a syntax error in your ALTER statement or you have mistyped one of the field names in the relationship (Thanks to Christian & Mateo for the tip)
  10. The name of your foreign key exceeds the max length of 64 chars.  (Thanks to Nyleta for the tip)

The MySQL documentation includes a page explaining requirements for foreign keys. Though they don’t specifically indicate it, these are all potential causes of errno 150. If you still haven’t solved your problem you may want to check there for deeper technical explainations.

If you run into this error and find that it’s caused by something else, please leave a comment and I’ll add it to the list.

234 Responses to “MySQL Error Number 1005 Can’t create table ‘.\mydb\#sql-328_45.frm’ (errno: 150)”

  1. tatata September 7, 2012 at 3:14 pm #

    Consider the following sql statements

    CREATE TABLE `schools` (
    `schoolname` varchar(50) CHARACTER SET utf8 NOT NULL,
    PRIMARY KEY (`schoolname`)

    CREATE TABLE `departments` (
    `departmentname` varchar(100) NOT NULL,
    `schoolname` varchar(50) DEFAULT NULL,
    PRIMARY KEY (`departmentname`),
    KEY `schoolname` (`schoolname`),
    KEY `ds` (`schoolname`),
    CONSTRAINT `ds` FOREIGN KEY (`schoolname`) REFERENCES `schools` (`schoolname`)

    An attempt to create table department will fail with the same error 150, To solve the problem, make sure that the properties of the foreign key are identical.

    Solution One
    Insert the character set of the foreign key as defined in the primary table

    The departments table will be:
    CREATE TABLE `departments` (
    `departmentname` varchar(100) NOT NULL,
    `schoolname` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
    PRIMARY KEY (`departmentname`),
    KEY `schoolname` (`schoolname`),
    KEY `ds` (`schoolname`),
    CONSTRAINT `ds` FOREIGN KEY (`schoolname`) REFERENCES `schools` (`schoolname`)

    Solution 2:
    Modify the table school and remove the character set from it. The resultant statements will be

    CREATE TABLE `schools` (
    `schoolname` varchar(50) NOT NULL,
    PRIMARY KEY (`schoolname`)

    CREATE TABLE `departments` (
    `departmentname` varchar(100) NOT NULL,
    `schoolname` varchar(50) DEFAULT NULL,
    PRIMARY KEY (`departmentname`),
    KEY `schoolname` (`schoolname`),
    KEY `ds` (`schoolname`),
    CONSTRAINT `ds` FOREIGN KEY (`schoolname`) REFERENCES `schools` (`schoolname`)

    In general, make sure that the primary -and foreign key attributes have the same characteristics or properties.

  2. Cosmin September 11, 2012 at 9:12 am #

    Thanks a lot man, this list realy helped.

    wow .. and I just noticed this post is from 2006 :D

  3. ajay September 12, 2012 at 11:18 am #

    Thanks after a lot of headache found cure for my problem……. :D

  4. Hannes September 25, 2012 at 5:24 am #

    Thanks a lot. My problem was that the order in which the two-part PK differed from that I tried to set as FK.

  5. soba September 25, 2012 at 7:28 pm #

    Thanks it helped, was creating foreign key in child table.

  6. punkck September 26, 2012 at 7:54 am #

    You Saved me :)
    #3 was irritating me !

    Well Thankx Again

  7. Malcolm Boyle October 2, 2012 at 2:36 pm #



    to the end of every CREATE TABLE fixed it for me – thanks for the tips.

  8. Arvind October 26, 2012 at 12:26 am #

    Thanks for the tips. It saved me a lot of frustration…

  9. Alex November 6, 2012 at 3:20 am #

    Point 4 for me, MyISAM vs InnoDB.

    Thanks, hard to pinpoint. MySQL error messages could be a little more helpful!!!

  10. Rasha November 22, 2012 at 6:18 pm #

    U solved my problem! Thanks a lot=D
    it was the stupid “on delete set null”:/

  11. Mati November 29, 2012 at 2:53 am #

    Thank you!

  12. Kevin Sharp December 17, 2012 at 3:08 pm #

    Thanks for this article, it saved me a lot of frustration. It was problem #4 for me.

  13. Toxa December 27, 2012 at 3:18 am #

    try to see for signed/unsigned in key fields ;)

  14. Gabe January 7, 2013 at 11:15 am #

    I followed the steps but what finally worked for me is:
    SET foreign_key_checks = 0;

  15. Christie January 12, 2013 at 2:03 pm #

    Perfect. I hate getting this error message, and this helped me track it down within minutes. Bookmarking for future reference – thanks a ton!

  16. Stosh January 15, 2013 at 9:48 am #

    I have just discovered that it helps to define the table that will be referenced by the foreign key *before* the table with the foreign key.

  17. Jamie January 25, 2013 at 11:27 pm #

    Oh. My. Goodness.

    I read through this entire blog and did not find my situation listed…. so I kept working on the problem, and working, and working.

    I finally found it.

    You see, I’m using MySQL Workbench, which has a nice handy “comments” section that lets you nicely comment on all of your fields. Therein lie the problem for me. I was entering the comments the way I normally would for anything. This included the use of special characters: () ” ‘ . ; @ etc.

    I don’t know which one, but one of them messed up my script. You see, I returned to my comments and turned everything into pure text–just letters and numbers. Nothing else and…


    Here’s hoping I save someone from puling out their hair.

  18. Damian February 5, 2013 at 6:48 am #

    You should also check for typos or changes from development to production in DB/table names.

  19. Alexandra March 6, 2013 at 4:49 am #

    Your post helped me. Thanks !

  20. Nishant Nawarkhede March 14, 2013 at 5:20 am #

    The First one..
    The two key fields type and/or size is not an exact match.

    Thank you …

  21. GrayFox April 12, 2013 at 1:05 pm #

    My problem was solved by adding an index to the referenciated column of the referenciated table and erasing the data from the other table (the one I wanted to add a foreign key).
    Thanks a lot.

  22. WaveyA August 9, 2013 at 3:27 am #

    I’ve been struggling with this problem for years. Wish I had seen this before. Thanks.

  23. Szparag August 13, 2013 at 6:42 am #

    Remember that UNSIGNED attribute is also very important

  24. Gus October 28, 2013 at 6:36 am #

    For some reason I got this error when I accidentally used “table.column” syntax as opposed to “table (column)” for the referenced field.

    ERROR 1005 (HY000): Can’t create table ‘mydb.#sql-467_26′ (errno: 150)

    should be:

    Query OK, 0 rows affected (0.00 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    MySQL error messages are a complete mystery to me. They very rarely, if ever, seem to have any connection to the actual problem that occurred. It might as well just say “>Nope.” and put you back at the prompt.

  25. JoAnn Moak December 19, 2013 at 10:26 am #

    I followed through this while looking for a solution to a class assignment and still did not find the answer to my problem. However, I did eventually find it on my own. My table names and my key names were too similar. For example, I had a table named knife_id that contained a key named Knife_Id. I thought they would be different enough having used capital letters in the key. I was wrong. I kept getting this error until I removed _id from the table name. Then it finally accepted it.

    Hopefully, this might help someone else who has this issue.

  26. KevinW April 11, 2014 at 5:23 pm #

    My base table was “user” and my foreign key was trying to reference a table named “users”. Took me a while to realize that I had mistyped the table name.

Leave a Reply

Please leave these two fields as-is: