我已经使用MySql Workbench生成了一个sql脚本。
当我尝试创建我的数据库时,我有“错误1215 1215 - 无法添加外键约束”。
问题似乎与我在上一个创建表中的最后一个外键有关:
CONSTRAINT `fk_tb_animation_option_col_debut`
FOREIGN KEY (`DT_ANO_DEBUT`)
REFERENCES `brest2016`.`tb_animation_option` (`DT_ANO_DEBUT`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
这是我的整个剧本:
-- MySQL Script generated by MySQL Workbench
-- 01/16/16 00:46:22
-- Model: New Model Version: 1.0
-- 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 brest2016
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `brest2016` DEFAULT CHARACTER SET utf8 ;
USE `brest2016` ;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_visiteur`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_visiteur` (
`D_VI_CODE` INT(3) NOT NULL AUTO_INCREMENT,
`V_VI_NOM` VARCHAR(45) NOT NULL,
`V_VI_PRENOM` VARCHAR(45) NOT NULL,
`V_VI_EMAIL` VARCHAR(45) NOT NULL,
`V_VI_TELEPHONE` VARCHAR(45) NULL,
`DT_VI_DEBUT` DATETIME NOT NULL,
`DT_VI_FIN` DATETIME NOT NULL,
PRIMARY KEY (`D_VI_CODE`),
UNIQUE INDEX `idVisiteur_UNIQUE` (`D_VI_CODE` ASC),
UNIQUE INDEX `email_UNIQUE` (`V_VI_EMAIL` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_type_animation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_type_animation` (
`D_TY_CODE` INT(3) NOT NULL AUTO_INCREMENT,
`V_TY_LIBELLE` VARCHAR(45) NULL,
PRIMARY KEY (`D_TY_CODE`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_animation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_animation` (
`D_AN_CODE` INT(3) NOT NULL AUTO_INCREMENT,
`V_AN_LIBELLE` VARCHAR(45) NOT NULL,
`V_AN_LIEU` VARCHAR(45) NOT NULL,
`V_AN_IMAGE` VARCHAR(45) NULL,
`V_AN_DESCRIPTIF` VARCHAR(45) NULL,
`D_AN_PRIORITE` VARCHAR(45) NOT NULL,
`D_TY_CODE` INT(3) NULL,
PRIMARY KEY (`D_AN_CODE`),
UNIQUE INDEX `idAnimation_UNIQUE` (`D_AN_CODE` ASC),
INDEX `fk_tb_type_animation_col_code_1_idx` (`D_TY_CODE` ASC),
CONSTRAINT `fk_tb_type_animation_col_code`
FOREIGN KEY (`D_TY_CODE`)
REFERENCES `brest2016`.`tb_type_animation` (`D_TY_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_option`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_option` (
`D_OP_CODE` INT(3) NOT NULL AUTO_INCREMENT,
`V_OP_LIBELLE` VARCHAR(45) NOT NULL,
PRIMARY KEY (`D_OP_CODE`),
UNIQUE INDEX `idOption_UNIQUE` (`D_OP_CODE` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_animation_option`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_animation_option` (
`D_AN_CODE` INT(3) NOT NULL,
`D_OP_CODE` INT(3) NOT NULL,
`DT_ANO_DEBUT` DATETIME NOT NULL,
`DT_ANO_FIN` DATETIME NOT NULL,
`D_ANO_PLACES_MAX` INT(3) NOT NULL,
`D_ANO_DUREE` INT(3) NOT NULL,
PRIMARY KEY (`D_AN_CODE`, `D_OP_CODE`, `DT_ANO_DEBUT`),
INDEX `fk_tb_option_idx` (`D_OP_CODE` ASC),
CONSTRAINT `fk_tb_animation_col_code`
FOREIGN KEY (`D_AN_CODE`)
REFERENCES `brest2016`.`tb_animation` (`D_AN_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_option_col_code`
FOREIGN KEY (`D_OP_CODE`)
REFERENCES `brest2016`.`tb_option` (`D_OP_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = 'association entre une animation et une option';
-- -----------------------------------------------------
-- Table `brest2016`.`tb_administrateur`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_administrateur` (
`D_AD_CODE` INT(3) NOT NULL,
`V_AD_ID` VARCHAR(12) NOT NULL,
`V_AD_MDP` VARCHAR(12) NOT NULL,
PRIMARY KEY (`D_AD_CODE`),
UNIQUE INDEX `idAdministrateur_UNIQUE` (`D_AD_CODE` ASC),
UNIQUE INDEX `V_UT_LOGIN_UNIQUE` (`V_AD_ID` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_reservation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_reservation` (
`D_VI_CODE` INT(3) NOT NULL,
`D_AN_CODE` INT(3) NOT NULL,
`D_OP_CODE` INT(3) NOT NULL,
`DT_ANO_DEBUT` DATETIME NOT NULL,
PRIMARY KEY (`D_VI_CODE`, `D_AN_CODE`, `D_OP_CODE`, `DT_ANO_DEBUT`),
INDEX `fk_tb_animation_option_col_code_animation_idx` (`D_AN_CODE` ASC),
INDEX `fk_tb_animation_option_col_code_option_idx` (`D_OP_CODE` ASC),
INDEX `fk_tb_animation_option_col_debut_idx` (`DT_ANO_DEBUT` ASC),
CONSTRAINT `fk_tb_visiteur_col_code`
FOREIGN KEY (`D_VI_CODE`)
REFERENCES `brest2016`.`tb_visiteur` (`D_VI_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_animation_option_col_code_animation`
FOREIGN KEY (`D_AN_CODE`)
REFERENCES `brest2016`.`tb_animation_option` (`D_AN_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_animation_option_col_code_option`
FOREIGN KEY (`D_OP_CODE`)
REFERENCES `brest2016`.`tb_animation_option` (`D_OP_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_animation_option_col_debut`
FOREIGN KEY (`DT_ANO_DEBUT`)
REFERENCES `brest2016`.`tb_animation_option` (`DT_ANO_DEBUT`)
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 :(得分:1)
必须为外键中指定的列编制索引。
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_animation_option` (
`D_AN_CODE` INT(3) NOT NULL,
`D_OP_CODE` INT(3) NOT NULL,
`DT_ANO_DEBUT` DATETIME NOT NULL,
`DT_ANO_FIN` DATETIME NOT NULL,
`D_ANO_PLACES_MAX` INT(3) NOT NULL,
`D_ANO_DUREE` INT(3) NOT NULL,
PRIMARY KEY (`D_AN_CODE`, `D_OP_CODE`, `DT_ANO_DEBUT`),
INDEX `fk_tb_option_idx` (`D_OP_CODE` ASC),
INDEX `dt_ano_debut_idx` (`T_ANO_DEBUT`), -- Add this line
CONSTRAINT `fk_tb_animation_col_code`
FOREIGN KEY (`D_AN_CODE`)
REFERENCES `brest2016`.`tb_animation` (`D_AN_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_option_col_code`
FOREIGN KEY (`D_OP_CODE`)
REFERENCES `brest2016`.`tb_option` (`D_OP_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = 'association entre une animation et une option';