I am trying to move my database model from mysql workbench to mysql server. I am using the reverse engineer sql create script but when importing it gives me an error. I have tried to google the problem but no luck to my situation.
-- -----------------------------------------------------
-- Table `MapLibrary`.`Books`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `MapLibrary`.`Books` ;
CREATE TABLE IF NOT EXISTS `MapLibrary`.`Books` (
`ISBN` VARCHAR(13) NOT NULL,
`date_of_publication` INT NULL,
`book_title` VARCHAR(45) NULL,
PRIMARY KEY (`ISBN`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `MapLibrary`.`Genre`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `MapLibrary`.`Genre` ;
CREATE TABLE IF NOT EXISTS `MapLibrary`.`Genre` (
`genre_code` INT NOT NULL,
`genre_name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`genre_code`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `MapLibrary`.`Books_By_Genre`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `MapLibrary`.`Books_By_Genre` ;
CREATE TABLE IF NOT EXISTS `MapLibrary`.`Books_By_Genre` (
`genre_code` INT NOT NULL,
`ISBN` VARCHAR(13) NOT NULL,
PRIMARY KEY (`genre_code`, `ISBN`),
INDEX `Books_idx` (`ISBN` ASC),
CONSTRAINT `Books`
FOREIGN KEY (`ISBN`)
REFERENCES `MapLibrary`.`Books` (`ISBN`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Genre`
FOREIGN KEY (`genre_code`)
REFERENCES `MapLibrary`.`Genre` (`genre_code`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
ERROR:
ERROR 1022 (23000) at line 89: Can't write; duplicate key in table 'books_by_genre'
Operation failed with exitcode 1
The problem is that the ISBN is NOT a unique identifier on its own in the referenced tables.
I assume, that the Books table is your base table and the three referenced tables are M:N link tables. If this is true, then your foreign keys are pointing in the wrong direction. (Try to drag the connection in the other direction?)