MySQL Workbench 和 phpMyadmin

MySQL Workbench and phpMyadmin

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VISIBLE, CONSTRAINT fk_TeamStatistik_Team FOREIGN KEY (Team_id) ' at line 10

我使用 mysql workbench 创建了我的数据库并得到了这个错误,下面我发布了 mysql workbench 生成的 sql 代码...当我试图在 phpmyadmin 中输入 sql 代码时出现此错误。任何人都可以帮助?提前致谢。

-- MySQL Script generated by MySQL Workbench
-- Sun Oct 21 14:37:37 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 dbpws18db16
-- -----------------------------------------------------

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

-- -----------------------------------------------------
-- Table `dbpws18db16`.`Team`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `dbpws18db16`.`Team` ;

CREATE TABLE IF NOT EXISTS `dbpws18db16`.`Team` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(45) NULL,
  `Stadt` VARCHAR(45) NULL,
  `Staat` VARCHAR(45) NULL,
  `Besitzer` VARCHAR(45) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dbpws18db16`.`Spieler`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `dbpws18db16`.`Spieler` ;

CREATE TABLE IF NOT EXISTS `dbpws18db16`.`Spieler` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `Alter` INT NULL,
  `Vorname` VARCHAR(45) NULL,
  `Nachname` VARCHAR(45) NULL,
  `Position` VARCHAR(45) NULL,
  `Größe` DECIMAL NULL,
  `Team_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_Spieler_Team1_idx` (`Team_id` ASC) VISIBLE,
  CONSTRAINT `fk_Spieler_Team1`
    FOREIGN KEY (`Team_id`)
    REFERENCES `dbpws18db16`.`Team` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dbpws18db16`.`Individuelle Statistik`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `dbpws18db16`.`Individuelle Statistik` ;

CREATE TABLE IF NOT EXISTS `dbpws18db16`.`Individuelle Statistik` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `PPG` VARCHAR(45) NULL,
  `RPG` VARCHAR(45) NULL,
  `SPG` VARCHAR(45) NULL,
  `APG` VARCHAR(45) NULL,
  `GP` VARCHAR(45) NULL,
  `BPG` VARCHAR(45) NULL,
  `FG` INT NULL,
  `Spieler_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_Individuelle Statistik_Spieler1_idx` (`Spieler_id` ASC) VISIBLE,
  CONSTRAINT `fk_Individuelle Statistik_Spieler1`
    FOREIGN KEY (`Spieler_id`)
    REFERENCES `dbpws18db16`.`Spieler` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dbpws18db16`.`Saison`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `dbpws18db16`.`Saison` ;

CREATE TABLE IF NOT EXISTS `dbpws18db16`.`Saison` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `Beginn` DATETIME NULL,
  `Ende` DATETIME NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dbpws18db16`.`Spiele`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `dbpws18db16`.`Spiele` ;

CREATE TABLE IF NOT EXISTS `dbpws18db16`.`Spiele` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `hd` VARCHAR(45) NULL,
  `Spielecol` VARCHAR(45) NULL,
  `Saison_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_Spiele_Saison1_idx` (`Saison_id` ASC) VISIBLE,
  CONSTRAINT `fk_Spiele_Saison1`
    FOREIGN KEY (`Saison_id`)
    REFERENCES `dbpws18db16`.`Saison` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dbpws18db16`.`TeamStatistik`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `dbpws18db16`.`TeamStatistik` ;

CREATE TABLE IF NOT EXISTS `dbpws18db16`.`TeamStatistik` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `BPG` DECIMAL NULL,
  `APG` DECIMAL NULL,
  `SPG` DECIMAL NULL,
  `GP` INT NULL,
  `PPG` DECIMAL NULL,
  `RPG` DECIMAL NULL,
  `Team_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_TeamStatistik_Team_idx` (`Team_id` ASC) VISIBLE,
  CONSTRAINT `fk_TeamStatistik_Team`
    FOREIGN KEY (`Team_id`)
    REFERENCES `dbpws18db16`.`Team` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dbpws18db16`.`Team_has_Spiele`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `dbpws18db16`.`Team_has_Spiele` ;

