2016-02-04 148 views
2
CREATE TABLE `ROUTE` (
    `route_ID` INT NOT NULL, 
    `route_name` VARCHAR(45) NOT NULL, 
    `DELIVERY_VEHICLE_veh_ID` INT NOT NULL, 
    `DELIVERY_DRIVER_dr_ID` INT NOT NULL, 
    PRIMARY KEY (`route_ID`), 
    CONSTRAINT `fk_ROUTE_DELIVERY1` 
    FOREIGN KEY (`DELIVERY_VEHICLE_veh_ID` , `DELIVERY_DRIVER_dr_ID`) 
    (`VEHICLE_veh_ID` , `DRIVER_dr_ID`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION); 

我是SQL新手,嘗試通過練習來學習。我的語法有一個錯誤,我不明白。任何幫助/建議?SQL新手,CREATE TABLE錯誤

第9行

VEHICLE_veh_IDDRIVER_dr_ID

CREATE TABLE `DRIVER` (
    `dr_ID` INT NOT NULL, 
    `dr_title` VARCHAR(15) NOT NULL, 
    `dr_fname` VARCHAR(45) NOT NULL, 
    `dr_lname` VARCHAR(45) NOT NULL, 
    `dr_DOB` DATETIME NOT NULL, 
    `dr_licenceNo` VARCHAR(45) NOT NULL, 
    `dr_phone` VARCHAR(15) NOT NULL, 
    `dr_email` VARCHAR(45) NOT NULL, 
    PRIMARY KEY (`dr_ID`)); 

CREATE TABLE `VEHICLE` (
    `veh_ID` INT NOT NULL, 
    `veh_reg` VARCHAR(15) NOT NULL, 
    `veh_make` VARCHAR(45) NOT NULL, 
    `veh_model` VARCHAR(45) NOT NULL, 
    `veh_mileage` INT NOT NULL, 
    `veh_MOTdate` DATETIME NOT NULL, 
    `veh_servicedate` DATETIME NOT NULL, 
    PRIMARY KEY (`veh_ID`)); 

這些途徑是相關的其他表。我沒有將DRIVER和VEHICLE表插入數據庫的問題,ROUTE出了什麼問題?

+0

'外鍵(DELIVERY_VEHICLE_veh_ID)參考車輛(veh_ID)'等等... – shawnt00

回答

0
CREATE TABLE `ROUTE` (
    `route_ID` INT NOT NULL, 
    `route_name` VARCHAR(45) NOT NULL, 
    `DELIVERY_VEHICLE_veh_ID` INT NOT NULL, 
    `DELIVERY_DRIVER_dr_ID` INT NOT NULL, 
    PRIMARY KEY (`route_ID`), 
    CONSTRAINT `fk_ROUTE_VEHICLE` 
    FOREIGN KEY (`DELIVERY_VEHICLE_veh_ID`) 
    REFERENCES `VEHICLE`(`veh_ID`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 

    CONSTRAINT `fk_ROUTE_DRIVER` 
    FOREIGN KEY (`DELIVERY_DRIVER_dr_ID`) 
    REFERENCES `DRIVER`(`dr_ID`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION); 
2

看起來好像有兩個這個ROUTE表中的外鍵。

其中一個參考DRIVER表。

其他參考VEHICLE表。

你的外鍵約束定義應該看起來更像是這樣的:

, CONSTRAINT `fk_ROUTE_DELIVERY_DRIVER` 
    FOREIGN KEY (`DELIVERY_DRIVER_dr_ID`) 
     REFERENCES `DRIVER` (`dr_ID`) 
     ON DELETE RESTRICT ON UPDATE RESTRICT 

    , CONSTRAINT `fk_ROUTE_DELIVERY_VEHICLE` 
    FOREIGN KEY (`DELIVERY_VEHICLE_veh_ID`) 
     REFERENCES `VEHICLE` (`veh_ID`) 
     ON DELETE RESTRICT ON UPDATE RESTRICT