问题
When I try to generate a database using Forward Engineer I get an error like this.
Executing SQL script in server
ERROR: Error 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, INDEX
fk_Personal_Human1_idx
(Human_idMan
ASC) VISIBLE, CONSTRA' at line 12
SQL Code:
-- -----------------------------------------------------
-- Table `mydb`.`Personal`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Personal` (
`idPersonal` INT NOT NULL,
`Experience` INT NULL,
`Department_idDepartment` INT NOT NULL,
`Date_of_accept` DATE NOT NULL,
`Date_of_release` DATE NULL,
`Human_idMan` INT NOT NULL,
PRIMARY KEY (`idPersonal`),
INDEX `fk_Personal_Department1_idx` (`Department_idDepartment` ASC) VISIBLE,
INDEX `fk_Personal_Human1_idx` (`Human_idMan` ASC) VISIBLE,
CONSTRAINT `fk_Personal_Department1`
FOREIGN KEY (`Department_idDepartment`)
REFERENCES `mydb`.`Department` (`idDepartment`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Personal_Human1`
FOREIGN KEY (`Human_idMan`)
REFERENCES `mydb`.`Human` (`idMan`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 7 succeeded, 1 failed
Fetching back view definitions in final form. Nothing to fetch
回答1:
The problem here is the difference in syntax across different MySQL server versions. MySQL Workbench 8.0 is auto-generating CREATE UNIQUE INDEX
statement for the MySQL server version 8.0.
Most likely, your MySQL server version < 8.0. You can remove VISIBLE
keyword from your CREATE TABLE
statement. It will look as follows:
CREATE TABLE IF NOT EXISTS `mydb`.`Personal` (
`idPersonal` INT NOT NULL,
`Experience` INT NULL,
`Department_idDepartment` INT NOT NULL,
`Date_of_accept` DATE NOT NULL,
`Date_of_release` DATE NULL,
`Human_idMan` INT NOT NULL,
PRIMARY KEY (`idPersonal`),
INDEX `fk_Personal_Department1_idx` (`Department_idDepartment` ASC),
INDEX `fk_Personal_Human1_idx` (`Human_idMan` ASC),
CONSTRAINT `fk_Personal_Department1`
FOREIGN KEY (`Department_idDepartment`)
REFERENCES `mydb`.`Department` (`idDepartment`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Personal_Human1`
FOREIGN KEY (`Human_idMan`)
REFERENCES `mydb`.`Human` (`idMan`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
From the MySQL Server 8.0 Docs, the syntax for CREATE INDEX
is:
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}
However, this option of {VISIBLE | INVISIBLE}
is not available in the MySQL Server 5.7. 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}
来源:https://stackoverflow.com/questions/53040978/mysql-workbench-does-not-generate-a-database