MySQL错误:#1005 - 无法创建表(错误号:150)当我尝试创建超过1个FK时

时间:2022-09-11 15:27:19

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):

我已经创建了一个fk sugestoes.idproduto - > produtos.id工作,但我希望其他每个字段也通过新的FK引用produtos.id。运行以下命令返回MySQL错误:#1005 - 无法创建表(错误号: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?

有谁知道发生了什么事?

2 个解决方案

#1


10  

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

您已定义SET NULL条件,但某些列定义为NOT NULL

You can see exact error when you run

您可以在运行时看到确切的错误

SHOW ENGINE INNODB STATUS;

#2


0  

Perhaps removing the ROW_FORMAT = FIXED:

也许删除ROW_FORMAT = FIXED:

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
;

On second thought, how do you expect the ON DELETE SET NULL to behave when your column is defined with NOT NULL ?

第二个想法,当你的列定义为NOT NULL时,你如何期望ON DELETE SET NULL能够表现?


And third, does the table has any data in it? If some of the rows violate this FK constraint, then you can't create that FK.

第三,表中是否有任何数据?如果某些行违反此FK约束,则无法创建该FK。

#1


10  

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

您已定义SET NULL条件,但某些列定义为NOT NULL

You can see exact error when you run

您可以在运行时看到确切的错误

SHOW ENGINE INNODB STATUS;

#2


0  

Perhaps removing the ROW_FORMAT = FIXED:

也许删除ROW_FORMAT = FIXED:

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
;

On second thought, how do you expect the ON DELETE SET NULL to behave when your column is defined with NOT NULL ?

第二个想法,当你的列定义为NOT NULL时,你如何期望ON DELETE SET NULL能够表现?


And third, does the table has any data in it? If some of the rows violate this FK constraint, then you can't create that FK.

第三,表中是否有任何数据?如果某些行违反此FK约束,则无法创建该FK。