Cannot add foreign key constraint, i can't find the mistake - mysql

I'm just starting with this, but when i imported my file to sql to beguin filling all the information, it shows me the error on the above, i know it's given because it's not the same length or the same type, so i tried to look for it on the plane code or the work bench and i couldn't find it. Any ideas?
I'll really appreciate the help.
-- 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;

I tested your script, and found the table Insumo failed to be created.
Then I ran SHOW ENGINE INNODB STATUS to get more detailed information on the cause of the foreign key failure:
------------------------
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.
Read the last sentence.
When you create a foreign key, you must match all the columns of the primary key in the table you reference. If the referenced table has two columns in its primary key, your foreign key must have two columns.
In your case, you tried to make a foreign key on one column idInsumo referencing the second column of another table's primary key.
FOREIGN KEY (`idInsumo`)
REFERENCES `dhl_dummy`.`Proporciona` (`Num_Art`)
But the primary key of Proporciona is two columns:
PRIMARY KEY (`Clave_Prov`, `Num_Art`),
You shouldn't create a foreign key that references part of that primary key.
I didn't test further, but I looked at the later table definitions and I see you have similar mistakes in several other tables. You're trying to make foreign keys from columns that don't match the whole primary key of the table they reference.

Related

Error 1215: Cannot add foreign key constraint mysql workbench 1

