我正在为学院写一个小的webapp,但我无法弄清楚为什么MySQL脚本无法通过。它说“无法添加外键约束”
这是我正在尝试运行的脚本。
1)我在所有桌子上检查了InnoDB。 2)如果FK有任何错误,但没有任何结果。
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';
CREATE SCHEMA IF NOT EXISTS `bdchile` DEFAULT CHARACTER SET utf8 ;
USE `bdchile` ;
-- -----------------------------------------------------
-- Table `bdchile`.`REGION`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bdchile`.`REGION` (
`REGION_ID` INT(2) NOT NULL DEFAULT '0' ,
`REGION_NOMBRE` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`REGION_ID`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `bdchile`.`PROVINCIA`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bdchile`.`PROVINCIA` (
`PROVINCIA_ID` INT(3) NOT NULL DEFAULT '0' ,
`PROVINCIA_NOMBRE` VARCHAR(23) NOT NULL ,
`REGION_REGION_ID` INT(2) NOT NULL ,
PRIMARY KEY (`PROVINCIA_ID`) ,
INDEX `fk_PROVINCIA_REGION_idx` (`REGION_REGION_ID` ASC) ,
CONSTRAINT `fk_PROVINCIA_REGION`
FOREIGN KEY (`REGION_REGION_ID` )
REFERENCES `bdchile`.`REGION` (`REGION_ID` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `bdchile`.`COMUNA`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bdchile`.`COMUNA` (
`COMUNA_ID` INT(5) NOT NULL DEFAULT '0' ,
`COMUNA_NOMBRE` VARCHAR(20) NOT NULL ,
`PROVINCIA_PROVINCIA_ID` INT(3) NOT NULL ,
PRIMARY KEY (`COMUNA_ID`) ,
INDEX `fk_COMUNA_PROVINCIA_idx` (`PROVINCIA_PROVINCIA_ID` ASC) ,
CONSTRAINT `fk_COMUNA_PROVINCIA`
FOREIGN KEY (`PROVINCIA_PROVINCIA_ID` )
REFERENCES `bdchile`.`PROVINCIA` (`PROVINCIA_ID` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `bdchile`.`USUARIO`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bdchile`.`USUARIO` (
`USUARIO_ID` INT(9) NOT NULL ,
`USUARIO_CLAVE` VARCHAR(12) NOT NULL ,
`USUARIO_PRIVILEGIO` INT(1) NOT NULL DEFAULT 3 ,
`USUARIO_ESTADO` INT(1) NOT NULL DEFAULT 1 ,
PRIMARY KEY (`USUARIO_ID`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 1;
-- -----------------------------------------------------
-- Table `bdchile`.`SEXO`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bdchile`.`SEXO` (
`SEXO_ID` INT(1) NOT NULL ,
`SEXO_NOMBRE` VARCHAR(8) NOT NULL ,
PRIMARY KEY (`SEXO_ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `bdchile`.`ESTUDIO`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bdchile`.`ESTUDIO` (
`ESTUDIO_ID` INT(1) NOT NULL ,
`ESTUDIO_NIVEL` VARCHAR(51) NOT NULL ,
PRIMARY KEY (`ESTUDIO_ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `bdchile`.`PERSONA`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bdchile`.`PERSONA` (
`PERSONA_RUN` INT(10) NOT NULL ,
`PERSONA_NOMBRES` VARCHAR(45) NOT NULL ,
`PERSONA_PRIMER_APELLIDO` VARCHAR(20) NOT NULL ,
`PERSONA_SEGUNDO_APELLIDO` VARCHAR(20) NULL ,
`PERSONA_FECHA_NACIMIENTO` DATE NOT NULL ,
`PERSONA_DIRECCION` VARCHAR(45) NOT NULL ,
`COMUNA_COMUNA_ID` INT(5) NOT NULL ,
`SEXO_SEXO_ID` INT(1) NOT NULL ,
`ESTUDIO_ESTUDIO_ID` INT(1) NOT NULL ,
PRIMARY KEY (`PERSONA_RUN`) ,
INDEX `fk_PERSONA_COMUNA_idx` (`COMUNA_COMUNA_ID` ASC, `PERSONA_RUN` ASC) ,
INDEX `fk_PERSONA_SEXO_idx` (`SEXO_SEXO_ID` ASC) ,
INDEX `fk_PERSONA_ESTUDIO_idx` (`ESTUDIO_ESTUDIO_ID` ASC) ,
CONSTRAINT `fk_PERSONA_COMUNA`
FOREIGN KEY (`COMUNA_COMUNA_ID` , `PERSONA_RUN` )
REFERENCES `bdchile`.`COMUNA` (`COMUNA_ID` , `COMUNA_ID` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_PERSONA_SEXO`
FOREIGN KEY (`SEXO_SEXO_ID` )
REFERENCES `bdchile`.`SEXO` (`SEXO_ID` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_PERSONA_ESTUDIO`
FOREIGN KEY (`ESTUDIO_ESTUDIO_ID` )
REFERENCES `bdchile`.`ESTUDIO` (`ESTUDIO_ID` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
,输出就是这样:
**[Err] 1215 - Cannot add foreign key constraint**