2013-05-26 85 views
0

這是我的模式來創建三個表。第三個有以下兩個外鍵。我不知道MySQL爲什麼不能創建第三個。列的類型匹配。MySQL返回:errno:150 - 外鍵錯誤

DROP TABLE IF EXISTS `SET`; 
CREATE TABLE IF NOT EXISTS `SET` (
    `ID` int(11) NOT NULL AUTO_INCREMENT, 
    `SET_ID` int(11) DEFAULT 0, 
    `SET_NAME` varchar(32) NOT NULL, 
    `SET_RELEASE_DATE` date NOT NULL, 
    PRIMARY KEY (`ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

DROP TABLE IF EXISTS `TYPE`; 
CREATE TABLE IF NOT EXISTS `TYPE` (
    `ID` int(11) NOT NULL AUTO_INCREMENT, 
    `TYPE_ID` int(11) DEFAULT 0, 
    `TYPE_NAME` varchar(32) NOT NULL, 
    PRIMARY KEY (`ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

DROP TABLE IF EXISTS `CARD`; 
CREATE TABLE IF NOT EXISTS `CARD` (
    `ID` int(11) NOT NULL AUTO_INCREMENT, 
    `CARD_ID` int(11) NOT NULL DEFAULT 0, 
    `CARD_NAME` varchar(64) DEFAULT NULL, 
    `SET_ID` int(11) DEFAULT 0, 
    `TYPE_1` int(11) DEFAULT 0, 
    `TYPE_2` int(11) DEFAULT 0, 
    `TYPE_3` int(11) DEFAULT 0, 
    `TYPE_4` int(11) DEFAULT 0, 
    PRIMARY KEY (`ID`), 
    CONSTRAINT `card_idfk_1` FOREIGN KEY (`SET_ID`) 
    REFERENCES `SET`(`SET_ID`) ON DELETE CASCADE ON UPDATE CASCADE, 
    CONSTRAINT `card_t1fk_1` FOREIGN KEY (`TYPE_1`) 
    REFERENCES `TYPE`(`TYPE_ID`) ON DELETE CASCADE ON UPDATE CASCADE, 
    CONSTRAINT `card_t2fk_1` FOREIGN KEY (`TYPE_2`) 
    REFERENCES `TYPE`(`TYPE_ID`) ON DELETE CASCADE ON UPDATE CASCADE, 
    CONSTRAINT `card_t3fk_1` FOREIGN KEY (`TYPE_3`) 
    REFERENCES `TYPE`(`TYPE_ID`) ON DELETE CASCADE ON UPDATE CASCADE, 
    CONSTRAINT `card_t4fk_1` FOREIGN KEY (`TYPE_4`) 
    REFERENCES `TYPE`(`TYPE_ID`) ON DELETE CASCADE ON UPDATE CASCADE 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

回答

1

當你創建一個外鍵關係,有問題的鍵必須對被引用表的索引(見here)。

例如,下面的代碼:

DROP TABLE IF EXISTS `SET`; 
CREATE TABLE IF NOT EXISTS `SET` (
    `ID` int(11) NOT NULL AUTO_INCREMENT, 
    `SET_ID` int(11) NOT NULL DEFAULT 0, 
    `SET_NAME` varchar(32) NOT NULL, 
    `SET_RELEASE_DATE` date NOT NULL, 
    PRIMARY KEY (`ID`), 
    index(set_id) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

修復的card_idfk_1約束的問題。注意:您可以使用keyindex作爲關鍵字。

+0

謝謝,我不記得我的課。我認爲類型必須匹配。 –