MySQL错误:#1005 - 无法创建表(errno:150)当我尝试创建超过1个FK [英] MySQL Error : #1005 - Can't create table (errno: 150) When I try create more than 1 FK

查看:221
本文介绍了MySQL错误:#1005 - 无法创建表(errno:150)当我尝试创建超过1个FK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个表:

pre $ code $ CREATE TABLE IF NOT EXISTS``produtos`(
`id` int 11)NOT NULL auto_increment,
`idcatprodutos` int(11)NOT NULL,
`idcategoria` int(11)NOT NULL,
`idmarca` int(11)NOT NULL,
`nome` varchar(100)NOT NULL,
PRIMARY KEY(`id`),
KEY`FK_produtos_2`(`idcatprodutos`),
KEY`FK_produtos_3`(`idmarca` ),
KEY`FK_produtos_4`(`idcategoria`)
)ENGINE = InnoDB DEFAULT CHARSET = latin1 ROW_FORMAT = DYNAMIC AUTO_INCREMENT = 39;

和这张表:

` idproduto` int(11)NOT NULL,
` (11)非空,
`idsugestao2` int(11)非空,
`idsugestao3` int(11)非空,
`idsugestao4` int(11) ,
PRIMARY KEY(`id`),
KEY`FK_sugestoes_prod`(`idproduto`)
)ENGINE = InnoDB DEFAULT CHARSET = latin1 ROW_FORMAT = FIXED AUTO_INCREMENT = 9;

我已经创建了一个fk sugestoes.idproduto - > produtos.id 工作,但我希望每个其他字段也通过新的FK引用 produtos.id
运行下面这个命令返回MySQL错误:#1005 - 不能创建表(errno:150):

$ $ $ $ $ $ $ $ $ > ALTER TABLE`infantile`.`sugestoes` ADD CONSTRAINT`FK_sugestoes_2` FOREIGN KEY`FK_sugestoes_2`(`idsugestao1`)
REFERENCES`produtos`(`id`)
ON DELETE SET NULL
ON UPDATE CASCADE
,ROW_FORMAT = FIXED;

有谁知道发生了什么事?

解决方案

试试这个,


$ b $

  ALTER TABLE`sugestoes` 
ADD CONSTRAINT`FK_idproduto_produtos_1` FOREIGN KEY(`idproduto`)REFERENCES`produtos`(`id`) ,
ADD CONSTRAINT`FK_sugestoes_produtos_2` FOREIGN KEY(`idsugestao1`)REFERENCES`produtos`(`id`),
ADD CONSTRAINT`FK_sugestoes_produtos_3` FOREIGN KEY(`idsugestao2`)REFERENCES`produtos`(`id ('id`),
ADD CONSTRAINT`FK_sugestoes_produtos_5` FOREIGN KEY(`idsugestao4`)REFERENCES`produtos`( ```

更新:

你不能指定

  ON DELETE SET NULL 

因此:

您已经定义了一个SET NULL条件,尽管一些
列被定义为NOT NULL



你可以看到确切的错误,当哟你运行

  SHOW ENGINE INNODB STATUS; 


I have this table:

CREATE TABLE IF NOT EXISTS `produtos` (
  `id` int(11) NOT NULL auto_increment,
  `idcatprodutos` int(11) NOT NULL,
  `idcategoria` int(11) NOT NULL,
  `idmarca` int(11) NOT NULL,
  `nome` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `FK_produtos_2` (`idcatprodutos`),
  KEY `FK_produtos_3` (`idmarca`),
  KEY `FK_produtos_4` (`idcategoria`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=39 ;

and this table:

CREATE TABLE IF NOT EXISTS `sugestoes` (
  `id` int(11) NOT NULL auto_increment,
  `idproduto` int(11) NOT NULL,
  `idsugestao1` int(11) NOT NULL,
  `idsugestao2` int(11) NOT NULL,
  `idsugestao3` int(11) NOT NULL,
  `idsugestao4` int(11) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `FK_sugestoes_prod` (`idproduto`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED AUTO_INCREMENT=9 ;

I already have created a fk sugestoes.idproduto -> produtos.id working, but I want each of the other fields also refer to the produtos.id through new FK. Run this command below that return MySQL Error : #1005 - Can't create table (errno: 150):

ALTER TABLE `infantile`.`sugestoes` ADD CONSTRAINT `FK_sugestoes_2` FOREIGN KEY `FK_sugestoes_2` (`idsugestao1`)
    REFERENCES `produtos` (`id`)
    ON DELETE SET NULL
    ON UPDATE CASCADE
, ROW_FORMAT = FIXED;

Does anyone have any idea what's going on?

解决方案

Try this,

it works:

ALTER TABLE `sugestoes`
ADD CONSTRAINT `FK_idproduto_produtos_1` FOREIGN KEY (`idproduto`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_2` FOREIGN KEY (`idsugestao1`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_3` FOREIGN KEY (`idsugestao2`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_4` FOREIGN KEY (`idsugestao3`) REFERENCES `produtos` (`id`),  
ADD CONSTRAINT `FK_sugestoes_produtos_5` FOREIGN KEY (`idsugestao4`) REFERENCES `produtos` (`id`)

UPDATE:

You can not specify

ON DELETE SET NULL

Because of this:

You have defined a SET NULL condition though some of the columns are defined as NOT NULL

You can see exact error when you run

SHOW ENGINE INNODB STATUS;

这篇关于MySQL错误:#1005 - 无法创建表(errno:150)当我尝试创建超过1个FK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