Cannot add foreign key constraint, workbench - mysql

I used workbench to implement a database schema, but I'm getting this error when using foreign keys in a certain table.
1215 - Cannot add foreign key constraint
SQL query:
CREATE TABLE IF NOT EXISTS `Gam3ty`.`Frequently_Used_Location` (
`idFrequently_Used_Location` INT NOT NULL,
`User_idUser` INT NOT NULL,
`User_College_idCollege` INT NOT NULL,
PRIMARY KEY (`idFrequently_Used_Location`,`User_idUser`,`User_College_idCollege`),
INDEX `fk_Frequently_Used_Location_User1_idx` (`User_idUser` ASC,`User_College_idCollege` ASC),
CONSTRAINT `fk_Frequently_Used_Location_User1`
FOREIGN KEY (`User_idUser` , `User_College_idCollege`)
REFERENCES `Gam3ty`.`User` (`idUser` , `College_idCollege`)
my SQL:
CREATE SCHEMA IF NOT EXISTS `Gam3ty` DEFAULT CHARACTER SET utf8 ;
USE `Gam3ty` ;
-- -----------------------------------------------------
-- Table `Gam3ty`.`Frequently_Used_Location`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Gam3ty`.`Frequently_Used_Location` (
`idFrequently_Used_Location` INT NOT NULL,
`User_idUser` INT NOT NULL,
`User_College_idCollege` INT NOT NULL,
PRIMARY KEY (`idFrequently_Used_Location`, `User_idUser`, `User_College_idCollege`),
INDEX `fk_Frequently_Used_Location_User1_idx` (`User_idUser` ASC, `User_College_idCollege` ASC),
CONSTRAINT `fk_Frequently_Used_Location_User1`
FOREIGN KEY (`User_idUser` , `User_College_idCollege`)
REFERENCES `Gam3ty`.`User` (`idUser` , `College_idCollege`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `Gam3ty`.`Location`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Gam3ty`.`Location` (
`idLocation` INT NOT NULL,
`Frequently_Used_Location_idFrequently_Used_Location` INT NOT NULL,
`Frequently_Used_Location_User_idUser` INT NOT NULL,
`Frequently_Used_Location_User_College_idCollege` INT NOT NULL,
`type` VARCHAR(45) NULL,
PRIMARY KEY (`idLocation`),
INDEX `fk_Location_Frequently_Used_Location1_idx` (`Frequently_Used_Location_idFrequently_Used_Location` ASC, `Frequently_Used_Location_User_idUser` ASC, `Frequently_Used_Location_User_College_idCollege` ASC),
CONSTRAINT `fk_Location_Frequently_Used_Location1`
FOREIGN KEY (`Frequently_Used_Location_idFrequently_Used_Location` , `Frequently_Used_Location_User_idUser` , `Frequently_Used_Location_User_College_idCollege`)
REFERENCES `Gam3ty`.`Frequently_Used_Location` (`idFrequently_Used_Location`, `User_idUser` , `User_College_idCollege`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `Gam3ty`.`University`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Gam3ty`.`University` (
`idUniversity` INT NOT NULL,
`Location_idLocation` INT NOT NULL,
`Info` VARCHAR(45) NULL,
PRIMARY KEY (`idUniversity`, `Location_idLocation`),
INDEX `fk_University_Location1_idx` (`Location_idLocation` ASC),
CONSTRAINT `fk_University_Location1`
FOREIGN KEY (`Location_idLocation`)
REFERENCES `Gam3ty`.`Location` (`idLocation`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `Gam3ty`.`College`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Gam3ty`.`College` (
`idCollege` INT NOT NULL,
`University_idUniversity` INT NOT NULL,
`Location_idLocation` INT NOT NULL,
`Info` VARCHAR(45) NULL,
`Staff` VARCHAR(45) NULL,
`Department` VARCHAR(45) NULL,
PRIMARY KEY (`idCollege`, `University_idUniversity`, `Location_idLocation`),
INDEX `fk_College_University1_idx` (`University_idUniversity` ASC),
INDEX `fk_College_Location1_idx` (`Location_idLocation` ASC),
CONSTRAINT `fk_College_University1`
FOREIGN KEY (`University_idUniversity`)
REFERENCES `Gam3ty`.`University` (`idUniversity`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_College_Location1`
FOREIGN KEY (`Location_idLocation`)
REFERENCES `Gam3ty`.`Location` (`idLocation`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `Gam3ty`.`User`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Gam3ty`.`User` (
`idUser` INT NOT NULL,
`College_idCollege` INT NOT NULL,
`UserName` VARCHAR(45) NOT NULL,
`Password` VARCHAR(45) NOT NULL,
`E-mail` VARCHAR(45) NOT NULL,
`Social_media_accounts` VARCHAR(45) NULL,
`Gender` VARCHAR(45) NOT NULL,
`Job` VARCHAR(45) NOT NULL,
`Tel-num` BIGINT(11) NULL,
`Adress` VARCHAR(45) NULL,
PRIMARY KEY (`idUser`, `College_idCollege`, `UserName`),
INDEX `fk_User_College_idx` (`College_idCollege` ASC),
CONSTRAINT `fk_User_College`
FOREIGN KEY (`College_idCollege`)
REFERENCES `Gam3ty`.`College` (`idCollege`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I checked the datatypes and they are all the same, also every foreign key is a primary key in it's original table.

Tables are created in order from top to bottom when you run a SQL script.
You can't create a foreign key that references a table that hasn't been created yet.
You must order the tables so that each table is created before any tables that reference it.
#Rahul wrote:
You need to refer all of the columns designated as primary key.
I agree this is a recommended practice, because otherwise you can create a foreign key where a given row references multiple rows in the parent table. This leads to ambiguous semantics. For instance, can you delete a row in the parent table if there's a row referencing it in the child table, but there's a second row in the parent table that satisfies the reference? This breaks the definition of referential integrity in standard SQL.
Nevertheless, InnoDB allows it. You can make a foreign key that references any left-most subset of columns of any key (unique or non-unique). It's a very bad idea, but InnoDB lets you do it and does not throw an error.
The following is crazy, but it's not an error to InnoDB:
create table foo (a int, b int, key (a, b));
create table bar (a int, foreign key (a) references foo(a));

That's cause table Gam3ty.User defines primary key on 3 columns as seen below but you are referencing only two of them. which creates partial functional dependency. You need to refer all of the columns designated as primary key
CREATE TABLE IF NOT EXISTS `Gam3ty`.`User` (
....
PRIMARY KEY (`idUser`, `College_idCollege`, `UserName`)
Your referencing table
FOREIGN KEY (`User_idUser` , `User_College_idCollege`)
REFERENCES `Gam3ty`.`User` (`idUser` , `College_idCollege`)

Related

ER_FK_NO_INDEX_PARENT: Failed to add the foreign key constaint

Hey so I'm on my first project with a complexe database :)
I have created all of my tables and I am now updating them to add the Foreign Keys (I had an issue so I decided to add Foreign Keys after creating all the tables).
Here the ERR Diagram that describe the project : click to open
The exact error i get : ER_FK_NO_INDEX_PARENT: Failed to add the foreign key constaint. Missing index for constraint 'fk_user_in_org__org_roles1' in the referenced table 'org_roles'
I've searched my error but i didn't found a solution to my problem here some of the best I found :
Link 1
Link 2
The tables concerned (simplified) and the update command :
------------------
-- Tables setup --
------------------
-- A role is unique for an org but differents orgs can have a role that have the same label
CREATE TABLE IF NOT EXISTS students.org_roles (
`org_id` INT NOT NULL,
`label` VARCHAR(32) NOT NULL,
PRIMARY KEY (`org_id`, `label`))
ENGINE = InnoDB;
-- The users are unique
CREATE TABLE IF NOT EXISTS students.users (
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
UNIQUE INDEX id_UNIQUE (`id` ASC));
-- The orgs are unique
CREATE TABLE IF NOT EXISTS students.organizations (
`id` INT NOT NULL AUTO_INCREMENT,
`name` TEXT(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX id_UNIQUE (`id` ASC))
ENGINE = InnoDB;
-- The table that match a user in an org with a specified role (= label in the org_role table)
CREATE TABLE IF NOT EXISTS students.user_in_org (
`user_id` INT NOT NULL,
`org_id` INT NOT NULL,
`role` TEXT(32) NOT NULL,
PRIMARY KEY (`user_id`, `org_id`),
UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC));
------------------------
-- The update command --
------------------------
ALTER TABLE students.user_in_org ADD
(CONSTRAINT fk_user_in_org__users1
FOREIGN KEY (`user_id`) REFERENCES students.users(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_user_in_org__organizations1
FOREIGN KEY (`org_id`) REFERENCES students.organizations(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_user_in_org__org_roles1
FOREIGN KEY (`role`) REFERENCES students.org_roles(`label`)
ON DELETE RESTRICT
ON UPDATE CASCADE);
Thank you in advance for your help :)
And please forgive me if my english isn't that fluent :/
This can be because the table students.org_roles doesn't have Unique index in the field org_id, it is what error says
For
CONSTRAINT fk_user_in_org__org_roles1
FOREIGN KEY (`role`) REFERENCES students.org_roles(`label`)
You need a index on the table org_roles for the column label
CREATE TABLE IF NOT EXISTS students.org_roles (
`org_id` INT NOT NULL,
`label` VARCHAR(32) NOT NULL,
KEy (`label`),
PRIMARY KEY (`org_id`, `label`))
ENGINE = InnoDB;
In your origi8nal design you have a combined primary key so you can only make a reference to the combined primary key or define a new one for label
CREATE TABLE IF NOT EXISTS org_roles (
`org_id` INT NOT NULL,
`label` VARCHAR(32) NOT NULL,
PRIMARY KEY (`org_id`, `label`))
ENGINE = InnoDB;
-- The users are unique
CREATE TABLE IF NOT EXISTS users (
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
UNIQUE INDEX id_UNIQUE (`id` ASC));
-- The orgs are unique
CREATE TABLE IF NOT EXISTS organizations (
`id` INT NOT NULL AUTO_INCREMENT,
`name` TEXT(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX id_UNIQUE (`id` ASC))
ENGINE = InnoDB;
-- The table that match a user in an org with a specified role (= label in the org_role table)
CREATE TABLE IF NOT EXISTS user_in_org (
`user_id` INT NOT NULL,
`org_id` INT NOT NULL,
`role` TEXT(32) NOT NULL,
PRIMARY KEY (`user_id`, `org_id`),
UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC));
ALTER TABLE user_in_org ADD
(CONSTRAINT fk_user_in_org__users1
FOREIGN KEY (`user_id`) REFERENCES users(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_user_in_org__organizations1
FOREIGN KEY (`org_id`) REFERENCES organizations(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_user_in_org__org_roles1
FOREIGN KEY (`org_id`,`role`) REFERENCES org_roles(`org_id`,`label`)
ON DELETE RESTRICT
ON UPDATE CASCADE);
db<>fiddle here

How do I solve this forward engineer problem on MySQL?

I'm trying to forward engineer my ERD that I made, but the Forward Engineer Process gives me as output an error. ERROR: Error 3734: Failed to add the foreign key constraint. Missing column 'straatid' for constraint 'verzinzelf3' in the referenced table 'locatie'.
But that is very odd, because the columnname straatid is in fact in the table Locatie. The relation between table Team and Locatie is exactly the same as the relation between Locatie and Plant, but it seems that I only get an error between the relation of table Locatie and Plant. I can't really figure out how to solve this error. Can someone maybe help me with this problem?.
This is the review of the SQL script that is supposed to be executed:
-- MySQL Workbench Forward Engineering
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`table1`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`table1` (
`StraatID` VARCHAR(45) NOT NULL,
`Straat` VARCHAR(45) NULL,
`Latitude` VARCHAR(45) NULL,
`Longitude` VARCHAR(45) NULL,
PRIMARY KEY (`StraatID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Locatie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Locatie` (
`Huisnr` VARCHAR(45) NOT NULL,
`StraatID` VARCHAR(45) NOT NULL,
`Toevoeging` VARCHAR(45) NULL,
PRIMARY KEY (`Huisnr`, `StraatID`),
UNIQUE INDEX `StraatID_UNIQUE` (`StraatID` ASC) VISIBLE,
UNIQUE INDEX `Huisnr_UNIQUE` (`Huisnr` ASC) VISIBLE,
INDEX `verzinzelf2_idx` (`StraatID` ASC) VISIBLE,
CONSTRAINT `verzinzelf2`
FOREIGN KEY (`StraatID`)
REFERENCES `mydb`.`table1` (`StraatID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Team`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Team` (
`Teamnr` INT NOT NULL,
`StraatID` VARCHAR(45) NOT NULL,
`Huisnr` VARCHAR(45) NOT NULL,
`StraatID1` VARCHAR(45) NOT NULL,
PRIMARY KEY (`Teamnr`),
INDEX `verzinzelf5_idx` (`Huisnr` ASC, `StraatID1` ASC) VISIBLE,
CONSTRAINT `verzinzelf5`
FOREIGN KEY (`Huisnr` , `StraatID1`)
REFERENCES `mydb`.`Locatie` (`Huisnr` , `StraatID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Eigenaar`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Eigenaar` (
`Eigenaar` INT NOT NULL,
PRIMARY KEY (`Eigenaar`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Plant`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Plant` (
`Plant beschrijving` VARCHAR(45) NOT NULL,
`Kaartlokatie` VARCHAR(45) NOT NULL,
`Oppervlakte` VARCHAR(45) NOT NULL,
`Eigenaar` INT NOT NULL,
`Huisnr` VARCHAR(45) NOT NULL,
`StraatID` VARCHAR(45) NOT NULL,
PRIMARY KEY (`Plant beschrijving`, `Kaartlokatie`, `Oppervlakte`),
INDEX `verzinzelf4_idx` (`Eigenaar` ASC) VISIBLE,
INDEX `verzinzelf3_idx` (`Huisnr` ASC, `StraatID` ASC) VISIBLE,
CONSTRAINT `verzinzelf4`
FOREIGN KEY (`Eigenaar`)
REFERENCES `mydb`.`Eigenaar` (`Eigenaar`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `verzinzelf3`
FOREIGN KEY (`Huisnr` , `StraatID`)
REFERENCES `mydb`.`Locatie` (`Huisnr` , `StraatID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Meting`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Meting` (
`Teamnr` INT NOT NULL,
`Plant` VARCHAR(45) NOT NULL,
`Meetsoort` VARCHAR(45) NULL,
PRIMARY KEY (`Teamnr`, `Plant`),
INDEX `verzinzelf1_idx` (`Plant` ASC) VISIBLE,
INDEX `verzinzelf_idx` (`Teamnr` ASC) VISIBLE,
CONSTRAINT `verzinzelf`
FOREIGN KEY (`Teamnr`)
REFERENCES `mydb`.`Team` (`Teamnr`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `verzinzelf1`
FOREIGN KEY (`Plant`)
REFERENCES `mydb`.`Plant` (`Plant beschrijving`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
And the full error message:
Executing SQL script in server
ERROR: Error 3734: Failed to add the foreign key constraint. Missing column 'straatid' for constraint 'verzinzelf3' in the referenced table 'locatie'
SQL Code:
-- -----------------------------------------------------
-- Table `mydb`.`Plant`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Plant` (
`Plant beschrijving` VARCHAR(45) NOT NULL,
`Kaartlokatie` VARCHAR(45) NOT NULL,
`Oppervlakte` VARCHAR(45) NOT NULL,
`Eigenaar` INT NOT NULL,
`Huisnr` VARCHAR(45) NOT NULL,
`StraatID` VARCHAR(45) NOT NULL,
PRIMARY KEY (`Plant beschrijving`, `Kaartlokatie`, `Oppervlakte`),
INDEX `verzinzelf4_idx` (`Eigenaar` ASC) VISIBLE,
INDEX `verzinzelf3_idx` (`Huisnr` ASC, `StraatID` ASC) VISIBLE,
CONSTRAINT `verzinzelf4`
FOREIGN KEY (`Eigenaar`)
REFERENCES `mydb`.`Eigenaar` (`Eigenaar`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `verzinzelf3`
FOREIGN KEY (`Huisnr` , `StraatID`)
REFERENCES `mydb`.`Locatie` (`Huisnr` , `StraatID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 9 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch

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

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.

MySQL Foreign Key Constraint Error (1215)

I'm trying to run a SQL script to generate my database for a project through MySQL workbench but every time I attempt, I keep getting a 1215 error saying that it cannot add the foreign key constraint.
The error was in reference to the relationship below where I'm attempting to have the id of the faculty entity as a foreign key for a grad_program under the director_id field.
-- -----------------------------------------------------
-- Table `jupitercollege`.`faculty`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `jupitercollege`.`faculty` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`dept` VARCHAR(10) NOT NULL,
`email` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `jupitercollege`.`grad_program`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `jupitercollege`.`grad_program` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`dept` VARCHAR(10) NOT NULL,
`phone` VARCHAR(12) NULL DEFAULT 'UNKNOWN',
`email` VARCHAR(45) NULL DEFAULT 'UNKNOWN',
`director_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `director_id_idx` (`director_id` ASC),
UNIQUE INDEX `email_UNIQUE` (`email` ASC),
UNIQUE INDEX `name_UNIQUE` (`name` ASC),
CONSTRAINT `director_id`
FOREIGN KEY (`director_id`)
REFERENCES `jupitercollege`.`faculty` (`id`)
ON DELETE SET NULL
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I've seen a lot of posts about this error but none have really been directly applicable to my scenario, unless I misunderstood them. Any aid in rectifying the situation would be appreciated.
Your child table has ON DELETE SET NULL. MySql 5.6 documentation states
If you specify a SET NULL action, make sure that you have not declared
the columns in the child table as NOT NULL.
Your director_id column has a not null constraint. Remove that constraint or remove the ON DELETE clause.
https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

While creating database: Error Code: 1215. Cannot add foreign key constraint

When I am creating a database, I'm getting a little bit of a difficulty with creating the "main" table for this database.
I thought there was a type difference between the foreign key and the primary key, but there wasn't.
So can anyone help me out? There must be something that I'm overlooking, but I can't find the problem.
[code]
-- -----------------------------------------------------
-- Table `boat`.`terms_and_conditions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `boat`.`terms_and_conditions` (
`Terms_And_Conditions_Id` INT NOT NULL,
`Terms_And_Conditions_Doc` VARCHAR(255) NULL DEFAULT NULL,
`Terms_And_Conditions_Date` DATE NULL DEFAULT NULL,
PRIMARY KEY (`Terms_And_Conditions_Id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `boat`.`player`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `boat`.`player` (
`Player_Id` INT NOT NULL,
`Player_Firstname` VARCHAR(45) NULL DEFAULT NULL,
`Player_Lastname` VARCHAR(45) NULL DEFAULT NULL,
`Player_Password` VARCHAR(45) NULL,
`Player_Budget` DOUBLE NULL,
`Terms_Id` INT NOT NULL,
PRIMARY KEY (`Player_Id`),
INDEX `fk_player_terms_and_conditions1_idx` (`Terms_Id` ASC),
CONSTRAINT `fk_player_player_result1`
FOREIGN KEY (`Player_Id`)
REFERENCES `boat`.`player_result` (`Player_Id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_player_supervisor_player1`
FOREIGN KEY (`Player_Id`)
REFERENCES `boat`.`supervisor_player` (`Player_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_player_terms_and_conditions1`
FOREIGN KEY (`Terms_Id`)
REFERENCES `boat`.`terms_and_conditions` (`Terms_And_Conditions_Id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Problem was in the Primary Key in the Player Table. It was configured to act like a foreign key for the other tables.