MySql Error 150 - Foreign keys

2019-01-03 18:16发布

When I execute the follow two queries (I have stripped them down to absolutely necessary):

mysql> CREATE TABLE foo(id INT PRIMARY KEY);
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE bar ( id INT, ref INT, FOREIGN KEY (ref) REFERENCES foo(id)) ENGINE InnoDB;

I get the following error: ERROR 1005 (HY000): Can't create table './test/bar.frm' (errno: 150)

Where the **** is my error? I haven't found him while staring at this for half an hour.

7条回答
时光不老,我们不散
2楼-- · 2019-01-03 18:38

From FOREIGN KEY Constraints

If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.

My suspicion is that it's because you didn't create foo as InnoDB, as everything else looks OK.

Edit: from the same page -

Both tables must be InnoDB tables and they must not be TEMPORARY tables.

查看更多
放我归山
3楼-- · 2019-01-03 18:44

I had very same problem and the reason was the "collation" of columns was different. One was latin1 while the other was utf8

查看更多
祖国的老花朵
4楼-- · 2019-01-03 18:48

To create a foreign key ,

  1. both the main column and the reference column must have same definition.
  2. both tables engine must be InnoDB.

You can alter the engine of table using this command , please take the backup before executing this command.

alter table [table name] ENGINE=InnoDB;

查看更多
Evening l夕情丶
5楼-- · 2019-01-03 18:57

You can use the command SHOW ENGINE INNODB STATUS

查看更多
Juvenile、少年°
6楼-- · 2019-01-03 18:59

This may also happen if you have not given correct column name after "references" keyword.

查看更多
我命由我不由天
7楼-- · 2019-01-03 19:02

Apart form many other reasons to end up with MySql Error 150 (while using InnoDB), One of the probable reason, is the undefined KEY in the create statement of the table containing the column name referenced as a foreign key in the relative table.

Let's say the create statement of master table is -

CREATE TABLE 'master_table' (
 'id' int(10) NOT NULL AUTO_INCREMENT,
 'record_id' char(10) NOT NULL,
 'name' varchar(50) NOT NULL DEFAULT '',
 'address' varchar(200) NOT NULL DEFAULT '',
 PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

and the create syntax for the relative_table table where the foreign key constraint is set from primary table -

CREATE TABLE 'relative_table' (
 'id' int(10) NOT NULL AUTO_INCREMENT,
 'salary' int(10) NOT NULL DEFAULT '',
 'grade' char(2) NOT NULL DEFAULT '',
 'record_id' char(10) DEFAULT NULL,
 PRIMARY KEY ('id'),
 CONSTRAINT 'fk_slave_master' FOREIGN KEY ('record_id') REFERENCES 'master' ('record_id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

This script is definitely going to end with MySql Error 150 if using InnoDB.

To solve this, we need to add a KEY for the The column record_id in the master_table table and then reference in the relative_table table to be used as a foreign_key.

Finally, the create statement for the master_table, will be -

CREATE TABLE 'master_table' (
 'id' int(10) NOT NULL AUTO_INCREMENT,
 'record_id' char(10) NOT NULL,
 'name' varchar(50) NOT NULL DEFAULT '',
 'address' varchar(200) NOT NULL DEFAULT '',
 PRIMARY KEY ('id'),
 KEY 'record_id' ('record_id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

查看更多
登录 后发表回答