将MySQL Workbench转移到服务器

时间:2016-05-23 15:30:34

标签: mysql sql innodb mysql-workbench

我正在尝试将我的数据库模型从mysql workbench移动到mysql服务器。我正在使用逆向工程sql创建脚本,但导入它时给我一个错误。我试图谷歌问题,但没有运气我的situtation。

-- -----------------------------------------------------
-- 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

1 个答案:

答案 0 :(得分:1)

问题是ISBN在引用的表中不是唯一的唯一标识符。

我假设Books表是你的基表,三个引用的表是M:N链接表。如果这是真的,那么您的外键指向错误的方向。 (尝试向另一个方向拖动连接?)