无法找出此代码的SQL语法

时间:2013-03-14 17:24:53

标签: mysql

在我尝试将此代码粘贴到phpmyadmin数据库时,我做了SQL之间很长时间它不会创建表我得到错误:

#1064 - 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 'NOT NULL PRIMARY KEY, WORKER CHAR(30), CONSTRAINT WORKER_FK FOREIGN KEY(WORKE' at line 2

这是sql表的代码:

CREATE TABLE REPORT(
 REPORT_ID NOT NULL PRIMARY KEY,
  WORKER CHAR(30) CONSTRAINT WORKER_FK FOREIGN KEY(WORKER) REFERENCES WORKER(WORKER_ID)
  ON UPDATE CASCADE
  ON DELETE SET NULL,
  CLIENT CHAR (30) CONSTRAINT CLIENT_FK FOREIGN KEY(client) REFERENCES CLIENT(CLIENT_ID)
  ON UPDATE CASCADE
  ON DELETE SET NULL,
 START_DATE DATE CONSTRAINT STARTDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(START_DATE)
  ON UPDATE CASCADE
  ON DELETE SET NULL,
  END_DATE DATE CONSTRAINT ENDDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(END_DATE)
  ON UPDATE CASCADE
  ON DELETE SET NULL,
  COMMENT CHAR(30)
 )engine innoDB;

任何人都可以帮助我

2 个答案:

答案 0 :(得分:2)

实际的错误消息来自report_id没有数据类型的事实。

REPORT_ID NOT NULL PRIMARY KEY,

应该是

REPORT_ID INTEGER NOT NULL PRIMARY KEY,

但是你有很多问题。 MySQL内部忽略了“内联”外键 - 即使使用InnoDB也是如此。你必须把它们移到最后。

此外,您对job表的外键不正确。首先,您的表格中没有job列,因此FOREIGN KEY(JOB) REFERENCES JOB(END_DATE)无法正确。

其次,您将两个不同的PK定义引用到同一个表中。我的猜测是你真的想要:

CONSTRAINT START_END_DATE_FK 
   FOREIGN KEY (START_DATE, END_DATE) 
   REFERENCES JOB(START_DATE, END_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,

这假设(START_DATE, END_DATE)JOB表的主键 - 听起来有点奇怪。

不确定comment是否是MySQL中的保留字。如果是,则需要引用列名称。

所以总而言之,你可能需要这样的东西:

CREATE TABLE REPORT
(
  REPORT_ID INTEGER NOT NULL PRIMARY KEY,
  WORKER CHAR(30) ,
  CLIENT CHAR (30),
  START_DATE DATE ,
  END_DATE DATE,
  COMMENT CHAR(30),
  CONSTRAINT WORKER_FK FOREIGN KEY(WORKER) REFERENCES WORKER(WORKER_ID)
  ON UPDATE CASCADE
  ON DELETE SET NULL,
  CONSTRAINT CLIENT_FK FOREIGN KEY (client) REFERENCES CLIENT(CLIENT_ID)
  ON UPDATE CASCADE
  ON DELETE SET NULL,
  CONSTRAINT START_END_DATE_FK FOREIGN KEY (START_DATE, END_DATE) REFERENCES JOB(START_DATE, END_DATE)
  ON UPDATE CASCADE
  ON DELETE SET NULL,
  CONSTRAINT ENDDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(END_DATE)
  ON UPDATE CASCADE
  ON DELETE SET NULL
 );

答案 1 :(得分:1)

您没有为REPORT_ID定义列类型。即REPORT_ID int(11) NOT NULL PRIMARY KEY,

CREATE TABLE REPORT(
 REPORT_ID int(11) NOT NULL PRIMARY KEY,
  WORKER CHAR(30) CONSTRAINT WORKER_FK FOREIGN KEY(WORKER) REFERENCES WORKER(WORKER_ID)
  ON UPDATE CASCADE
  ON DELETE SET NULL,
  CLIENT CHAR (30) CONSTRAINT CLIENT_FK FOREIGN KEY(client) REFERENCES CLIENT(CLIENT_ID)
  ON UPDATE CASCADE
  ON DELETE SET NULL,
 START_DATE DATE CONSTRAINT STARTDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(START_DATE)
  ON UPDATE CASCADE
  ON DELETE SET NULL,
  END_DATE DATE CONSTRAINT ENDDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(END_DATE)
  ON UPDATE CASCADE
  ON DELETE SET NULL,
  COMMENT CHAR(30)
 )engine innoDB;