带有外键的MariaDB表创建错误

时间:2018-11-26 11:56:47

标签: mysql foreign-keys mariadb mysql-workbench

有很多类似的问题,但我在其中找不到答案。

您能告诉我这是怎么回事吗?该脚本由mysql Workbench创建,但是除了答案

-- MySQL Script generated by MySQL Workbench
-- Mon Nov 26 14:14:46 2018
-- Model: New Model    Version: 1.0
-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;    
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE,     SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`Owner`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Owner` ;

CREATE TABLE IF NOT EXISTS `mydb`.`Owner` (
  `OwnerId` CHAR(36) NOT NULL,
  `Name` NVARCHAR(60) NOT NULL,
  `DateOfBirth` DATE NOT NULL,
  `Adress` NVARCHAR(100) NOT NULL,
  PRIMARY KEY (`OwnerId`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Account`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Account` ;

CREATE TABLE IF NOT EXISTS `mydb`.`Account` (
  `AccountId` CHAR(36) NOT NULL,
  `DateCreated` DATE NOT NULL,
  `AccountType` VARCHAR(45) NOT NULL,
  `OwnerId` CHAR(36) NULL,
  PRIMARY KEY (`AccountId`),
  INDEX `fk_Account_Owner_idx` (`OwnerId` ASC) VISIBLE,
  CONSTRAINT `fk_Account_Owner`
    FOREIGN KEY (`OwnerId`)
    REFERENCES `mydb`.`Owner` (`OwnerId`)
    ON DELETE RESTRICT
    ON UPDATE CASCADE)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

给定的错误是

  

错误代码:1064。您的SQL语法有错误;检查   手册
      对应于您的MariaDB服务器版本以使用正确的语法       在'CONSTRAINT fk_Account_Owner外键(OwnerId)附近
      在第7行引用“ myd”

2 个答案:

答案 0 :(得分:1)

在MySQL Workbench中:

转到:

编辑>首选项>建模> MySQL。

然后,将“默认目标MySQL版本”设置为5.7

来自https://stackoverflow.com/a/52785302/2625955

答案 1 :(得分:0)

还有另一种需要去除单词VISIBLE的情况。