Sql 为什么不能添加此外键?
我有这个模式:Sql 为什么不能添加此外键?,sql,mysql,foreign-keys,constraints,Sql,Mysql,Foreign Keys,Constraints,我有这个模式: CREATE TABLE `lotto`.`combinaciones` ( `indice` mediumint(8) unsigned NOT NULL, `binario` int(10) unsigned NOT NULL, PRIMARY KEY USING BTREE (`indice`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 CREATE TABLE `lotto`.`sorteo` ( `numeroSo
CREATE TABLE `lotto`.`combinaciones` (
`indice` mediumint(8) unsigned NOT NULL,
`binario` int(10) unsigned NOT NULL,
PRIMARY KEY USING BTREE (`indice`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `lotto`.`sorteo` (
`numeroSorteo` int(11) NOT NULL,
`fechaSorteo` date NOT NULL,
`precioCarton` double NOT NULL,
`valorSerial` double NOT NULL,
`valorMiniserial` double NOT NULL,
`estatusSorteo` int(11) NOT NULL,
`cantidadCartones` int(11) NOT NULL,
PRIMARY KEY (`numeroSorteo`),
UNIQUE KEY `fechaSorteo` (`fechaSorteo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `lotto`.`cartones` (
`numeroSorteo` int(11) NOT NULL,
`serial` mediumint(9) NOT NULL,
`indice` mediumint(8) NOT NULL,
`binario` int(11) NOT NULL,
`miniserial` smallint(6) NOT NULL,
`estatus` tinyint(4) NOT NULL default '0',
PRIMARY KEY (`numeroSorteo`,`serial`),
KEY `new_index` (`indice`), -- ADD LATER
CONSTRAINT `cartones_ibfk_1` FOREIGN KEY (`numeroSorteo`) REFERENCES `sorteo` (`numeroSorteo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
我想补充一点:
ALTER TABLE `lotto`.`cartones` ADD CONSTRAINT `new_fk_56` FOREIGN KEY `new_fk_56` (`indice`)
REFERENCES `combinaciones` (`indice`)
ON DELETE RESTRICT
ON UPDATE RESTRICT;
但INNODB一直抱怨找不到索引:
在引用的表中找不到引用列作为第一列出现的索引
但它不是外键:combinaciones(indice)
与外键sorteo(numeroSorteo)
?,它是工作的
编辑:
我用:键“新索引”(
indice)
在lotto.cartones
和不带它的情况下进行了测试
`indice` mediumint(8) NOT NULL,
与的类型不同
`indice` mediumint(8) unsigned NOT NULL,
您需要将两个
标记为未签名或两个都未签名。这是显示引擎INNODB状态的最后一个外键错误吗?