0

我正在尝试在具有三个外键的 MySQL 数据库中创建一个表,但出现错误。这个错误对我来说并不明显,可以使用第三方检查。

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;

根据输出,我收到错误消息

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'

就在第一个外键声明之后

FOREIGN KEY strat_key REFERENCES prod_main_strategicdirection(id)

但我看不出什么会导致错误。

4

1 回答 1

1

您忘记了 and 上的括号,在结束前的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;

现场演示。

使用外键约束:

[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
于 2013-08-02T21:17:25.040 回答