MySQL Forums
Forum List  »  InnoDB

A solution to: ERROR 1005: Can't create table (errno: 150)
Posted by: Andrew Pitonyak
Date: September 10, 2008 12:37PM

I tried a command such as:

ALTER TABLE TABLE1 ADD ( FOREIGN KEY (FIELD1) REFERENCES TABLE2(FIELD2) ) ;

The generated error is of the form:

ERROR 1005 (HY000): Can't create table '.\schema_name\#sql-97c_c.frm' (errno: 150)

The error lead me to believe that it could not create the modified table specified by the alter command. First, I inspected FIELD1 in TABLE1.

SHOW CREATE TABLE TABLE1;

This shows the details required to create TABLE1, including very specific details related to the individual fields. Pay attention to the exact definition for FIELD1 and the character set for the field or table. Now, repeat for FIELD2 in TABLE2.

If the fields are not declared exactly alike, then use the alter command to make them the same. For example, I had something like:

FIELD2 varchar(20) DEFAULT NULL
FIELD1 varchar(20) NOT NULL DEFAULT ''

The fields are very similar, but they are not exactly the same. Now, I do not know for certain that they really must be exactly the same, but it is something that I did.

ALTER TABLE TABLE2 CHANGE FIELD2 FIELD2 VARCHAR(20) NOT NULL DEFAULT '';

If required, do the same for both tables and fields. I still received the error. Next, I created an index on FIELD2.

ALTER TABLE TABLE2 ADD INDEX (FIELD2);

Finally, creating the foreign key works.

Options: ReplyQuote


Subject
Views
Written By
Posted
644481
March 24, 2005 01:20PM
5076
October 09, 2012 09:48PM
3382
December 19, 2011 10:13PM
8778
May 09, 2006 06:01PM
7980
November 15, 2006 02:05PM
7026
T D
February 01, 2008 06:47AM
8833
August 27, 2008 08:08AM
A solution to: ERROR 1005: Can't create table (errno: 150)
15103
September 10, 2008 12:37PM
9363
December 03, 2008 10:15AM
5803
May 01, 2009 07:39AM
3040
August 29, 2011 04:46PM
4645
s l
December 15, 2009 01:48PM
4617
December 22, 2009 02:54AM
2893
February 18, 2011 08:55AM
3458
May 24, 2011 10:11AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.