为啥 mariadb 为使用工作台创建的 sql 文件提供语法错误
Posted
技术标签:
【中文标题】为啥 mariadb 为使用工作台创建的 sql 文件提供语法错误【英文标题】:why does mariadb gives a syntax error for a sql file created with workbench为什么 mariadb 为使用工作台创建的 sql 文件提供语法错误 【发布时间】:2020-02-12 11:19:31 【问题描述】:我已经用 mysql workbench 创建了一个模型,每次我想执行 sql 文件时都会遇到这个错误
ERROR 1064 (42000) at line 55 in file: 'D:\db.sql': 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以获取在 ' 附近使用的正确语法 约束
fk_customer_order_customer1
外键 (customer_id
) ' 在第 8 行ERROR 1064 (42000) at line 76 in file: 'D:\db.sql': You have an error in your SQL syntax;检查与您的 MariaDB 服务器版本相对应的手册,以获取在 ' 附近使用的正确语法 约束
fk_product_category
外键 (category_id
) REFERENC' 在第 9 行ERROR 1064 (42000) at line 98 in file: 'D:\db.sql': You have an error in your SQL syntax;检查与您的 MariaDB 服务器版本相对应的手册,以获取在 ' 附近使用的正确语法 索引
fk_ordered_product_product
(customer_order_id
ASC) 可见, 第 6 行的 CONST'
这是为我生成的代码mysql工作台
-- 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 ecommerce_ee
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `ecommerce_ee` ;
-- -----------------------------------------------------
-- Schema ecommerce_ee
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `ecommerce_ee` ;
USE `ecommerce_ee` ;
-- -----------------------------------------------------
-- Table `ecommerce_ee`.`customer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ecommerce_ee`.`customer` ;
CREATE TABLE IF NOT EXISTS `ecommerce_ee`.`customer` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`email` VARCHAR(45) NOT NULL,
`phone` VARCHAR(45) NOT NULL,
`address` VARCHAR(45) NOT NULL,
`city_region` VARCHAR(45) NOT NULL,
`cc_number` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ecommerce_ee`.`category`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ecommerce_ee`.`category` ;
CREATE TABLE IF NOT EXISTS `ecommerce_ee`.`category` (
`id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ecommerce_ee`.`customer_order`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ecommerce_ee`.`customer_order` ;
CREATE TABLE IF NOT EXISTS `ecommerce_ee`.`customer_order` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`amount` DECIMAL(6,2) NOT NULL,
`date_created` TIMESTAMP NOT NULL,
`confirmation_number` INT UNSIGNED NOT NULL,
`customer_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_customer_order_customer` (`customer_id` ASC) VISIBLE,
CONSTRAINT `fk_customer_order_customer1`
FOREIGN KEY (`customer_id`)
REFERENCES `ecommerce_ee`.`customer` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ecommerce_ee`.`product`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ecommerce_ee`.`product` ;
CREATE TABLE IF NOT EXISTS `ecommerce_ee`.`product` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`price` DECIMAL(6,2) NOT NULL,
`description` TINYTEXT NULL,
`last_update` TIMESTAMP NOT NULL,
`category_id` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`id`, `name`),
INDEX `fk_product_category` (`category_id` ASC) VISIBLE,
CONSTRAINT `fk_product_category`
FOREIGN KEY (`category_id`)
REFERENCES `ecommerce_ee`.`category` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ecommerce_ee`.`ordered_product`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ecommerce_ee`.`ordered_product` ;
CREATE TABLE IF NOT EXISTS `ecommerce_ee`.`ordered_product` (
`customer_order_id` INT UNSIGNED NOT NULL,
`product_id` INT UNSIGNED NOT NULL,
`quantity` SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (`customer_order_id`, `product_id`),
INDEX `fk_ordered_product_customer_order` (`product_id` ASC, `quantity` ASC) VISIBLE,
INDEX `fk_ordered_product_product` (`customer_order_id` ASC) VISIBLE,
CONSTRAINT `fk_customer_order_has_product_customer_order1`
FOREIGN KEY (`customer_order_id`)
REFERENCES `ecommerce_ee`.`customer_order` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_customer_order_has_product_product1`
FOREIGN KEY (`product_id` , `quantity`)
REFERENCES `ecommerce_ee`.`product` (`id` , `name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
【问题讨论】:
重复的重复***.com/questions/53480613/… MariaDB Table Creation Error with Foreign Key的可能重复 【参考方案1】:删除关键字VISIBLE
。 (它只是 在“附近”文本之前。)
【讨论】:
谢谢这对我有用,其他解决方案没有VISIBLE
是一个非常新的关键字;这是默认设置。 Workbench 似乎太急于不必要地包含它。正如您所发现的,它不应该这样做,因为它会导致向后不兼容。以上是关于为啥 mariadb 为使用工作台创建的 sql 文件提供语法错误的主要内容,如果未能解决你的问题,请参考以下文章
为啥 MariaDB - 或 phpMyAdmin - 拒绝我为临时表创建 TABLE 语法?
为啥 MariaDB 不接受我的字符串变量作为我的 sql 查询中的表名?