Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Error 1064 when adding foreign key with MySQL Workbench

So I have an error, when I want to add a foreign key. I added the foreign key in MySQL Workbench with an EER Diagram Model. The lines workbench tries to add are:`

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;

The error I get is:

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

I searched for solutions and find that parenthesis are important or backticks, but since the code is created by the tool, it seems correct to me. What could cause the error?

like image 489
NECben067 Avatar asked Aug 19 '18 12:08

NECben067


People also ask

How do I add a foreign key in MySQL workbench query?

To add a foreign key, click the last row in the Foreign Key Name list. Enter a name for the foreign key and select the column or columns that you wish to index by checking the column name in the Column list. You can remove a column from the index by removing the check mark from the appropriate column.

What is error code 1064 in MySQL workbench?

The error message with error code 1064 occurs due to the incorrect syntax of MySQL queries. In simple words, MySQL does not understand the commands that you have written. The commands are mistyped or misspelled within the MySQL environment which the database does not recognize.

How do I fix error 1005 in MySQL?

To solve 'MySQL ERROR 1005: Can't create table (errno: 150)' you likely just have to ensure that your foreign key has the exact same type as the primary key. Hope it helps.


1 Answers

For anyone looking this, in the EER Diagram Model window you can set wich MySQL version want use to generate SQL script. Go to Edit, Preferences, then Modeling section, MySQL and set your custom MySQL version.

like image 168
imnothuman Avatar answered Sep 18 '22 15:09

imnothuman