我正在开发一个项目,我对项目的现有数据库模式设计进行了扩展。我决定让MySQL Workbench创建ER图,在那里我添加了关系的新表。已经存在的部分由许多根本没有关系的表组成。在与同事一起完成设计之后,我通过检查the YouTube video以SQL脚本的形式导出了ER数据,并获得了一个SQL文件。保存文件时出现问题,因此我将其复制到缓存中并将其放入文件中。您可以在下面看到新创建的部分。
运行代码时,旧表(未包含在附件中的部分)运行没有问题,但由于MySQL Error 1215: Cannot add foreign key constraint问题,无法创建具有外键的新表。首先,我使用复制/粘贴到MySQL数据库的SQL窗口逐个尝试生成的脚本命令,我在那里单独执行它们,我发现上面已经写过的新表与它们的关系因为问题。虽然我试图通过遵循这个问题的答案来修复错误,但我没能摆脱错误。 您可以在上面的图像中看到标有黄色的旧表格(小写)。
我在脚本中所做的是通过替换现有的其他名称来制作所有引擎InnoDB,但无济于事。关键字段都是INT类型,没有 ON DELETE SET NULL 命令。我还在SQL脚本中将 latin1 中的CHARACTER SET更改为连贯的 utf8 ,但现在我不知道该怎么做才能使生成的脚本在MySQL数据库。
你有类似的问题吗?你认为我接下来要做什么?
感谢您的帮助!
以下是MySQL Workbench生成的数据库的修改部分:
-- 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';
-- -----------------------------------------------------
-- Table `test_apptest`.`Addresses_have_Persons`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Addresses_have_Persons` (
`Addresses_ID` INT NOT NULL,
`Persons_ID` INT NOT NULL,
PRIMARY KEY (`Addresses_ID`, `Persons_ID`),
INDEX `fk_Addresses_have_Persons_Persons1_idx` (`Persons_ID` ASC),
INDEX `fk_Addresses_have_Persons_Addresses1_idx` (`Addresses_ID` ASC),
CONSTRAINT `fk_Addresses_have_Persons_Addresses1`
FOREIGN KEY (`Addresses_ID`)
REFERENCES `test_apptest`.`Addresses` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Addresses_have_Persons_Persons1`
FOREIGN KEY (`Persons_ID`)
REFERENCES `test_apptest`.`Persons` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Groups_have_Persons`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Groups_have_Persons` (
`Groups_ID` INT NOT NULL,
`Persons_ID` INT NOT NULL,
PRIMARY KEY (`Groups_ID`, `Persons_ID`),
INDEX `fk_Groups_have_Persons_Persons1_idx` (`Persons_ID` ASC),
INDEX `fk_Groups_have_Persons_Groups1_idx` (`Groups_ID` ASC),
CONSTRAINT `fk_Groups_have_Persons_Groups1`
FOREIGN KEY (`Groups_ID`)
REFERENCES `test_apptest`.`Groups` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Groups_have_Persons_Persons1`
FOREIGN KEY (`Persons_ID`)
REFERENCES `test_apptest`.`Persons` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Groups_have_Roles`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Groups_have_Roles` (
`Groups_ID` INT NOT NULL,
`Roles_ID` INT NOT NULL,
PRIMARY KEY (`Groups_ID`, `Roles_ID`),
INDEX `fk_Groups_have_Roles_Roles1_idx` (`Roles_ID` ASC),
INDEX `fk_Groups_have_Roles_Groups1_idx` (`Groups_ID` ASC),
CONSTRAINT `fk_Groups_have_Roles_Groups1`
FOREIGN KEY (`Groups_ID`)
REFERENCES `test_apptest`.`Groups` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Groups_have_Roles_Roles1`
FOREIGN KEY (`Roles_ID`)
REFERENCES `test_apptest`.`Roles` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Roles_have_Persons`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Roles_have_Persons` (
`Roles_ID` INT NOT NULL,
`Persons_ID` INT NOT NULL,
PRIMARY KEY (`Roles_ID`, `Persons_ID`),
INDEX `fk_Roles_have_Persons_Persons1_idx` (`Persons_ID` ASC),
INDEX `fk_Roles_have_Persons_Roles1_idx` (`Roles_ID` ASC),
CONSTRAINT `fk_Roles_have_Persons_Roles1`
FOREIGN KEY (`Roles_ID`)
REFERENCES `test_apptest`.`Roles` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Roles_have_Persons_Persons1`
FOREIGN KEY (`Persons_ID`)
REFERENCES `test_apptest`.`Persons` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Roles_have_Rights`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Roles_have_Rights` (
`Roles_ID` INT NOT NULL,
`Rights_ID` INT NOT NULL,
PRIMARY KEY (`Roles_ID`, `Rights_ID`),
INDEX `fk_Roles_have_Rights_Rights1_idx` (`Rights_ID` ASC),
INDEX `fk_Roles_have_Rights_Roles1_idx` (`Roles_ID` ASC),
CONSTRAINT `fk_Roles_have_Rights_Roles1`
FOREIGN KEY (`Roles_ID`)
REFERENCES `test_apptest`.`Roles` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Roles_have_Rights_Rights1`
FOREIGN KEY (`Rights_ID`)
REFERENCES `test_apptest`.`Rights` (`ID`)
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;
这是最初未经修改的生成代码,也导致错误:
-- 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 test_apptest
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema test_apptest
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `test_apptest` DEFAULT CHARACTER SET latin1 ;
USE `test_apptest` ;
-- […]
-- -----------------------------------------------------
-- Table `test_apptest`.`Countries`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Countries` (
`ID` INT NOT NULL,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`States`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`States` (
`ID` INT NOT NULL,
`name` VARCHAR(45) NULL,
`Countries_ID` INT NOT NULL,
PRIMARY KEY (`ID`, `Countries_ID`),
INDEX `fk_States_Countries1_idx` (`Countries_ID` ASC),
CONSTRAINT `fk_States_Countries1`
FOREIGN KEY (`Countries_ID`)
REFERENCES `test_apptest`.`Countries` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Addresses`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Addresses` (
`ID` INT NOT NULL AUTO_INCREMENT,
`ZIP` VARCHAR(12) NULL,
`countryID` INT NOT NULL,
`stateID` INT NULL,
`settlement` VARCHAR(64) NOT NULL,
`street` VARCHAR(50) NOT NULL,
`addition` VARCHAR(64) NULL,
`phone` VARCHAR(22) NULL,
`Countries_ID` INT NOT NULL,
`States_ID` INT NOT NULL,
`States_Countries_ID` INT NOT NULL,
PRIMARY KEY (`ID`),
INDEX `fk_Addresses_Countries1_idx` (`Countries_ID` ASC),
INDEX `fk_Addresses_States1_idx` (`States_ID` ASC, `States_Countries_ID` ASC),
CONSTRAINT `fk_Addresses_Countries1`
FOREIGN KEY (`Countries_ID`)
REFERENCES `test_apptest`.`Countries` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Addresses_States1`
FOREIGN KEY (`States_ID` , `States_Countries_ID`)
REFERENCES `test_apptest`.`States` (`ID` , `Countries_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Filters`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Filters` (
`ID` INT NOT NULL,
`filter` BLOB NULL,
PRIMARY KEY (`ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Groups`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Groups` (
`ID` INT NOT NULL,
`name` VARCHAR(64) NOT NULL,
`description` VARCHAR(1024) NULL,
`Filters_ID` INT NOT NULL,
PRIMARY KEY (`ID`, `Filters_ID`),
INDEX `fk_Groups_Filters1_idx` (`Filters_ID` ASC),
CONSTRAINT `fk_Groups_Filters1`
FOREIGN KEY (`Filters_ID`)
REFERENCES `test_apptest`.`Filters` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Persons`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Persons` (
`ID` INT NOT NULL,
`username` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`firstName` VARCHAR(255) NOT NULL,
`lastName` VARCHAR(255) NOT NULL,
`eMail` VARCHAR(50) NOT NULL,
`standardAddressID` INT NOT NULL,
`address1ID` INT NULL,
`address2ID` INT NULL,
`phone1` VARCHAR(50) NOT NULL,
`phone2` VARCHAR(50) NULL,
`status` INT NULL,
`publicMail` VARCHAR(255) NULL,
`advisorID` INT NULL,
PRIMARY KEY (`ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Roles`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Roles` (
`ID` INT NOT NULL,
`name` VARCHAR(64) NOT NULL,
`description` VARCHAR(1024) NULL,
PRIMARY KEY (`ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Groups_have_Persons`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Groups_have_Persons` (
`Groups_ID` INT NOT NULL,
`Persons_ID` INT NOT NULL,
PRIMARY KEY (`Groups_ID`, `Persons_ID`),
INDEX `fk_Groups_has_Persons_Persons1_idx` (`Persons_ID` ASC),
INDEX `fk_Groups_has_Persons_Groups1_idx` (`Groups_ID` ASC),
CONSTRAINT `fk_Groups_has_Persons_Groups1`
FOREIGN KEY (`Groups_ID`)
REFERENCES `test_apptest`.`Groups` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Groups_has_Persons_Persons1`
FOREIGN KEY (`Persons_ID`)
REFERENCES `test_apptest`.`Persons` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Addresses_have_Persons`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Addresses_have_Persons` (
`Addresses_ID` INT NOT NULL,
`Persons_ID` INT NOT NULL,
PRIMARY KEY (`Addresses_ID`, `Persons_ID`),
INDEX `fk_Addresses_has_Persons_Persons1_idx` (`Persons_ID` ASC),
INDEX `fk_Addresses_has_Persons_Addresses1_idx` (`Addresses_ID` ASC),
CONSTRAINT `fk_Addresses_has_Persons_Addresses1`
FOREIGN KEY (`Addresses_ID`)
REFERENCES `test_apptest`.`Addresses` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Addresses_has_Persons_Persons1`
FOREIGN KEY (`Persons_ID`)
REFERENCES `test_apptest`.`Persons` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Rights`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Rights` (
`ID` INT NOT NULL,
`name` VARCHAR(64) NOT NULL,
`description` VARCHAR(1024) NULL,
PRIMARY KEY (`ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Roles_have_Rights`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Roles_have_Rights` (
`Roles_ID` INT NOT NULL,
`Rights_ID` INT NOT NULL,
PRIMARY KEY (`Roles_ID`, `Rights_ID`),
INDEX `fk_Roles_has_Rights_Rights1_idx` (`Rights_ID` ASC),
INDEX `fk_Roles_has_Rights_Roles1_idx` (`Roles_ID` ASC),
CONSTRAINT `fk_Roles_has_Rights_Roles1`
FOREIGN KEY (`Roles_ID`)
REFERENCES `test_apptest`.`Roles` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Roles_has_Rights_Rights1`
FOREIGN KEY (`Rights_ID`)
REFERENCES `test_apptest`.`Rights` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Groups_has_Roles`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Groups_has_Roles` (
`Groups_ID` INT NOT NULL,
`Roles_ID` INT NOT NULL,
PRIMARY KEY (`Groups_ID`, `Roles_ID`),
INDEX `fk_Groups_has_Roles_Roles1_idx` (`Roles_ID` ASC),
INDEX `fk_Groups_has_Roles_Groups1_idx` (`Groups_ID` ASC),
CONSTRAINT `fk_Groups_has_Roles_Groups1`
FOREIGN KEY (`Groups_ID`)
REFERENCES `test_apptest`.`Groups` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Groups_has_Roles_Roles1`
FOREIGN KEY (`Roles_ID`)
REFERENCES `test_apptest`.`Roles` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_apptest`.`Roles_has_Persons`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test_apptest`.`Roles_has_Persons` (
`Roles_ID` INT NOT NULL,
`Persons_ID` INT NOT NULL,
PRIMARY KEY (`Roles_ID`, `Persons_ID`),
INDEX `fk_Roles_has_Persons_Persons1_idx` (`Persons_ID` ASC),
INDEX `fk_Roles_has_Persons_Roles1_idx` (`Roles_ID` ASC),
CONSTRAINT `fk_Roles_has_Persons_Roles1`
FOREIGN KEY (`Roles_ID`)
REFERENCES `test_apptest`.`Roles` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Roles_has_Persons_Persons1`
FOREIGN KEY (`Persons_ID`)
REFERENCES `test_apptest`.`Persons` (`ID`)
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;
答案 0 :(得分:0)
我认为您不需要在映射表上添加主键。 尝试创建没有主键和索引的表(使用innodb时,添加外键将为您创建索引)。
第1步:
CREATE TABLE IF NOT EXISTS `test_apptest`.`Addresses_have_Persons` (
`Addresses_ID` INT NOT NULL,
`Persons_ID` INT NOT NULL)
ENGINE = InnoDB;
第2步:
ALTER TABLE Addresses_have_Persons
ADD CONSTRAINT fk_Addresses_have_Persons_Addresses1
FOREIGN KEY(Addresses_ID)
REFERENCES Addresses(ID);
第3步:
ALTER TABLE Addresses_have_Persons
ADD CONSTRAINT fk_Addresses_have_Persons_Persons1
FOREIGN KEY(Persons_ID)
REFERENCES Persons(ID);