I'm trying to forward engineering my schema with MySQL Workbench but this error appears. I'm sure that the foreign key refers to the primary key of the parent table and their are both VARCHAR(45).
This is my schema:
-- 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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema PiscineRoma
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema PiscineRoma
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `PiscineRoma` ;
USE `PiscineRoma` ;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Piscina`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Piscina` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Piscina` (
`Nome` VARCHAR(45) NOT NULL,
`NumeroDiTelefono` VARCHAR(45) NOT NULL,
`Indirizzo` VARCHAR(45) NOT NULL,
`NomeResponsabile` VARCHAR(45) NOT NULL,
`DataInizioDisponibilitàVascaEsterna` DATE NULL,
`DataFineDisponibilitàVascaEsterna` DATE NULL,
PRIMARY KEY (`Nome`),
UNIQUE INDEX `NumeroDiTelefono_UNIQUE` (`NumeroDiTelefono` ASC),
UNIQUE INDEX `Indirizzo_UNIQUE` (`Indirizzo` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Persona`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Persona` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Persona` (
`CF` VARCHAR(16) NOT NULL,
`Nome` VARCHAR(45) NOT NULL,
`Cognome` VARCHAR(45) NOT NULL,
`Indirizzo` VARCHAR(45) NOT NULL,
`Età` INT NOT NULL,
`Telefono` VARCHAR(20) NULL,
`Cellulare` VARCHAR(20) NULL,
`IndirizzoEMail` VARCHAR(45) NULL,
PRIMARY KEY (`CF`),
UNIQUE INDEX `Telefono_UNIQUE` (`Telefono` ASC),
UNIQUE INDEX `Cellulare_UNIQUE` (`Cellulare` ASC),
UNIQUE INDEX `IndirizzoEMail_UNIQUE` (`IndirizzoEMail` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`IngressoSingolo`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`IngressoSingolo` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`IngressoSingolo` (
`NomePiscina` VARCHAR(45) NOT NULL,
`Persona_CF` VARCHAR(16) NOT NULL,
`DataIngresso` DATETIME NOT NULL,
PRIMARY KEY (`NomePiscina`, `Persona_CF`),
INDEX `fk_Piscina_has_PersonaNonIscritta_Piscina_idx` (`NomePiscina` ASC),
INDEX `fk_IngressoSingolo_Persona1_idx` (`Persona_CF` ASC),
CONSTRAINT `fk_Piscina_has_PersonaNonIscritta_Piscina`
FOREIGN KEY (`NomePiscina`)
REFERENCES `PiscineRoma`.`Piscina` (`Nome`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_IngressoSingolo_Persona1`
FOREIGN KEY (`Persona_CF`)
REFERENCES `PiscineRoma`.`Persona` (`CF`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Insegnante`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Insegnante` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Insegnante` (
`CF` VARCHAR(16) NOT NULL,
`Nome` VARCHAR(45) NOT NULL,
`Cognome` VARCHAR(45) NOT NULL,
`Telefono` VARCHAR(20) NULL,
PRIMARY KEY (`CF`),
UNIQUE INDEX `Telefono_UNIQUE` (`Telefono` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Qualifica`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Qualifica` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Qualifica` (
`Nome` VARCHAR(45) NOT NULL,
PRIMARY KEY (`Nome`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Possiede`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Possiede` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Possiede` (
`Insegnante` VARCHAR(16) NOT NULL,
`Qualifica` VARCHAR(45) NOT NULL,
PRIMARY KEY (`Insegnante`, `Qualifica`),
INDEX `fk_Insegnante_has_Qualifica_Qualifica1_idx` (`Qualifica` ASC),
INDEX `fk_Insegnante_has_Qualifica_Insegnante1_idx` (`Insegnante` ASC),
CONSTRAINT `fk_Insegnante_has_Qualifica_Insegnante1`
FOREIGN KEY (`Insegnante`)
REFERENCES `PiscineRoma`.`Insegnante` (`CF`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Insegnante_has_Qualifica_Qualifica1`
FOREIGN KEY (`Qualifica`)
REFERENCES `PiscineRoma`.`Qualifica` (`Nome`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Impiego`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Impiego` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Impiego` (
`Codice` INT NOT NULL AUTO_INCREMENT,
`Inizio` DATE NOT NULL,
`Fine` DATE NULL,
`Tipo` VARCHAR(45) NOT NULL DEFAULT 'impiego_corrente',
`NomePiscina` VARCHAR(45) NOT NULL,
`Insegnante` VARCHAR(16) NOT NULL,
PRIMARY KEY (`Codice`),
INDEX `fk_Impiego_Piscina1_idx` (`NomePiscina` ASC),
INDEX `fk_Impiego_Insegnante1_idx` (`Insegnante` ASC),
CONSTRAINT `fk_Impiego_Piscina1`
FOREIGN KEY (`NomePiscina`)
REFERENCES `PiscineRoma`.`Piscina` (`Nome`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_Impiego_Insegnante1`
FOREIGN KEY (`Insegnante`)
REFERENCES `PiscineRoma`.`Insegnante` (`CF`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Corso`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Corso` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Corso` (
`NomeAttivita` VARCHAR(45) NOT NULL,
`NomePiscina` VARCHAR(45) NOT NULL,
`Costo` SMALLINT(2) NOT NULL,
`NumeroMinimoDiPartecipanti` SMALLINT(2) NOT NULL,
`NumeroMassimoDiPartecipanti` SMALLINT(2) NOT NULL,
PRIMARY KEY (`NomeAttivita`, `NomePiscina`),
INDEX `fk_Corso_Piscina1_idx` (`NomePiscina` ASC),
CONSTRAINT `fk_Corso_Piscina1`
FOREIGN KEY (`NomePiscina`)
REFERENCES `PiscineRoma`.`Piscina` (`Nome`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB
PACK_KEYS = DEFAULT
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Lezione`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Lezione` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Lezione` (
`Codice` INT NOT NULL AUTO_INCREMENT,
`NomeAttivitaCorso` VARCHAR(45) NOT NULL,
`NomePiscina` VARCHAR(45) NOT NULL,
`Insegnante` VARCHAR(16) NOT NULL,
`Dat` DATETIME NOT NULL,
`Numero` INT NOT NULL,
PRIMARY KEY (`Codice`),
INDEX `fk_Lezione_Insegnante1_idx` (`Insegnante` ASC),
CONSTRAINT `fk_Lezione_Corso1`
FOREIGN KEY (`NomeAttivitaCorso` , `NomePiscina`)
REFERENCES `PiscineRoma`.`Corso` (`NomeAttivita` , `NomePiscina`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_Lezione_Insegnante1`
FOREIGN KEY (`Insegnante`)
REFERENCES `PiscineRoma`.`Insegnante` (`CF`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Iscrizione`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Iscrizione` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Iscrizione` (
`Codice` INT NOT NULL AUTO_INCREMENT,
`DataIscrizione` DATE NOT NULL,
`CodiceCertificato` INT NULL,
`DataCertificato` DATE NULL,
`Medico` VARCHAR(45) NULL,
`NomeAttivitaCorso` VARCHAR(45) NOT NULL,
`NomePiscina` VARCHAR(45) NOT NULL,
`PersonaIscritta` VARCHAR(16) NOT NULL,
PRIMARY KEY (`Codice`),
INDEX `fk_Iscrizione_Corso1_idx` (`NomeAttivitaCorso` ASC, `NomePiscina` ASC),
INDEX `fk_Iscrizione_PersonaIscritta1_idx` (`PersonaIscritta` ASC),
UNIQUE INDEX `CodiceCertificato_UNIQUE` (`CodiceCertificato` ASC),
CONSTRAINT `fk_Iscrizione_Corso1`
FOREIGN KEY (`NomeAttivitaCorso` , `NomePiscina`)
REFERENCES `PiscineRoma`.`Corso` (`NomeAttivita` , `NomePiscina`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_Iscrizione_PersonaIscritta1`
FOREIGN KEY (`PersonaIscritta`)
REFERENCES `PiscineRoma`.`Persona` (`CF`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
USE `PiscineRoma` ;
The message log from MySQL Workbench tells that error is in the PiscineRoma.Corso.
I also tryed to run the command SHOW ENGINE INNODB STATUS and under the label the error is:
2019-02-14 19:52:03 0x7f9fa9700700 Error in foreign key constraint of table PiscineRoma/Iscrizione:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
CONSTRAINT `fk_Iscrizione_Corso1` FOREIGN KEY (`NomeAttivita`, `NomePiscina`) REFERENCES `Corso` (`NomeAttività`, `NomePiscina`) ON DELETE NO ACTION ON UPDATE CASCADE
The index in the foreign key in table is fk_Iscrizione_Corso1_idx
Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html for correct foreign key definition.
Thank you all for answers.

Error #1215 - Cannot add foreign key constraint

I have generated a sql script with MySql Workbench.
When I try to create my database I have "Error 1215 1215 - Cannot add foreign key constraint".
The problem seems to be with my last foreign key in the last create table :
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)
This is my entire script:
-- 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;
The column named in a foreign key must be indexed.
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';

Error Code 1022 on MySQL WorkBench

I'm trying to create these tables but I'm getting Error 1022 when creating the table cadastro_fornecedor.
I don't know where to start to solve this issue so any kind of help is welcome.
Here is the .sql:
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
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`administrador`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`administrador` (
`id_administrador` INT NOT NULL,
`nome` VARCHAR(45) NULL,
`senha` VARCHAR(45) NULL,
`CPF` INT NULL,
`endereço` VARCHAR(45) NULL,
`data_nascimento` DATE NULL,
PRIMARY KEY (`id_administrador`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`cadastro_usuario`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`cadastro_usuario` (
`id_cadastro` INT NOT NULL AUTO_INCREMENT,
`data_cadastro` DATETIME NOT NULL,
`id_administrador` INT NULL,
`usuario` INT NULL,
PRIMARY KEY (`id_cadastro`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `matricula_idx` (`usuario` ASC),
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `matricula`
FOREIGN KEY (`usuario`)
REFERENCES `mydb`.`usuario_cadastrado` (`matricula`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`usuario_cadastrado`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`usuario_cadastrado` (
`matricula` INT NOT NULL,
`nome` VARCHAR(45) NULL,
`endereco` VARCHAR(45) NULL,
`senha` VARCHAR(45) NULL,
`nacionalidade` VARCHAR(45) NULL DEFAULT 'brasileiro',
`naturalidade` VARCHAR(45) NULL,
`sexo` CHAR NULL,
`data_nascimento` DATE NULL,
`CPF` INT NULL,
`nome_pai` VARCHAR(45) NULL,
`nome_mae` VARCHAR(45) NULL,
`telefone_celular` VARCHAR(15) NULL,
`telefone_fixo` VARCHAR(15) NULL,
`id_cadastro` INT NULL,
PRIMARY KEY (`matricula`),
INDEX `id_cadastro_idx` (`id_cadastro` ASC),
CONSTRAINT `id_cadastro`
FOREIGN KEY (`id_cadastro`)
REFERENCES `mydb`.`cadastro_usuario` (`id_cadastro`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`obra`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`obra` (
`ISBN` VARCHAR(20) NOT NULL,
`titulo` VARCHAR(45) NULL,
`autor` VARCHAR(45) NULL,
`ano` YEAR NULL,
`editora` VARCHAR(45) NULL,
`categoria` VARCHAR(45) NULL,
`assunto` VARCHAR(45) NULL,
`descricao` VARCHAR(45) NULL,
PRIMARY KEY (`ISBN`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`fornecedores`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`fornecedores` (
`CNPJ` VARCHAR(45) NOT NULL,
`nome` VARCHAR(45) NOT NULL,
`endereco` VARCHAR(45) NULL,
`telefone` VARCHAR(45) NULL,
PRIMARY KEY (`CNPJ`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`exemplar`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`exemplar` (
`codigo_exemplar` INT NOT NULL,
`data_aquisicao` DATE NULL,
`edição` INT NULL,
`volume` INT NULL,
`paginas` INT NULL,
`ISBN` VARCHAR(20) NULL,
`CNPJ` VARCHAR(45) NULL,
PRIMARY KEY (`codigo_exemplar`),
INDEX `ISBN_idx` (`ISBN` ASC),
INDEX `CNPJ_idx` (`CNPJ` ASC),
CONSTRAINT `ISBN`
FOREIGN KEY (`ISBN`)
REFERENCES `mydb`.`obra` (`ISBN`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `CNPJ`
FOREIGN KEY (`CNPJ`)
REFERENCES `mydb`.`fornecedores` (`CNPJ`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`cadastro_fornecedor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`cadastro_fornecedor` (
`id_cadastro` INT NOT NULL AUTO_INCREMENT,
`data` DATETIME NULL,
`id_administrador` INT NULL,
`CNPJ` VARCHAR(45) NULL,
PRIMARY KEY (`id_cadastro`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `CNPJ_idx` (`CNPJ` ASC),
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `CNPJ`
FOREIGN KEY (`CNPJ`)
REFERENCES `mydb`.`fornecedores` (`CNPJ`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`emprestimo`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`emprestimo` (
`id_emprestimo` INT NOT NULL AUTO_INCREMENT,
`data_emprestimo` DATE NULL,
`data_devolucao_prevista` DATE NULL,
`matricula` INT NULL,
`id_administrador` INT NULL,
`codigo_exemplar` INT NULL,
PRIMARY KEY (`id_emprestimo`),
INDEX `matricula_idx` (`matricula` ASC),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `codigo_exemplar_idx` (`codigo_exemplar` ASC),
CONSTRAINT `matricula`
FOREIGN KEY (`matricula`)
REFERENCES `mydb`.`usuario_cadastrado` (`matricula`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `codigo_exemplar`
FOREIGN KEY (`codigo_exemplar`)
REFERENCES `mydb`.`exemplar` (`codigo_exemplar`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`devolucao`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`devolucao` (
`id_devolucao` INT NOT NULL,
`data_devolucao` DATE NULL,
`multa` INT NULL,
`matricula` INT NULL,
`id_administrador` INT NULL,
PRIMARY KEY (`id_devolucao`),
INDEX `matricula_idx` (`matricula` ASC),
INDEX `id_administrador_idx` (`id_administrador` ASC),
CONSTRAINT `matricula`
FOREIGN KEY (`matricula`)
REFERENCES `mydb`.`usuario_cadastrado` (`matricula`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`exclusao`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`exclusao` (
`id_exclusao` INT NOT NULL AUTO_INCREMENT,
`data_exclusao` DATE NULL,
`id_administrador` INT NULL,
`codigo_exemplar` INT NULL,
PRIMARY KEY (`id_exclusao`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `codigo_exemplar_idx` (`codigo_exemplar` ASC),
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `codigo_exemplar`
FOREIGN KEY (`codigo_exemplar`)
REFERENCES `mydb`.`exemplar` (`codigo_exemplar`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`registro`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`registro` (
`id_registro` INT NOT NULL AUTO_INCREMENT,
`data_registro` DATE NULL,
`id_administrador` INT NULL,
`codigo_exemplar` INT NULL,
PRIMARY KEY (`id_registro`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `codigo_exemplar_idx` (`codigo_exemplar` ASC),
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `codigo_exemplar`
FOREIGN KEY (`codigo_exemplar`)
REFERENCES `mydb`.`exemplar` (`codigo_exemplar`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`reserva`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`reserva` (
`id_reserva` INT NOT NULL,
`data_reserva` DATE NULL,
`matricula` INT NULL,
`ISBN` VARCHAR(20) NULL,
PRIMARY KEY (`id_reserva`),
INDEX `matricula_idx` (`matricula` ASC),
INDEX `ISBN_idx` (`ISBN` ASC),
CONSTRAINT `matricula`
FOREIGN KEY (`matricula`)
REFERENCES `mydb`.`usuario_cadastrado` (`matricula`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ISBN`
FOREIGN KEY (`ISBN`)
REFERENCES `mydb`.`obra` (`ISBN`)
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;
I appreciate any kind of help.
Thanks.
You're reusing existing constraint names (id_administrador is already used as a constraint name in the table cadastro_usuario and CNPJ is used in the table exemplar), you'll have to give your constraints unique names (here I renamed them to cf_id_administrador/CF_CNPJ);
-- -----------------------------------------------------
-- Table `mydb`.`cadastro_fornecedor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`cadastro_fornecedor` (
`id_cadastro` INT NOT NULL AUTO_INCREMENT,
`data` DATETIME NULL,
`id_administrador` INT NULL,
`CNPJ` VARCHAR(45) NULL,
PRIMARY KEY (`id_cadastro`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `CNPJ_idx` (`CNPJ` ASC),
CONSTRAINT `cf_id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `CF_CNPJ`
FOREIGN KEY (`CNPJ`)
REFERENCES `mydb`.`fornecedores` (`CNPJ`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

Create table failed in MySQL

I have designed the schema and generated the create SQL below using the MySQLWorkbench.
This query through this error: Can't create table 'museum.statue_details'. Could someone please tell me what seems to be the problem and how to solve it? Thanks.
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 `museum` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `museum` ;
-- -----------------------------------------------------
-- Table `museum`.`collection`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`collection` ;
CREATE TABLE IF NOT EXISTS `museum`.`collection` (
`collection_id` INT NOT NULL,
`name` VARCHAR(45) NULL,
`type` VARCHAR(45) NULL,
`description` VARCHAR(45) NULL,
`address` VARCHAR(45) NULL,
`phone` VARCHAR(45) NULL,
`current_contact_person` VARCHAR(45) NULL,
PRIMARY KEY (`collection_id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`country`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`country` ;
CREATE TABLE IF NOT EXISTS `museum`.`country` (
`country_id` INT NOT NULL,
`country_name` VARCHAR(45) NULL,
PRIMARY KEY (`country_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`epoch`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`epoch` ;
CREATE TABLE IF NOT EXISTS `museum`.`epoch` (
`epoch_id` INT NOT NULL,
`epoch_name` VARCHAR(45) NULL,
PRIMARY KEY (`epoch_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`artefact`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`artefact` ;
CREATE TABLE IF NOT EXISTS `museum`.`artefact` (
`artefact_id` INT NOT NULL,
`collection_id` INT NOT NULL,
`country_id` INT NOT NULL,
`epoch_id` INT NOT NULL,
`title` VARCHAR(50) NULL,
`description` TEXT NULL,
`year` DATE NULL,
PRIMARY KEY (`artefact_id`, `collection_id`, `country_id`, `epoch_id`),
INDEX `collection_id_idx` (`collection_id` ASC),
INDEX `country_id_idx` (`country_id` ASC),
INDEX `epoch_id_idx` (`epoch_id` ASC),
CONSTRAINT `collection_id`
FOREIGN KEY (`collection_id`)
REFERENCES `museum`.`collection` (`collection_id`)
ON DELETE RESTRICT
ON UPDATE NO ACTION,
CONSTRAINT `country_id`
FOREIGN KEY (`country_id`)
REFERENCES `museum`.`country` (`country_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `epoch_id`
FOREIGN KEY (`epoch_id`)
REFERENCES `museum`.`epoch` (`epoch_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`painting_details`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`painting_details` ;
CREATE TABLE IF NOT EXISTS `museum`.`painting_details` (
`artefact_id` INT NOT NULL,
`type` VARCHAR(45) NULL,
`material` VARCHAR(45) NULL,
`style` VARCHAR(45) NULL,
PRIMARY KEY (`artefact_id`),
CONSTRAINT `artefact_id`
FOREIGN KEY (`artefact_id`)
REFERENCES `museum`.`artefact` (`artefact_id`)
ON DELETE RESTRICT
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`statue_details`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`statue_details` ;
CREATE TABLE IF NOT EXISTS `museum`.`statue_details` (
`artefact_id` INT NOT NULL,
`material` VARCHAR(45) NULL,
`height` DECIMAL(10,2) NULL,
`weight` DECIMAL(10,2) NULL,
`style` VARCHAR(45) NULL,
PRIMARY KEY (`artefact_id`),
CONSTRAINT `artefact_id`
FOREIGN KEY (`artefact_id`)
REFERENCES `museum`.`artefact` (`artefact_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`relic_details`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`relic_details` ;
CREATE TABLE IF NOT EXISTS `museum`.`relic_details` (
`artefact_id` INT NOT NULL,
`material` VARCHAR(45) NULL,
`usage` VARCHAR(45) NULL,
PRIMARY KEY (`artefact_id`),
CONSTRAINT `artefact_id`
FOREIGN KEY (`artefact_id`)
REFERENCES `museum`.`artefact` (`artefact_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`owner`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`owner` ;
CREATE TABLE IF NOT EXISTS `museum`.`owner` (
`owner_id` INT NOT NULL,
`owner_name` VARCHAR(45) NULL,
PRIMARY KEY (`owner_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`other_artefact`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`other_artefact` ;
CREATE TABLE IF NOT EXISTS `museum`.`other_artefact` (
`artefact_id` INT NOT NULL,
`name` VARCHAR(45) NULL,
`description` TEXT NULL,
PRIMARY KEY (`artefact_id`),
CONSTRAINT `artifact_id`
FOREIGN KEY (`artefact_id`)
REFERENCES `museum`.`artefact` (`artefact_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`borrowed_artefact`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`borrowed_artefact` ;
CREATE TABLE IF NOT EXISTS `museum`.`borrowed_artefact` (
`artefact_id` INT NOT NULL,
`owner_id` INT NULL,
`collection_id` INT NOT NULL,
`date_borrowed` DATE NULL,
`date_returned` DATE NULL,
INDEX `owner_id_idx` (`owner_id` ASC),
INDEX `artifact_id_idx` (`artefact_id` ASC),
INDEX `collection_id_idx` (`collection_id` ASC),
CONSTRAINT `artefact_id`
FOREIGN KEY (`artefact_id`)
REFERENCES `museum`.`artefact` (`artefact_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `owner_id`
FOREIGN KEY (`owner_id`)
REFERENCES `museum`.`owner` (`owner_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `collection_id`
FOREIGN KEY (`collection_id`)
REFERENCES `museum`.`collection` (`collection_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`permenent_artefact`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`permenent_artefact` ;
CREATE TABLE IF NOT EXISTS `museum`.`permenent_artefact` (
`artefact_id` INT NOT NULL,
`cost` DECIMAL(10,2) NULL,
`status` VARCHAR(45) NULL,
`date_acquired` DATE NULL,
PRIMARY KEY (`artefact_id`),
CONSTRAINT `artefact_id`
FOREIGN KEY (`artefact_id`)
REFERENCES `museum`.`artefact` (`artefact_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`creator`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`creator` ;
CREATE TABLE IF NOT EXISTS `museum`.`creator` (
`creator_id` INT NOT NULL,
`country_id` INT NULL COMMENT 'Country of origin',
`epoch_id` INT NULL,
`creator_name` VARCHAR(45) NULL COMMENT 'The creator name is unique',
`creator_dob` DATE NULL COMMENT 'Date of birth',
`creator_dod` DATE NULL COMMENT 'Date of died',
`main_style` VARCHAR(45) NULL,
`description` TEXT NULL,
PRIMARY KEY (`creator_id`),
UNIQUE INDEX `creator_name_UNIQUE` (`creator_name` ASC),
CONSTRAINT `country_id`
FOREIGN KEY (`creator_id`)
REFERENCES `museum`.`country` (`country_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `epoch_id`
FOREIGN KEY ()
REFERENCES `museum`.`epoch` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`created_artefact`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`created_artefact` ;
CREATE TABLE IF NOT EXISTS `museum`.`created_artefact` (
`artefact_id` INT NOT NULL,
`creator_id` INT NULL,
INDEX `creator_id_idx` (`creator_id` ASC),
CONSTRAINT `artefact_id`
FOREIGN KEY (`artefact_id`)
REFERENCES `museum`.`artefact` (`artefact_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `creator_id`
FOREIGN KEY (`creator_id`)
REFERENCES `museum`.`creator` (`creator_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`exhibition`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`exhibition` ;
CREATE TABLE IF NOT EXISTS `museum`.`exhibition` (
`exhibition_id` INT NOT NULL,
`country_id` VARCHAR(45) NULL,
`start_date` DATE NOT NULL,
`end_date` DATE NOT NULL,
PRIMARY KEY (`exhibition_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `museum`.`artifact_displayed_during_exhibition`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `museum`.`artifact_displayed_during_exhibition` ;
CREATE TABLE IF NOT EXISTS `museum`.`artifact_displayed_during_exhibition` (
`exhibition_id` INT NOT NULL,
`artefact_id` INT NULL,
PRIMARY KEY (`exhibition_id`),
INDEX `artifact_id_idx` (`artefact_id` ASC),
CONSTRAINT `exhibition_id`
FOREIGN KEY (`exhibition_id`)
REFERENCES `museum`.`exhibition` (`exhibition_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `artefact_id`
FOREIGN KEY (`artefact_id`)
REFERENCES `museum`.`artefact` (`artefact_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;
You have many constraints with the same name,change them or let mysql name them.
CONSTRAINT `artefact_id`
this repeats everywhere.There are others.
Also in museum.creator you have missing columns:
CONSTRAINT `epoch_id`
FOREIGN KEY ()
REFERENCES `museum`.`epoch` ()
You probably want epoch_id in between.All those fixed, it runs on my machine.
you should give other name of your foreign keys like that:
insetead of
CONSTRAINT `artefact_id`
FOREIGN KEY (`artefact_id`)
REFERENCES `museum`.`artefact` (`artefact_id`)
do:
CONSTRAINT FK_mykey --> give name as you like here to distinguich between others
FOREIGN KEY (`artefact_id`)
REFERENCES `artefact` (`artefact_id`)
create table syntax:
http://dev.mysql.com/doc/refman/5.7/en/create-table.html
I believe the issue lies with you declaring the table name with {name1}.{name2}.
CREATE TABLE IF NOT EXISTS `collection` (
`collection_id` INT NOT NULL,
`name` VARCHAR(45) NULL,
`type` VARCHAR(45) NULL,
`description` VARCHAR(45) NULL,
`address` VARCHAR(45) NULL,
`phone` VARCHAR(45) NULL,
`current_contact_person` VARCHAR(45) NULL,
PRIMARY KEY (`collection_id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC))
ENGINE = InnoDB;
`
worked for me

Trying to import SQL file in a xampp server returns error

I have done a ER diagram in Mysql Workbench, and I am trying load in my server with phpMyAdmin, but it returns me the next error:
Error
SQL Query:
-- ----------------------------------------------------- -- Table `BDA`.`UG` -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `BDA`.`UG` ( `numero_ug` INT NOT NULL, `nombre` VARCHAR(45) NOT NULL, `segunda_firma_autorizada` VARCHAR(45) NOT NULL, `fecha_creacion` DATE NOT NULL, `nombre_depto` VARCHAR(140) NOT NULL, `dni` INT NOT NULL, `anho_contable` INT NOT NULL, PRIMARY KEY (`numero_ug`), INDEX `nombre_depto_idx` (`nombre_depto` ASC), INDEX `dni_idx` (`dni` ASC), INDEX `anho_contable_idx` (`anho_contable` ASC), CONSTRAINT `nombre_depto` FOREIGN KEY (`nombre_depto`) REFERENCES `BDA`.`Departamento` (`nombre_depto`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `dni` FOREIGN KEY (`dni`) REFERENCES `BDA`.`Trabajador` (`dni`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `anho_contable` FOREIGN KEY (`anho_contable`) REFERENCES `BDA`.`Capitulo_Contable` (`anho_contable`) [...]
MySQL said: Documentation
#1022 - Can't write; duplicate key in table 'ug'
I export the result of the diagram from Mysql Workbench to a SQL file, and this file is what I'm trying to upload.
This is the file. I can not find the duplicate key.
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 `BDA` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `BDA` ;
-- -----------------------------------------------------
-- Table `BDA`.`Departamento`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Departamento` (
`nombre_depto` VARCHAR(140) NOT NULL,
`area_depto` VARCHAR(140) NOT NULL,
PRIMARY KEY (`nombre_depto`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`Trabajador`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Trabajador` (
`dni` INT NOT NULL,
`direccion` VARCHAR(140) NOT NULL,
`nombre` VARCHAR(45) NOT NULL,
`apellidos` VARCHAR(140) NOT NULL,
`fecha_nacimiento` DATE NOT NULL,
`fecha_contrato` DATE NOT NULL,
`titulacion` VARCHAR(140) NULL,
`nombre_depto` VARCHAR(45) NOT NULL,
PRIMARY KEY (`dni`),
INDEX `nombre_depto_idx` (`nombre_depto` ASC),
CONSTRAINT `nombre_depto`
FOREIGN KEY (`nombre_depto`)
REFERENCES `BDA`.`Departamento` (`nombre_depto`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`Capitulo_Contable`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Capitulo_Contable` (
`anho_contable` INT NOT NULL,
`numero_ug` INT NOT NULL,
`debe` DOUBLE NOT NULL,
`haber` DOUBLE NOT NULL,
PRIMARY KEY (`anho_contable`),
INDEX `numero_ug_idx` (`numero_ug` ASC),
CONSTRAINT `numero_ug`
FOREIGN KEY (`numero_ug`)
REFERENCES `BDA`.`UG` (`numero_ug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`UG`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`UG` (
`numero_ug` INT NOT NULL,
`nombre` VARCHAR(45) NOT NULL,
`segunda_firma_autorizada` VARCHAR(45) NOT NULL,
`fecha_creacion` DATE NOT NULL,
`nombre_depto` VARCHAR(140) NOT NULL,
`dni` INT NOT NULL,
`anho_contable` INT NOT NULL,
PRIMARY KEY (`numero_ug`),
INDEX `nombre_depto_idx` (`nombre_depto` ASC),
INDEX `dni_idx` (`dni` ASC),
INDEX `anho_contable_idx` (`anho_contable` ASC),
CONSTRAINT `nombre_depto`
FOREIGN KEY (`nombre_depto`)
REFERENCES `BDA`.`Departamento` (`nombre_depto`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `dni`
FOREIGN KEY (`dni`)
REFERENCES `BDA`.`Trabajador` (`dni`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `anho_contable`
FOREIGN KEY (`anho_contable`)
REFERENCES `BDA`.`Capitulo_Contable` (`anho_contable`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`Cliente`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Cliente` (
`cif_cliente` INT NOT NULL,
`nombre_cliente` VARCHAR(140) NOT NULL,
PRIMARY KEY (`cif_cliente`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`Ingreso`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Ingreso` (
`id` INT NOT NULL,
`concepto` VARCHAR(45) NOT NULL,
`importe` DOUBLE NOT NULL,
`fecha` DATE NOT NULL,
`cif_cliente` INT NOT NULL,
`numero_ug` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `cif_cliente_idx` (`cif_cliente` ASC),
INDEX `numero_ug_idx` (`numero_ug` ASC),
CONSTRAINT `cif_cliente`
FOREIGN KEY (`cif_cliente`)
REFERENCES `BDA`.`Cliente` (`cif_cliente`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `numero_ug`
FOREIGN KEY (`numero_ug`)
REFERENCES `BDA`.`UG` (`numero_ug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`Proveedor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Proveedor` (
`cif_proveedor` INT NOT NULL,
`nombre_proveedor` VARCHAR(140) NOT NULL,
PRIMARY KEY (`cif_proveedor`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`Gasto`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Gasto` (
`id` INT NOT NULL,
`concepto` VARCHAR(45) NOT NULL,
`importe` DOUBLE NOT NULL,
`fecha` DATE NOT NULL,
`factura` INT NOT NULL,
`cif_proveedor` INT NOT NULL,
`numero_ug` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `cif_proveedor_idx` (`cif_proveedor` ASC),
INDEX `numero_ug_idx` (`numero_ug` ASC),
CONSTRAINT `cif_proveedor`
FOREIGN KEY (`cif_proveedor`)
REFERENCES `BDA`.`Proveedor` (`cif_proveedor`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `numero_ug`
FOREIGN KEY (`numero_ug`)
REFERENCES `BDA`.`UG` (`numero_ug`)
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;
Thanks for your advices.
As Kevin Horgan said, the problem was foreign keys, which can not be repeated.
Thank you.