2016-03-03 95 views
0

SQL查詢:MySQL的外鍵約束問題,當

CREATE TABLE IF NOT EXISTS `Rio VAMOOS Database`.`Card` (
    `Card_id` INT(8) NOT NULL, 
    `Card_start_date` DATETIME NOT NULL, 
    `Card_end_date` DATETIME NOT NULL, 
    `State_validity` VARCHAR(8) NOT NULL, 
    `Official_Official_id` INT(8) NOT NULL, 
    PRIMARY KEY (`Card_id`), 
    INDEX `fk_Card_State1_idx` (`State_validity` ASC), 
    INDEX `fk_Card_Official1_idx` (`Official_Official_id` ASC), 
    CONSTRAINT `fk_Card_State1` 
    FOREIGN KEY (`State_validity`) 
    REFERENCES `Rio VAMOOS Database`.`State` (`Card_id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_Card_Official1` 
    FOREIGN KEY (`Official_Official_id`) 
    REFERENCES `Rio VAMOOS Database`.`Official` (`Official_id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 

MySQL表示:文件

1215 - 不能添加外鍵約束

+0

我們似乎無法弄清楚爲什麼我們在導入時得到錯誤代碼,我們已經多次檢查過,但找不到問題。任何幫助將非常感激!! –

+0

檢查外鍵是否正常,'State_validity'需要與'State'中的'Card_id'具有相同的類型。 'Official_Official_id'和'Official_id'一樣 – Sal00m

回答

0

有也應該是表Rio VAMOOS DatabaseStateRio VAMOOS DatabaseOfficial。沒有這些表格,您將在嘗試運行此查詢時獲得ERROR 1215 (HY000): Cannot add foreign key constraint

您應該檢查這兩個表是否在Rio VAMOOS Database之前導入。 Card