使用外键创建表时如何解决错误1064?

时间:2019-05-11 14:49:11

标签: mysql mysql-workbench

MySql工作台告诉我,与我的MariaDB服务器版本相对应的SQL语法错误,我不知道为什么。

我已经看过其他问题,但是似乎没有一个问题有帮助,所以我现在问自己的问题...我真的不知道我的代码出了什么问题,也不知道是什么问题用我的语法是。我试图设置一些逗号或重命名列等,然后将模型中的SQL版本更改为与xampp一起使用的sql版本。但是仍然,在更改版本后,我仍然收到该错误...

创建表的sql是(我已经删除了注释):

CREATE SCHEMA IF NOT EXISTS `testDB` DEFAULT CHARACTER SET utf8 ;
USE `testDB` ;

CREATE TABLE IF NOT EXISTS `testDB`.`person` (
  `personID` INT NOT NULL AUTO_INCREMENT,
  `firstname` VARCHAR(45) NULL,
  `lastname` VARCHAR(45) NULL,
  PRIMARY KEY (`personID`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `testDB`.`comment` (
  `commentID` INT NOT NULL AUTO_INCREMENT,
  `comment` VARCHAR(45) NOT NULL,
  `person_personID` INT NOT NULL,
  PRIMARY KEY (`commentID`),
  INDEX `fk_comment_person_idx` (`person_personID` ASC) VISIBLE,
  CONSTRAINT `fk_comment_person`
    FOREIGN KEY (`person_personID`)
    REFERENCES `testDB`.`person` (`personID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

我得到的错误是:

Executing SQL script in server
ERROR: Error 1064: 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 '
  CONSTRAINT `fk_comment_person`
    FOREIGN KEY (`person_personID`)
    REFERE' at line 9
SQL Code:
        -- -----------------------------------------------------
        -- Table `testDB`.`comment`
        -- -----------------------------------------------------
        CREATE TABLE IF NOT EXISTS `testDB`.`comment` (
          `commentID` INT NOT NULL AUTO_INCREMENT,
          `comment` VARCHAR(45) NOT NULL,
          `person_personID` INT NOT NULL,
          PRIMARY KEY (`commentID`),
          INDEX `fk_comment_person_idx` (`person_personID` ASC) VISIBLE,
          CONSTRAINT `fk_comment_person`
            FOREIGN KEY (`person_personID`)
            REFERENCES `testDB`.`person` (`personID`)
            ON DELETE NO ACTION
            ON UPDATE NO ACTION)
        ENGINE = InnoDB

SQL script execution finished: statements: 6 succeeded, 1 failed

Fetching back view definitions in final form.
Nothing to fetch

1 个答案:

答案 0 :(得分:0)

在您的陈述中删除“可见”一词:

CREATE SCHEMA IF NOT EXISTS `testDB` DEFAULT CHARACTER SET utf8 ;
USE `testDB` ;

CREATE TABLE IF NOT EXISTS `testDB`.`person` (
  `personID` INT NOT NULL AUTO_INCREMENT,
  `firstname` VARCHAR(45) NULL,
  `lastname` VARCHAR(45) NULL,
  PRIMARY KEY (`personID`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `testDB`.`comment` (
  `commentID` INT NOT NULL AUTO_INCREMENT,
  `comment` VARCHAR(45) NOT NULL,
  `person_personID` INT NOT NULL,
  PRIMARY KEY (`commentID`),
  INDEX `fk_comment_person_idx` (`person_personID` ASC),
  CONSTRAINT `fk_comment_person`
    FOREIGN KEY (`person_personID`)
    REFERENCES `testDB`.`person` (`personID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

因为在MariaDB中尚未实现INVISIBLE选项。此选项在ORACLE中实现