MySQL数据库错误#1064语法错误

时间:2014-04-09 05:26:20

标签: mysql sql database

帮助! 我一直在收到错误。

  

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法   'MGRDATE DATE,PRIMARY KEY(DNUM),FOREIGN KEY(MGRSSN)附近   参考EMPL'第6行

CREATE TABLE EMPLOYEE ( 
    SSN INT NOT NULL, 
    FNAME VARCHAR(15) NOT NULL, 
    MINIT VARCHAR(1) NOT NULL, 
    LNAME VARCHAR(15) NOT NULL, 
    BDATE DATE, 
    ADDRESS VARCHAR(50), 
    SEX VARCHAR(1), 
    SALARY INT NOT NULL, 
    SUPERSSN INT NOT NULL, 
    DNO INT NOT NULL, 
    PRIMARY KEY (SSN)
);

CREATE TABLE DEPARTMENT ( 
    DNUM INT NOT NULL, 
    DNAME VARCHAR(15) NOT NULL, 
    MGRSSN INT NOT NULL,
    INDEX (MSGSSN)
    MGRDATE DATE, 
    PRIMARY KEY (DNUM), 
    FOREIGN KEY (MGRSSN) 
        REFERENCES EMPLOYEE(SSN)
        ON DELETE CASCADE,
); 

CREATE TABLE WORKS_ON ( 
    SSN INT NOT NULL, 
    INDEX (SSN)
    PNO INT NOT NULL,
    INDEX (PNO) 
    HOURS INT NOT NULL, 
    FOREIGN KEY (SSN) 
        REFERENCES EMPLOYEE(SSN)
        ON DELETE CASCADE,
    FOREIGN KEY (PNO) 
        REFERENCES PROJECT(PINT)
        ON DELETE CASCADE,
); 

CREATE TABLE DEPT_LOCATION ( 
    DNUM INT NOT NULL, 
    DLOCATION VARCHAR(20) NOT NULL
);

CREATE TABLE DEPENDENT ( 
    SSN INT NOT NULL, 
    INDEX (SSN)
    DEPENDENT_NAME VARCHAR(10) NOT NULL, 
    SEX VARCHAR(1) NOT NULL, 
    DOB DATE, 
    RELATIONSHIP VARCHAR(10) NOT NULL,
    FOREIGN KEY (SSN) 
        REFERENCES EMPLOYEE(SSN)
        ON DELETE CASCADE,
);

CREATE TABLE PROJECT (
    PNAME VARCHAR(15) NOT NULL, 
    PINT INT NOT NULL, 
    PLOCATION VARCHAR(20) NOT NULL,
    INDEX (PLOCATION) 
    DNUM INT NOT NULL, 
    INDEX (DNUM)
    PRIMARY KEY (PINT), 
    FOREIGN KEY (PLOCATION) 
        REFERENCES DEPT_LOCATION(DLOCATION)
        ON DELETE CASCADE,
    FOREIGN KEY (DNUM) 
        REFERENCES “DEPARTMENT” (DNUM)
        ON DELETE CASCADE,
); 

2 个答案:

答案 0 :(得分:1)

您使用 , 地段,您应该删除查询中的 comma

您应该更改以下表格:

1.DEPARTMENT 
2.WORKS_ON 
3.DEPENDENT 
4.PROJECT 

您的查询:

 ON DELETE CASCADE,  -- -->remove the comma
); 

更改为:

 ON DELETE CASCADE
); 

您应该在spelling mistake表中执行department,如下所示:

您的查询:

    MGRSSN INT NOT NULL,
    INDEX (MSGSSN)

更改为:

      MGRSSN INT NOT NULL,    
      INDEX (MGRSSN) ,

答案 1 :(得分:0)

在您的代码中,您在某些行中错过了逗号(,)并且还添加了逗号。

在DEPARTMENT表中,您给出列名MGRSSN并用作INDEX(MSGSSN),错误使用INDEX(MGRSSN)而不是INDEX(MSGSSN)。

在WORKS_ON表中,您可以使用PROJECT表放置一个外键,并尝试在PROJECT表之前创建WORKS_ON表。

在PROJECT表中,您尝试为DEPT_LOCATION(DLOCATION)分配外键,但在DEPT_LOCATION中没有为DLOCATION分配主键,因此应该发生错误。

请尝试以下代码。

CREATE TABLE EMPLOYEE ( 
    SSN INT NOT NULL, 
    FNAME VARCHAR(15) NOT NULL, 
    MINIT VARCHAR(1) NOT NULL, 
    LNAME VARCHAR(15) NOT NULL, 
    BDATE DATE, 
    ADDRESS VARCHAR(50), 
    SEX VARCHAR(1), 
    SALARY INT NOT NULL, 
    SUPERSSN INT NOT NULL, 
    DNO INT NOT NULL, 
    PRIMARY KEY (SSN)
);

CREATE TABLE DEPARTMENT ( 
    DNUM INT NOT NULL, 
    DNAME VARCHAR(15) NOT NULL, 
    MGRSSN INT NOT NULL,
    INDEX (MGRSSN),
    MGRDATE DATE, 
    PRIMARY KEY (DNUM), 
    FOREIGN KEY (MGRSSN) 
        REFERENCES EMPLOYEE(SSN)
        ON DELETE CASCADE
);

CREATE TABLE DEPT_LOCATION ( 
    DNUM INT NOT NULL, 
    DLOCATION VARCHAR(20) NOT NULL, 
    PRIMARY KEY (DLOCATION)
);

CREATE TABLE DEPENDENT ( 
    SSN INT NOT NULL, 
    INDEX (SSN),
    DEPENDENT_NAME VARCHAR(10) NOT NULL, 
    SEX VARCHAR(1) NOT NULL, 
    DOB DATE, 
    RELATIONSHIP VARCHAR(10) NOT NULL,
    FOREIGN KEY (SSN) 
        REFERENCES EMPLOYEE(SSN)
        ON DELETE CASCADE
);

CREATE TABLE PROJECT (
    PNAME VARCHAR(15) NOT NULL, 
    PINT INT NOT NULL, 
    PLOCATION VARCHAR(20) NOT NULL,
    INDEX (PLOCATION),
    DNUM INT NOT NULL, 
    INDEX (DNUM),
    PRIMARY KEY (PINT), 
    FOREIGN KEY (PLOCATION) 
        REFERENCES DEPT_LOCATION(DLOCATION)
        ON DELETE CASCADE,
    FOREIGN KEY (DNUM) 
        REFERENCES DEPARTMENT (DNUM)
        ON DELETE CASCADE
);  

CREATE TABLE WORKS_ON ( 
    SSN INT NOT NULL, 
    INDEX (SSN),
    PNO INT NOT NULL,
    INDEX (PNO), 
    HOURS INT NOT NULL, 
    FOREIGN KEY (SSN) 
        REFERENCES EMPLOYEE(SSN)
        ON DELETE CASCADE,
    FOREIGN KEY (PNO) 
        REFERENCES PROJECT(PINT)
        ON DELETE CASCADE
); 

有效。