CREATE TABLE IF NOT EXISTS `dbpws18db16`.`Team_has_Spiele` (
  `Team_id` INT NOT NULL,
  `Spiele_id` INT NOT NULL,
  PRIMARY KEY (`Team_id`, `Spiele_id`),
  INDEX `fk_Team_has_Spiele_Spiele1_idx` (`Spiele_id` ASC) VISIBLE,
  INDEX `fk_Team_has_Spiele_Team1_idx` (`Team_id` ASC) VISIBLE,
  CONSTRAINT `fk_Team_has_Spiele_Team1`
    FOREIGN KEY (`Team_id`)
    REFERENCES `dbpws18db16`.`Team` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Team_has_Spiele_Spiele1`
    FOREIGN KEY (`Spiele_id`)
    REFERENCES `dbpws18db16`.`Spiele` (`id`)
    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;

-- -----------------------------------------------------
-- Data for table `dbpws18db16`.`Team`
-- -----------------------------------------------------
START TRANSACTION;
USE `dbpws18db16`;
INSERT INTO `dbpws18db16`.`Team` (`id`, `Name`, `Stadt`, `Staat`, `Besitzer`) VALUES (1, 'abc', 'cf', 'dd', 'ddd');

COMMIT;


-- -----------------------------------------------------
-- Data for table `dbpws18db16`.`Spieler`
-- -----------------------------------------------------
START TRANSACTION;
USE `dbpws18db16`;
INSERT INTO `dbpws18db16`.`Spieler` (`id`, `Alter`, `Vorname`, `Nachname`, `Position`, `Größe`, `Team_id`) VALUES (1, 12, 'cdcrc', 'crcr', 'crc', 1.89, DEFAULT);

COMMIT;


-- -----------------------------------------------------
-- Data for table `dbpws18db16`.`Individuelle Statistik`
-- -----------------------------------------------------
START TRANSACTION;
USE `dbpws18db16`;
INSERT INTO `dbpws18db16`.`Individuelle Statistik` (`id`, `PPG`, `RPG`, `SPG`, `APG`, `GP`, `BPG`, `FG`, `Spieler_id`) VALUES (1, '2', '2', '2', '2', '2', NULL, NULL, DEFAULT);

COMMIT;


-- -----------------------------------------------------
-- Data for table `dbpws18db16`.`Saison`
-- -----------------------------------------------------
START TRANSACTION;
USE `dbpws18db16`;
INSERT INTO `dbpws18db16`.`Saison` (`id`, `Beginn`, `Ende`) VALUES (1, '01.04.2018', '01.04.2019');

COMMIT;


-- -----------------------------------------------------
-- Data for table `dbpws18db16`.`Spiele`
-- -----------------------------------------------------
START TRANSACTION;
USE `dbpws18db16`;
INSERT INTO `dbpws18db16`.`Spiele` (`id`, `hd`, `Spielecol`, `Saison_id`) VALUES (1, 'dw', 'de', DEFAULT);
INSERT INTO `dbpws18db16`.`Spiele` (`id`, `hd`, `Spielecol`, `Saison_id`) VALUES (, NULL, NULL, DEFAULT);

COMMIT;


-- -----------------------------------------------------
-- Data for table `dbpws18db16`.`TeamStatistik`
-- -----------------------------------------------------
START TRANSACTION;
USE `dbpws18db16`;
INSERT INTO `dbpws18db16`.`TeamStatistik` (`id`, `BPG`, `APG`, `SPG`, `GP`, `PPG`, `RPG`, `Team_id`) VALUES (1, 2.3, 2.2., 2.2, 2.2, 2.2, 2.2, DEFAULT);
INSERT INTO `dbpws18db16`.`TeamStatistik` (`id`, `BPG`, `APG`, `SPG`, `GP`, `PPG`, `RPG`, `Team_id`) VALUES (2, 3, 4, 3, 4, 4, 4, DEFAULT);

COMMIT;

这里的问题是不同 MySQL 服务器版本的语法差异。看来你的MySQLworkbench版本是8.0及以上。它自动生成的代码适用于 MySQL 服务器 版本 8.0.

您需要将 MySQL server 版本升级到 8.0 及更高版本。或者,您可以从所有地方(定义索引的地方)删除 VISIBLE 关键字,如下所示:

INDEX `fk_TeamStatistik_Team_idx` (`Team_id` ASC) VISIBLE, -- <-- remove VISIBLE

INDEX `fk_TeamStatistik_Team_idx` (`Team_id` ASC),

您还需要在查询的其他部分执行相同的操作。

其他详细信息

MySQL Server 8.0 Docs 开始,CREATE INDEX 的语法是:

CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name
    [index_type]
    ON tbl_name (key_part,...)
    [index_option]
    [algorithm_option | lock_option] ...

key_part: {col_name [(length)] | (expr)} [ASC | DESC]

index_option:
    KEY_BLOCK_SIZE [=] value
  | index_type
  | WITH PARSER parser_name
  | COMMENT 'string'
  | {VISIBLE | INVISIBLE}  -- Notice the option of VISIBLE / INVISIBLE

index_type:
  USING {BTREE | HASH}

但是,{VISIBLE | INVISIBLE} 的这个选项在 MySQL 服务器 5.5 中不可用(). From Docs:

CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name
    [index_type]
    ON tbl_name (key_part,...)
    [index_option]
    [algorithm_option | lock_option] ...

key_part:
    col_name [(length)] [ASC | DESC]

index_option:
    KEY_BLOCK_SIZE [=] value
  | index_type
  | WITH PARSER parser_name
  | COMMENT 'string'

index_type:
    USING {BTREE | HASH}

MySQL Workbench 允许您设置生成架构时使用的 MySQL 目标版本。

Edit / Preferences / Modeling / MySQL / set the *Default Target MySQL Version" to 5.5.60.

然后它会在为您生成 SQL 时避免使用版本 8 的新功能。