无法添加外键约束,我无法找到错误

时间:2018-04-04 00:13:46

标签: mysql foreign-keys

我刚从这开始,但是当我将我的文件导入sql以填充所有信息时,它向我显示上面的错误,我知道它已经给出,因为它'不一样的长度或相同的类型,所以我试图在飞机代码或工作台上寻找它,我无法找到它。有任何想法吗? 我非常感谢你的帮助。

-- MySQL Script generated by MySQL Workbench
    -- Tue Apr  3 18:56:56 2018
    -- 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 dhl_dummy
-- -----------------------------------------------------
-- Presentación DHL para importar a Microsoft BI.

-- -----------------------------------------------------
-- Schema dhl_dummy
--
-- Presentación DHL para importar a Microsoft BI.
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `dhl_dummy` DEFAULT CHARACTER SET latin1 COLLATE latin1_spanish_ci ;
USE `dhl_dummy` ;

-- -----------------------------------------------------
-- Table `dhl_dummy`.`Proveedor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `dhl_dummy`.`Proveedor` (
  `Clave_Prov` INT NOT NULL,
  `Nombre` VARCHAR(45) NOT NULL,
  `Telefono` VARCHAR(15) NOT NULL,
  `CalleyNum` VARCHAR(60) NULL,
  `Colonia` VARCHAR(45) NULL,
  `Ciudad` VARCHAR(45) NOT NULL,
  `Estado` VARCHAR(45) NOT NULL,
  `CP` INT NULL,
  PRIMARY KEY (`Clave_Prov`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dhl_dummy`.`PT`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `dhl_dummy`.`PT` (
  `Num_art` INT NOT NULL,
  `stock_PT` INT NOT NULL,
  `Nombre` VARCHAR(45) NOT NULL,
  `Precio` DECIMAL(10,2) NOT NULL,
  `Prec_pub` DECIMAL(10,2) NOT NULL,
  PRIMARY KEY (`Num_art`, `stock_PT`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dhl_dummy`.`Empleado`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `dhl_dummy`.`Empleado` (
  `idEmpleado` INT NOT NULL,
  `Nombre` VARCHAR(35) NOT NULL,
  `Apellidos` VARCHAR(60) NOT NULL,
  `CalleyNum` VARCHAR(60) NOT NULL,
  `Colonia` VARCHAR(30) NOT NULL,
  `Ciudad` VARCHAR(45) NOT NULL,
  `Estado` VARCHAR(20) NOT NULL,
  `CP` INT NOT NULL,
  `Salario` DECIMAL(10,2) NOT NULL,
  `Telefono` INT NULL,
  `id_depto` INT NOT NULL,
  PRIMARY KEY (`idEmpleado`, `id_depto`),
  INDEX `id_depto_idx` (`id_depto` ASC),
  CONSTRAINT `id_depto`
    FOREIGN KEY (`id_depto`)
    REFERENCES `dhl_dummy`.`Departamento` (`idDepartamento`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dhl_dummy`.`Departamento`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `dhl_dummy`.`Departamento` (
  `idDepartamento` INT NOT NULL,
  `Area` VARCHAR(45) NOT NULL,
  `id_emp` INT NOT NULL,
  PRIMARY KEY (`idDepartamento`, `id_emp`),
  INDEX `Id_empleado_idx` (`id_emp` ASC),
  CONSTRAINT `Id_empleado`
    FOREIGN KEY (`id_emp`)
    REFERENCES `dhl_dummy`.`Empleado` (`idEmpleado`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dhl_dummy`.`Proporciona`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `dhl_dummy`.`Proporciona` (
  `Clave_Prov` INT NOT NULL,
  `Num_Art` INT NOT NULL,
  `Precio_proy` DECIMAL(10,2) NOT NULL,
  `cantidad` INT NOT NULL,
  PRIMARY KEY (`Clave_Prov`, `Num_Art`),
  CONSTRAINT `Clav_prov`
    FOREIGN KEY (`Clave_Prov`)
    REFERENCES `dhl_dummy`.`Proveedor` (`Clave_Prov`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dhl_dummy`.`Insumo`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `dhl_dummy`.`Insumo` (
  `idInsumo` INT NOT NULL,
  `Nombre` VARCHAR(30) NOT NULL,
  `fecha_ingreso` DATE NOT NULL,
  `fecha_exp` DATE NOT NULL,
  `stock_insumo` INT NOT NULL,
  PRIMARY KEY (`idInsumo`, `fecha_ingreso`, `fecha_exp`, `stock_insumo`),
  CONSTRAINT `Num_Art`
    FOREIGN KEY (`idInsumo`)
    REFERENCES `dhl_dummy`.`Proporciona` (`Num_Art`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dhl_dummy`.`Re_stock_insumo`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `dhl_dummy`.`Re_stock_insumo` (
  `Num_pedido` INT NOT NULL,
  `id_Insumo` INT NOT NULL,
  `cant_actual` INT NOT NULL,
  `Fecha_pedido` DATETIME NOT NULL,
  `Cantidad` INT NOT NULL,
  `fecha_llegada` DATE NOT NULL,
  PRIMARY KEY (`Num_pedido`, `id_Insumo`, `cant_actual`),
  INDEX `stock_actual_f_Insum_idx` (`cant_actual` ASC),
  CONSTRAINT `idInsumo`
    FOREIGN KEY (`id_Insumo`)
    REFERENCES `dhl_dummy`.`Insumo` (`idInsumo`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `Num_art`
    FOREIGN KEY (`id_Insumo`)
    REFERENCES `dhl_dummy`.`PT` (`Num_art`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `stock_actual_f_Insum`
    FOREIGN KEY (`cant_actual`)
    REFERENCES `dhl_dummy`.`Insumo` (`stock_insumo`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dhl_dummy`.`Inventario`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `dhl_dummy`.`Inventario` (
  `Ubicacion` VARCHAR(40) NOT NULL,
  `Refrigeracion` TINYINT NOT NULL,
  `id_f_art` INT NOT NULL,
  `id_f_insumo` INT NOT NULL,
  `stock` INT NOT NULL,
  PRIMARY KEY (`Ubicacion`, `id_f_art`, `id_f_insumo`),
  INDEX `id_f_art_idx` (`id_f_art` ASC),
  INDEX `id_f_insumo_idx` (`id_f_insumo` ASC),
  INDEX `stock_f_inv_insum_idx` (`stock` ASC),
  CONSTRAINT `id_f_art`
    FOREIGN KEY (`id_f_art`)
    REFERENCES `dhl_dummy`.`PT` (`Num_art`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `id_f_insumo`
    FOREIGN KEY (`id_f_insumo`)
    REFERENCES `dhl_dummy`.`Insumo` (`idInsumo`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `stock_f_inv_insum`
    FOREIGN KEY (`stock`)
    REFERENCES `dhl_dummy`.`Insumo` (`stock_insumo`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `stock_f_inv_PT`
    FOREIGN KEY (`stock`)
    REFERENCES `dhl_dummy`.`PT` (`stock_PT`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dhl_dummy`.`Re_stock_cliente`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `dhl_dummy`.`Re_stock_cliente` (
  `id_pedido` INT NOT NULL AUTO_INCREMENT,
  `cliente_pedido` INT NOT NULL,
  `art_pedido` INT NOT NULL,
  `fecha_pedido` DATETIME(6) NOT NULL,
  `ETA` DATETIME(6) NOT NULL,
  PRIMARY KEY (`id_pedido`, `cliente_pedido`, `fecha_pedido`, `ETA`),
  INDEX `cliente_pedido_f_idx` (`cliente_pedido` ASC),
  INDEX `art_ordenado_idx` (`art_pedido` ASC),
  INDEX `fecha_client_idx` (`fecha_pedido` ASC),
  CONSTRAINT `cliente_pedido_f`
    FOREIGN KEY (`cliente_pedido`)
    REFERENCES `dhl_dummy`.`Cliente` (`idCliente`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `art_ordenado`
    FOREIGN KEY (`art_pedido`)
    REFERENCES `dhl_dummy`.`PT` (`Num_art`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fecha_client`
    FOREIGN KEY (`fecha_pedido`)
    REFERENCES `dhl_dummy`.`Cliente` (`fecha_pedido`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `dhl_dummy`.`Cliente`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `dhl_dummy`.`Cliente` (
  `idCliente` INT NOT NULL,
  `Nombre` VARCHAR(45) NOT NULL,
  `Direccion` VARCHAR(45) NOT NULL,
  `Telefono` VARCHAR(45) NULL,
  `Pedido_arts` INT NOT NULL,
  `fecha_pedido` DATETIME(6) NOT NULL,
  `fecha_ETA` DATETIME(6) NOT NULL,
  PRIMARY KEY (`idCliente`, `fecha_pedido`, `fecha_ETA`),
  INDEX `art_faltante_f_idx` (`Pedido_arts` ASC),
  INDEX `ETA_producto_idx` (`fecha_ETA` ASC),
  CONSTRAINT `art_faltante_f`
    FOREIGN KEY (`Pedido_arts`)
    REFERENCES `dhl_dummy`.`Re_stock_cliente` (`art_pedido`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `ETA_producto`
    FOREIGN KEY (`fecha_ETA`)
    REFERENCES `dhl_dummy`.`Re_stock_cliente` (`ETA`)
    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;

1 个答案:

答案 0 :(得分:0)

我测试了您的脚本,发现无法创建表Insumo

然后我运行SHOW ENGINE INNODB STATUS以获取有关外键失败原因的更详细信息:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
2018-04-03 18:22:32 70000df33000 Error in foreign key constraint of table dhl_dummy/insumo:

    FOREIGN KEY (`idInsumo`)
    REFERENCES `dhl_dummy`.`Proporciona` (`Num_Art`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.

阅读最后一句话。

创建外键时,必须匹配所引用表中主键的所有列。如果引用的表在其主键中有两列,则外键必须有两列。

在您的情况下,您尝试在引用另一个表的主键的第二列的一列idInsumo上创建外键。

FOREIGN KEY (`idInsumo`)
REFERENCES `dhl_dummy`.`Proporciona` (`Num_Art`)

Proporciona的主键是两列:

PRIMARY KEY (`Clave_Prov`, `Num_Art`),

您不应该创建引用该主键的 part 的外键。

我没有进一步测试,但我查看了后面的表定义,我发现你在其他几个表中也有类似的错误。您正在尝试从与其引用的表的整个主键不匹配的列创建外键。