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

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值