简体   繁体   中英

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. 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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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