MySQL: Can't create table (errno: 150)

2018-12-31 12:02发布

I am trying to import a .sql file and its failing on creating tables.

Here's the query that fails:

CREATE TABLE `data` (
`id` int(10) unsigned NOT NULL,
`name` varchar(100) NOT NULL,
`value` varchar(15) NOT NULL,
UNIQUE KEY `id` (`id`,`name`),
CONSTRAINT `data_ibfk_1` FOREIGN KEY (`id`) REFERENCES `keywords` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;    

I exported the .sql from the the same database, I dropped all the tables and now im trying to import it, why is it failing?

MySQL: Can't create table './dbname/data.frm' (errno: 150)

30条回答
听够珍惜
2楼-- · 2018-12-31 12:40

I had the same error. In my case the reason for the error was that I had a ON DELETE SET NULL statement in the constraint while the field on which I put the constraint in its definition had a NOT NULL statement. Allowing NULL in the field solved the problem.

查看更多
心情的温度
3楼-- · 2018-12-31 12:42

The column of PARENT table to which you are referring to from child table has to be unique. If it is not, cause an error no 150.

查看更多
何处买醉
4楼-- · 2018-12-31 12:43

In some cases, you may encounter this error message if there are different engines between the relating tables. For example, a table may be using InnoDB while the other uses MyISAM. Both need to be same

查看更多
与君花间醉酒
5楼-- · 2018-12-31 12:43

After cruising through the answers above, and experimenting a bit, this is an effective way to solve Foreign Key errors in MySQL (1005 - error 150).

For the foreign key to be properly created, all MySQL asks for is:

  • All referenced keys MUST have either PRIMARY or UNIQUE index.
  • Referencing Column again MUST have identical data type to the Referenced column.

Satisfy these requirements and all will be well.

查看更多
深知你不懂我心
6楼-- · 2018-12-31 12:43

Make sure that the all tables can support foreign key - InnoDB engine

查看更多
步步皆殇っ
7楼-- · 2018-12-31 12:44

In most of the cases the problem is because of the ENGINE dIfference .If the parent is created by InnoDB then the referenced tables supposed to be created by MyISAM & vice versa

查看更多
登录 后发表回答