MYSQL帮我不要让我创建表

时间:2016-01-27 19:12:48

标签: mysql

我很困惑为什么我的代码无法正常工作。

CREATE TABLE DELIVERY (
VEHICLE_VEH_ID INT NOT NULL,
DRIVER_DR_ID INT NOT NULL,
DEL_DATE DATETIME NOT NULL,
DEL_TIME DATETIME NOT NULL,
PRIMARY KEY (VEHICLE_VEH_ID , DRIVER_DR_ID)
INDEX (DRIVER_DR_ID),
INDEX (VEHICLE_VEH_ID),
CONSTRAINT FK_VEHICLE_HAS_DRIVER_VEHICLE FOREIGN KEY (VEHICLE_VEH_ID) REFERENCES VEHICLE (VEH_ID) 
CONSTRAINT FK_VEHICLE_HAS_DRIVER_DRIVER FOREIGN KEY (DRIVER_DR_ID) DRIVER (DR_ID));

CREATE TABLE DRIVER (
 DR_ID INT NOT NULL PRIMARY KEY,
 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);

CREATE TABLE VEHICLE (
 VEH_ID INT NOT NULL PRIMARY KEY,
 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);





mysql> CREATE TABLE DELIVERY (
    -> VEHICLE_VEH_ID INT NOT NULL,
    -> DRIVER_DR_ID INT NOT NULL,
    -> DEL_DATE DATETIME NOT NULL,
    -> DEL_TIME DATETIME NOT NULL,
    -> PRIMARY KEY (VEHICLE_VEH_ID , DRIVER_DR_ID)
    -> INDEX (DRIVER_DR_ID),
    -> INDEX (VEHICLE_VEH_ID),
    -> CONSTRAINT FK_VEHICLE_HAS_DRIVER_VEHICLE , CONSTRAINT FK_VEHICLE_HAS_DRIVER_DRIVER  FOREIGN KEY (VEHICLE_VEH_ID) REFERENCES VEHICLE (VEH_ID) FOREIGN KEY (DRIVER_DR_ID) DRIVER (DR_ID));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INDEX (DRIVER_DR_ID),
INDEX (VEHICLE_VEH_ID),
CONSTRAINT FK_VEHICLE_HAS_DRIVER_V' at line 7
mysql>

我试图创建一个表,但它说我有一个错误,当我不认为我这样做。如果你能看出我做错了什么,我会非常感激的。谢谢大家的帮助!

2 个答案:

答案 0 :(得分:2)

你忘记了一个逗号。

...
PRIMARY KEY (VEHICLE_VEH_ID , DRIVER_DR_ID), -- Need a comma here
INDEX (DRIVER_DR_ID),
...

答案 1 :(得分:2)

首先 需要创建这两个表:

CREATE TABLE DRIVER (
 DR_ID INT NOT NULL PRIMARY KEY,
 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);

CREATE TABLE VEHICLE (
 VEH_ID INT NOT NULL PRIMARY KEY,
 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);

然后 使用此sql代码创建最后一个,引用上面的两个表:

CREATE TABLE DELIVERY (
VEHICLE_VEH_ID INT NOT NULL,
DRIVER_DR_ID INT NOT NULL,
DEL_DATE DATETIME NOT NULL,
DEL_TIME DATETIME NOT NULL,
PRIMARY KEY (VEHICLE_VEH_ID , DRIVER_DR_ID),
INDEX (DRIVER_DR_ID),
INDEX (VEHICLE_VEH_ID),
CONSTRAINT FK_VEHICLE_HAS_DRIVER_VEHICLE FOREIGN KEY (VEHICLE_VEH_ID) REFERENCES VEHICLE (VEH_ID) ,
CONSTRAINT FK_VEHICLE_HAS_DRIVER_DRIVER FOREIGN KEY (DRIVER_DR_ID) REFERENCES DRIVER (DR_ID));

您的代码在REFERENCES外键约束中缺少两个逗号和FK_VEHICLE_HAS_DRIVER_DRIVER关键字。

Demo here