Ces*_*sar 0 mysql sql constraints foreign-keys
我有这个架构:
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
Run Code Online (Sandbox Code Playgroud)
我想添加这个:
ALTER TABLE `lotto`.`cartones` ADD CONSTRAINT `new_fk_56` FOREIGN KEY `new_fk_56` (`indice`)
REFERENCES `combinaciones` (`indice`)
ON DELETE RESTRICT
ON UPDATE RESTRICT;
Run Code Online (Sandbox Code Playgroud)
但INNODB一直抱怨没有找到一个指标:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns...
但它不是外键:combinaciones(indice)和外键一样sorteo(numeroSorteo)吗?,它正在发挥作用
编辑:
我已经测试了:KEY 'new_index' (indice )in not lotto.cartones和without.
`indice` mediumint(8) NOT NULL,
Run Code Online (Sandbox Code Playgroud)
与...不是同一类型
`indice` mediumint(8) unsigned NOT NULL,
Run Code Online (Sandbox Code Playgroud)
您需要同时使用indice无符号或无符号.