简体   繁体   English

MySQL表创建错误1064-找不到原因

[英]MySQL table creation error 1064 - can't find reason

I'm trying to create a table in a MySQL database that has three foreign keys, but am getting an error. 我正在尝试在具有三个外键的MySQL数据库中创建表,但出现错误。 This error is not obvious to me, and can use a third party check. 这个错误对我来说并不明显,可以使用第三方检查。

CREATE TABLE `main_message`(
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  strat_key INT NULL,
  task INT NULL,
  user INT,
  comment VARCHAR(255) NOT NULL,
  datestamp DATETIME, 
  FOREIGN KEY strat_key REFERENCES prod_main_strategicdirection(id) ON UPDATE CASCADE ON DELETE CASCADE,
  FOREIGN KEY task REFERENCES main_task(id) ON UPDATE CASCADE ON DELETE CASCADE,
  FOREIGN KEY user REFERENCES prod_auth_user(id) ON UPDATE CASCADE ON DELETE CASCADE,
) ENGINE = INNODB;

According to the output, I'm getting the error message 根据输出,我收到错误消息

You have an error in your SQL syntax; check the manual that corresponds to your 
MariaDB server version for the right syntax to use near ' foreign key strat_key
references prod_main_strategicdirection(id) on update cas'

just after the first foreign key declaration 在第一次外键声明之后

FOREIGN KEY strat_key REFERENCES prod_main_strategicdirection(id)

but I see nothing obvious to what would be causing the error. 但我看不出导致错误的原因。

You forgot the parenthesis on the FOREIGN KEY and to remove the comma after the last CASCADE before the closing ) 您忘记了FOREIGN KEY上的括号,并在结束前的最后一个CASCADE之后删除了逗号)

CREATE TABLE `main_message`(
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  strat_key INT NULL,
  task INT NULL,
  user INT,
  comment VARCHAR(255) NOT NULL,
  datestamp DATETIME, 
  FOREIGN KEY(strat_key) REFERENCES prod_main_strategicdirection(id) ON UPDATE CASCADE ON DELETE CASCADE,
  FOREIGN KEY(task) REFERENCES main_task(id) ON UPDATE CASCADE ON DELETE CASCADE,
  FOREIGN KEY(user) REFERENCES prod_auth_user(id) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE = INNODB;

Live DEMO. 现场演示。

Using FOREIGN KEY Constraints: 使用外键约束:

[CONSTRAINT [symbol]] FOREIGN KEY
    [index_name] (index_col_name, ...)
    REFERENCES tbl_name (index_col_name,...)
    [ON DELETE reference_option]
    [ON UPDATE reference_option]

reference_option:
    RESTRICT | CASCADE | SET NULL | NO ACTION

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM