因此,当我想添加外键时出现错误。我在带有EER图模型的MySQL Workbench中添加了外键。工作台尝试添加的行是:
CREATE SCHEMA IF NOT EXISTS `barberDB` DEFAULT CHARACTER SET latin1 ; USE `barberDB` ; -- ----------------------------------------------------- -- Table `barberDB`.`BARBER` -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `barberDB`.`BARBER` ( `ID` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(45) NULL, PRIMARY KEY (`ID`)) ENGINE = InnoDB; -- ----------------------------------------------------- -- Table `barberDB`.`CUSTOMER` -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `barberDB`.`CUSTOMER` ( `ID` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(45) NULL, `isHaircut` INT NULL, `isBeard` INT NULL, `isEyebrows` INT NULL, `BARBER_ID` INT NOT NULL, PRIMARY KEY (`ID`), INDEX `fk_CUSTOMER_BARBER_idx` (`BARBER_ID` ASC) VISIBLE, CONSTRAINT `fk_CUSTOMER_BARBER` FOREIGN KEY (`BARBER_ID`) REFERENCES `barberDB`.`BARBER` (`ID`) ON DELETE CASCADE 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;
我得到的错误是:
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, CONSTRAINT `fk_CUSTOMER_BARBER` FOREIGN KEY (`BARBER_ID`) REF' at line 12 SQL Code: -- ----------------------------------------------------- -- Table `barberDB`.`CUSTOMER` -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `barberDB`.`CUSTOMER` ( `ID` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(45) NULL, `isHaircut` INT NULL, `isBeard` INT NULL, `isEyebrows` INT NULL, `BARBER_ID` INT NOT NULL, PRIMARY KEY (`ID`), INDEX `fk_CUSTOMER_BARBER_idx` (`BARBER_ID` ASC) VISIBLE, CONSTRAINT `fk_CUSTOMER_BARBER` FOREIGN KEY (`BARBER_ID`) REFERENCES `barberDB`.`BARBER` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = InnoDB SQL script execution finished: statements: 6 succeeded, 1 failed Fetching back view definitions in final form. Nothing to fetch
我搜索了解决方案,发现括号很重要或反引号,但是由于代码是由该工具创建的,因此对我来说似乎是正确的。是什么导致错误?
对于任何看此的人,都可以在EER图模型窗口中设置要用于生成SQL脚本的MySQL版本。转到“编辑”,“首选项”,然后是“MySQL”“建模”部分,并设置您的自定义MySQL版本。