我有這樣的模式:爲什麼不能添加這個外鍵?
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一直抱怨沒有找到一個指數之:
Cannot find an index in the referenced table where the referenced columns appear as the first columns...
但它的不是外鍵:combinaciones(indice)
與外鍵相同sorteo(numeroSorteo)
?,這是工作NG
編輯:KEY 'new_index' (
指數之)
在lotto.cartones
,沒有它:
我已經與測試。
這是來自'SHOW ENGINE INNODB STATUS'的最後一個外鍵錯誤嗎? – derobert