EER图正向工程错误:mysql ERROR:错误1215:无法添加外键约束

时间:2018-03-18 15:51:04

标签: mysql sql key constraints

我使用MySQL工作台使用EER图创建数据库结构。完成后,我使用正向工程功能来创建我的模型的sql文件:EER Diagram

当我尝试在MySQL服务器上部署此查询时,它一直给我mysql错误:错误1215:无法添加外键约束。

有人能帮助我吗?

    -- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`TblPersoon`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblPersoon` ;

CREATE TABLE IF NOT EXISTS `mydb`.`TblPersoon` (
  `PersoonID` INT NOT NULL,
  `GSM` VARCHAR(45) NULL,
  `Functie` VARCHAR(45) NULL,
  `Voornaam` VARCHAR(45) NULL,
  `Achternaam` VARCHAR(45) NULL,
  PRIMARY KEY (`PersoonID`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`TblDevice`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblDevice` ;

CREATE TABLE IF NOT EXISTS `mydb`.`TblDevice` (
  `DeviceID` INT NOT NULL,
  `Van` VARCHAR(45) NULL,
  `Tot` VARCHAR(45) NULL,
  PRIMARY KEY (`DeviceID`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`TblEvents`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblEvents` ;

CREATE TABLE IF NOT EXISTS `mydb`.`TblEvents` (
  `EventID` INT NOT NULL AUTO_INCREMENT,
  `Naam` VARCHAR(45) NULL,
  `Locatie` VARCHAR(45) NULL,
  `ContactpersoonID` INT NOT NULL,
  `VerantwoordelijkeID` INT NOT NULL,
  `EventNaam` VARCHAR(45) NULL,
  PRIMARY KEY (`EventID`),
  INDEX `PersoonID_idx` (`ContactpersoonID` ASC),
  INDEX `PersoonID_idx1` (`VerantwoordelijkeID` ASC),
  CONSTRAINT `PersoonID`
    FOREIGN KEY (`ContactpersoonID`)
    REFERENCES `mydb`.`TblPersoon` (`PersoonID`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `PersoonID`
    FOREIGN KEY (`VerantwoordelijkeID`)
    REFERENCES `mydb`.`TblPersoon` (`PersoonID`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`TblContainer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblContainer` ;

CREATE TABLE IF NOT EXISTS `mydb`.`TblContainer` (
  `ContainerID` INT NULL,
  `Plaats` INT NULL,
  PRIMARY KEY (`ContainerID`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`TblInstallatie`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblInstallatie` ;

CREATE TABLE IF NOT EXISTS `mydb`.`TblInstallatie` (
  `InstallatieID` INT NULL,
  `ContainerID` INT NULL,
  `DeviceID` INT NULL,
  `Van` VARCHAR(45) NULL,
  `Tot` VARCHAR(45) NULL,
  `EventID` INT NULL,
  `Omschrijving` VARCHAR(45) NULL,
  `VerantwoordelijkeID` INT NULL,
  PRIMARY KEY (`InstallatieID`),
  INDEX `PersoonID_idx` (`VerantwoordelijkeID` ASC),
  INDEX `DeviceID_idx` (`DeviceID` ASC),
  INDEX `EventID_idx` (`EventID` ASC),
  INDEX `ContainerID_idx` (`ContainerID` ASC),
  CONSTRAINT `PersoonID`
    FOREIGN KEY (`VerantwoordelijkeID`)
    REFERENCES `mydb`.`TblPersoon` (`PersoonID`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `DeviceID`
    FOREIGN KEY (`DeviceID`)
    REFERENCES `mydb`.`TblDevice` (`DeviceID`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `EventID`
    FOREIGN KEY (`EventID`)
    REFERENCES `mydb`.`TblEvents` (`EventID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `ContainerID`
    FOREIGN KEY (`ContainerID`)
    REFERENCES `mydb`.`TblContainer` (`ContainerID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`TblLog`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblLog` ;

CREATE TABLE IF NOT EXISTS `mydb`.`TblLog` (
  `LogID` INT NOT NULL,
  `InstallatieID` INT NULL,
  PRIMARY KEY (`LogID`),
  INDEX `InstallatieID_idx` (`InstallatieID` ASC),
  CONSTRAINT `InstallatieID`
    FOREIGN KEY (`InstallatieID`)
    REFERENCES `mydb`.`TblInstallatie` (`InstallatieID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

1 个答案:

答案 0 :(得分:0)

我注意到TblEvents中的两个外键约束都具有相同的名称PersoonID。约束名称必须是唯一的,实际上它们在整个模式中必须是唯一的,而不仅仅是在定义它们的表中。

TblInstallatie也有自己的约束PersoonID。因此,如果您只更改TblEvents中的一个约束名称,则在到达CREATE TABLE TblInstallatie时仍会出现错误。

我使用MySQL 5.6对此进行了测试,并通过为TblEvents中的两个约束赋予不同的名称来实现它。

CREATE TABLE IF NOT EXISTS `mydb`.`TblEvents` (
  ...
  CONSTRAINT `PersoonID1`
    FOREIGN KEY (`ContactpersoonID`)
    ...
  CONSTRAINT `PersoonID2`
    FOREIGN KEY (`VerantwoordelijkeID`)
    ...

TblInstallatie CONSTRAINT PersoonID的后期创作可以成功。