2009-09-27 32 views
0

我有這樣的模式:爲什麼不能添加這個外鍵?

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,沒有它:

我已經與測試。

+0

這是來自'SHOW ENGINE INNODB STATUS'的最後一個外鍵錯誤嗎? – derobert

回答

5
`indice` mediumint(8) NOT NULL, 

是不一樣的類型

`indice` mediumint(8) unsigned NOT NULL, 

你需要讓你的兩個簽名indice或都沒有的無符號。

+0

謝謝,解決了我的問題! – Cesar