I have to create a double foreign key in the "Sceglie" table, but MySQL shows me always the same error: "#1215 - Cannot add foreign key constraint ". It only happens with "Cognome_candidato" and not with "Nome_candidato".I've also tried to write FOREIGN KEY (Nome_candidato, Cognome_candidato) REFERENCES Candidato(Nome_candidato, Cognome_candidato) ON DELETE CASCADE ON UPDATE CASCADE, but it didn't create the double foreign key. How can i solve it?
create table Candidato (
Nome_candidato varchar(255),
Cognome_candidato varchar(255),
PRIMARY KEY (Nome_candidato, Cognome_candidato));
create table Sceglie (
Email varchar(255),
Nome_candidato varchar(255),
Cognome_candidato varchar(255),
PRIMARY KEY (Email, Nome_candidato, Cognome_candidato),
FOREIGN KEY (Email) REFERENCES Utente(Email)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (Nome_candidato) REFERENCES Candidato(Nome_candidato)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (Cognome_candidato) REFERENCES Candidato(Cognome_candidato)
ON DELETE CASCADE
ON UPDATE CASCADE );
You need an index for cognome_candidato, before a foreign key can reference that column.
This should solve it.
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Candidato`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Candidato` (
`Nome_candidato` VARCHAR(255) NOT NULL,
`Cognome_candidato` VARCHAR(255) NOT NULL,
PRIMARY KEY (`Nome_candidato`, `Cognome_candidato`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Utente`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Utente` (
`Email` VARCHAR(255) NOT NULL,
PRIMARY KEY (`Email`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Sceglie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Sceglie` (
`Nome_candidato` VARCHAR(255) NOT NULL,
`Cognome_candidato` VARCHAR(255) NOT NULL,
`Email` VARCHAR(255) NOT NULL,
PRIMARY KEY (`Nome_candidato`, `Cognome_candidato`, `Email`),
INDEX `fk_Candidato_has_Utente_Utente1_idx` (`Email` ASC),
INDEX `fk_Candidato_has_Utente_Candidato_idx` (`Nome_candidato` ASC, `Cognome_candidato` ASC),
CONSTRAINT `fk_Candidato_has_Utente_Candidato`
FOREIGN KEY (`Nome_candidato` , `Cognome_candidato`)
REFERENCES `mydb`.`Candidato` (`Nome_candidato` , `Cognome_candidato`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_Candidato_has_Utente_Utente1`
FOREIGN KEY (`Email`)
REFERENCES `mydb`.`Utente` (`Email`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
Related
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.
I am using the MySQL workbench to create a database structure using the EER diagram. When it was complete I used the forward engineering function to create a sql file of my model:EER Diagram
When I try to deploy this query on MySQL server it keeps giving me mysql ERROR: Error 1215: Cannot add foreign key constraint.
Can somebody help me?
-- 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 mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`TblPersoon`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblPersoon` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TblPersoon` (
`PersoonID` INT NOT NULL,
`GSM` VARCHAR(45) NULL,
`Functie` VARCHAR(45) NULL,
`Voornaam` VARCHAR(45) NULL,
`Achternaam` VARCHAR(45) NULL,
PRIMARY KEY (`PersoonID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`TblDevice`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblDevice` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TblDevice` (
`DeviceID` INT NOT NULL,
`Van` VARCHAR(45) NULL,
`Tot` VARCHAR(45) NULL,
PRIMARY KEY (`DeviceID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`TblEvents`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblEvents` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TblEvents` (
`EventID` INT NOT NULL AUTO_INCREMENT,
`Naam` VARCHAR(45) NULL,
`Locatie` VARCHAR(45) NULL,
`ContactpersoonID` INT NOT NULL,
`VerantwoordelijkeID` INT NOT NULL,
`EventNaam` VARCHAR(45) NULL,
PRIMARY KEY (`EventID`),
INDEX `PersoonID_idx` (`ContactpersoonID` ASC),
INDEX `PersoonID_idx1` (`VerantwoordelijkeID` ASC),
CONSTRAINT `PersoonID`
FOREIGN KEY (`ContactpersoonID`)
REFERENCES `mydb`.`TblPersoon` (`PersoonID`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `PersoonID`
FOREIGN KEY (`VerantwoordelijkeID`)
REFERENCES `mydb`.`TblPersoon` (`PersoonID`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`TblContainer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblContainer` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TblContainer` (
`ContainerID` INT NULL,
`Plaats` INT NULL,
PRIMARY KEY (`ContainerID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`TblInstallatie`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblInstallatie` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TblInstallatie` (
`InstallatieID` INT NULL,
`ContainerID` INT NULL,
`DeviceID` INT NULL,
`Van` VARCHAR(45) NULL,
`Tot` VARCHAR(45) NULL,
`EventID` INT NULL,
`Omschrijving` VARCHAR(45) NULL,
`VerantwoordelijkeID` INT NULL,
PRIMARY KEY (`InstallatieID`),
INDEX `PersoonID_idx` (`VerantwoordelijkeID` ASC),
INDEX `DeviceID_idx` (`DeviceID` ASC),
INDEX `EventID_idx` (`EventID` ASC),
INDEX `ContainerID_idx` (`ContainerID` ASC),
CONSTRAINT `PersoonID`
FOREIGN KEY (`VerantwoordelijkeID`)
REFERENCES `mydb`.`TblPersoon` (`PersoonID`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `DeviceID`
FOREIGN KEY (`DeviceID`)
REFERENCES `mydb`.`TblDevice` (`DeviceID`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `EventID`
FOREIGN KEY (`EventID`)
REFERENCES `mydb`.`TblEvents` (`EventID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ContainerID`
FOREIGN KEY (`ContainerID`)
REFERENCES `mydb`.`TblContainer` (`ContainerID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`TblLog`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblLog` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TblLog` (
`LogID` INT NOT NULL,
`InstallatieID` INT NULL,
PRIMARY KEY (`LogID`),
INDEX `InstallatieID_idx` (`InstallatieID` ASC),
CONSTRAINT `InstallatieID`
FOREIGN KEY (`InstallatieID`)
REFERENCES `mydb`.`TblInstallatie` (`InstallatieID`)
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 noticed both foreign key constraints in TblEvents have the same name, PersoonID. Constraint names must be unique, and in fact they must be unique within the whole schema, not only within the table in which they are defined.
Also TblInstallatie has its own constraint named PersoonID. So if you just change one of the constraint names in TblEvents, it still results in an error when you get to CREATE TABLE TblInstallatie.
I tested this with MySQL 5.6 and I got it to work by giving distinct names to both constraints in TblEvents.
CREATE TABLE IF NOT EXISTS `mydb`.`TblEvents` (
...
CONSTRAINT `PersoonID1`
FOREIGN KEY (`ContactpersoonID`)
...
CONSTRAINT `PersoonID2`
FOREIGN KEY (`VerantwoordelijkeID`)
...
The later creation of TblInstallatie's CONSTRAINT PersoonID can then succeed.
I am trying to add data to 3 tables. The table "Gebruiker" is the parent table.
Wen i use the following code it gives me the follow error:
My SQL code:
SELECT *
FROM Gebruiker
INNER JOIN Inlog
ON Gebruiker.idGebruiker=Inlog.Gebruiker_idGebruiker
INNER JOIN GGevens
ON Gebruiker.idGebruiker=GGevens.Gebruiker_idGebruiker
INNER JOIN Domein
ON Gebruiker.Domein_idDomeint=Domein.idDomeint;
BEGIN;
INSERT INTO Gebruiker (Domein_idDomeint,idGebruiker)
VALUES (1,NULL);
INSERT INTO Inlog (Gebruiker_idGebruiker,UserName,UserPass)
VALUES (LAST_INSERT_ID(),'profile','drie');
INSERT INTO GGevens (Gebruiker_idGebruiker,Email,Voornaam,Tussenvoeg,Achternaam,Geslacht,Opleiding,GebDatum)
VALUES (LAST_INSERT_ID(),'aapje#peer.nl','Aapje','van','Drie',1,'Zeerslim','2014-11-11');
COMMIT;
Error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`zolstm001`.`GGevens`, CONSTRAINT `fk_GGevens_Gebruiker1` FOREIGN KEY (`Gebruiker_idGebruiker`) REFERENCES `Gebruiker` (`idGebruiker`) ON DELETE NO ACTION ON UPDATE NO ACTION)
My database structure:
http://i.imgur.com/bxeQbhQ.png
I think its because the table "GGevens" needs a foreign key from the table "Gebruiker"
Does someone know how i can add this?
(LAST_INSERT_ID() does not work...
SQL code to create the tables:
-- 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 zolstm001
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema zolstm001
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `zolstm001` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `zolstm001` ;
-- -----------------------------------------------------
-- Table `zolstm001`.`Domein`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Domein` (
`idDomeint` INT NOT NULL AUTO_INCREMENT,
`Naam` VARCHAR(45) NOT NULL,
`OverigeGegevens` VARCHAR(45) NULL,
PRIMARY KEY (`idDomeint`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`Gebruiker`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Gebruiker` (
`idGebruiker` INT NOT NULL AUTO_INCREMENT,
`Domein_idDomeint` INT NOT NULL,
PRIMARY KEY (`idGebruiker`),
INDEX `fk_Gebruiker_Domein1_idx` (`Domein_idDomeint` ASC),
CONSTRAINT `fk_Gebruiker_Domein1`
FOREIGN KEY (`Domein_idDomeint`)
REFERENCES `zolstm001`.`Domein` (`idDomeint`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`GGevens`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`GGevens` (
`idGGevens` INT NOT NULL AUTO_INCREMENT,
`Gebruiker_idGebruiker` INT NOT NULL,
`Email` VARCHAR(45) NOT NULL,
`GebDatum` DATE NOT NULL,
PRIMARY KEY (`idGGevens`),
INDEX `fk_GGevens_Gebruiker1_idx` (`Gebruiker_idGebruiker` ASC),
CONSTRAINT `fk_GGevens_Gebruiker1`
FOREIGN KEY (`Gebruiker_idGebruiker`)
REFERENCES `zolstm001`.`Gebruiker` (`idGebruiker`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`Inlog`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Inlog` (
`idInlog` INT NOT NULL AUTO_INCREMENT,
`UserName` VARCHAR(45) NOT NULL,
`UserPass` VARCHAR(45) NOT NULL,
`Gebruiker_idGebruiker` INT NOT NULL,
PRIMARY KEY (`idInlog`),
INDEX `fk_Inlog_Gebruiker1_idx` (`Gebruiker_idGebruiker` ASC),
CONSTRAINT `fk_Inlog_Gebruiker1`
FOREIGN KEY (`Gebruiker_idGebruiker`)
REFERENCES `zolstm001`.`Gebruiker` (`idGebruiker`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`Startup`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Startup` (
`idStartup` INT NOT NULL AUTO_INCREMENT,
`Naam` VARCHAR(45) NOT NULL,
`Gebruiker_idGebruiker` INT NOT NULL,
PRIMARY KEY (`idStartup`),
INDEX `fk_Startup_Gebruiker1_idx` (`Gebruiker_idGebruiker` ASC),
CONSTRAINT `fk_Startup_Gebruiker1`
FOREIGN KEY (`Gebruiker_idGebruiker`)
REFERENCES `zolstm001`.`Gebruiker` (`idGebruiker`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`Reactie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Reactie` (
`idReactie` INT NOT NULL AUTO_INCREMENT,
`Gebruiker_idGebruiker` INT NOT NULL,
`Bericht` VARCHAR(45) NOT NULL,
`Startup_idStartup` INT NOT NULL,
`Reactiecol` VARCHAR(45) NULL,
PRIMARY KEY (`idReactie`),
INDEX `fk_Reactie_Gebruiker1_idx` (`Gebruiker_idGebruiker` ASC),
INDEX `fk_Reactie_Startup1_idx` (`Startup_idStartup` ASC),
CONSTRAINT `fk_Reactie_Gebruiker1`
FOREIGN KEY (`Gebruiker_idGebruiker`)
REFERENCES `zolstm001`.`Gebruiker` (`idGebruiker`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Reactie_Startup1`
FOREIGN KEY (`Startup_idStartup`)
REFERENCES `zolstm001`.`Startup` (`idStartup`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`Category`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Category` (
`idCategory` INT NOT NULL AUTO_INCREMENT,
`Startup_idStartup` INT NOT NULL,
PRIMARY KEY (`idCategory`),
INDEX `fk_Category_Startup1_idx` (`Startup_idStartup` ASC),
CONSTRAINT `fk_Category_Startup1`
FOREIGN KEY (`Startup_idStartup`)
REFERENCES `zolstm001`.`Startup` (`idStartup`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`Unit`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Unit` (
`idUnit` INT NOT NULL AUTO_INCREMENT,
`Startup_idStartup` INT NOT NULL,
PRIMARY KEY (`idUnit`),
INDEX `fk_Unit_Startup1_idx` (`Startup_idStartup` ASC),
CONSTRAINT `fk_Unit_Startup1`
FOREIGN KEY (`Startup_idStartup`)
REFERENCES `zolstm001`.`Startup` (`idStartup`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`SGegevens`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`SGegevens` (
`idSGegevens` INT NOT NULL AUTO_INCREMENT,
`Startup_idStartup` INT NOT NULL,
PRIMARY KEY (`idSGegevens`),
INDEX `fk_SGegevens_Startup1_idx` (`Startup_idStartup` ASC),
CONSTRAINT `fk_SGegevens_Startup1`
FOREIGN KEY (`Startup_idStartup`)
REFERENCES `zolstm001`.`Startup` (`idStartup`)
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 would change the INSERT INTO gebruiker to
INSERT INTO Gebruiker (Domein_idDomeint) VALUES (1);
Furthermore to get this working, you will need to use some programming code to help you. This can be done in the shape of a stored procedure.
CREATE PROCEDURE gebruikerProc (IN {your_params})
BEGIN
DECLARE lastId INT;
INSERT INTO Gebruiker (Domein_idDomeint) VALUES (1);
SET lastId=LAST_INSERT_ID();
INSERT INTO Inlog (Gebruiker_idGebruiker,UserName,UserPass)
VALUES (lastId,'profile','drie');
INSERT INTO GGevens (Gebruiker_idGebruiker,Email,Voornaam,Tussenvoeg,Achternaam,Geslacht,Opleiding,GebDatum)
VALUES (lastId,'aapje#peer.nl','Aapje','van','Drie',1,'Zeerslim','2014-11-11');
END;
One warning: Do not use TAB to indent your code in a stored procedure/function/trigger etc in MySQL. You will get an error. It just can not handle TAB.
This is the table I get "Cannot add foreign key constraint" error.
-- -----------------------------------------------------
-- Table `mydb`.`Supervise1`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Supervise1` (
`S1_Date` VARCHAR(45) NOT NULL,
`S1_Contracter` VARCHAR(45) NOT NULL,
`S1_Contractee` VARCHAR(45) NOT NULL,
`S1_ID` VARCHAR(45) NOT NULL,
PRIMARY KEY (`S1_Contracter`, `S1_Contractee`, `S1_Date`, `S1_ID`),
INDEX `Contracter_idx` (`S1_Contracter` ASC),
INDEX `Contractee_idx` (`S1_Contractee` ASC),
INDEX `S1_ID_idx` (`S1_ID` ASC),
CONSTRAINT `S1_Date`
FOREIGN KEY (`S1_Date`)
REFERENCES `mydb`.`Contract` (`Date`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `S1_Contracter`
FOREIGN KEY (`S1_Contracter`)
REFERENCES `mydb`.`Contract` (`Contracter`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `S1_Contractee`
FOREIGN KEY (`S1_Contractee`)
REFERENCES `mydb`.`Contract` (`Contractee`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `S1_ID`
FOREIGN KEY (`S1_ID`)
REFERENCES `mydb`.`Lawfirm` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Contract and Lawfirm table
-- -----------------------------------------------------
-- Table `mydb`.`Contract`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Contract` (
`Date` VARCHAR(45) NOT NULL,
`Contracter` VARCHAR(45) NOT NULL,
`Contractee` VARCHAR(45) NOT NULL,
INDEX `Contracter_idx` (`Contracter` ASC),
INDEX `Contractee_idx` (`Contractee` ASC),
PRIMARY KEY (`Contracter`, `Contractee`, `Date`),
CONSTRAINT `Contracter`
FOREIGN KEY (`Contracter`)
REFERENCES `mydb`.`Agency` (`AgencyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Contractee`
FOREIGN KEY (`Contractee`)
REFERENCES `mydb`.`Agency` (`AgencyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Lawfirm`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Lawfirm` (
`ID` VARCHAR(45) NOT NULL,
PRIMARY KEY (`ID`))
ENGINE = InnoDB;
I looked through some answers for similar problem but mine columns are same type. I don't understand why it gives me error. I can provide more information if needed.
Solved: Check the sql if there is any constraint with the same name. Even it says foreign key error was about constraint names.
Using InnoDB = Yes
Spelling = used the relationship referencing of workbench(more or less should be correct)
Primary key = Yes
I dont really know how to solve this one...I wanted PEO-MVAlign's SCHOOLID to be referenced to MVALIGN and PEO's SchoolID...but one only one SCHOOL ID should be seen or exist in the PEO-MVALIGN table..
Error Message
Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
CREATE TABLE IF NOT EXISTS `mydb`.`PEO-MVAlign` (
`SchoolID` DECIMAL(5) NOT NULL,
`values` CHAR(25) NOT NULL,
`DegreeProgram` VARCHAR(50) NOT NULL,
`PEOCode` CHAR(4) NOT NULL,
PRIMARY KEY (`SchoolID`, `values`, `DegreeProgram`, `PEOCode`),
INDEX `fk_PEO-MVAlign_MVAlign1_idx` (`SchoolID` ASC, `values` ASC),
INDEX `fk_PEO-MVAlign_PEO1_idx` (`SchoolID` ASC, `DegreeProgram` ASC, `PEOCode` ASC),
CONSTRAINT `fk_PEO-MVAlign_MVAlign1`
FOREIGN KEY (`SchoolID` , `values`)
REFERENCES `mydb`.`MVAlign` (`SchoolID` , `values`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_PEO-MVAlign_PEO1`
FOREIGN KEY (`DegreeProgram` , `PEOCode` , `SchoolID`)
REFERENCES `mydb`.`PEO` (`DegreeProgram` , `PEOCode` , `SchoolID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 23 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Codes
-- 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 mydb
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`REF-School`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`REF-School` ;
CREATE TABLE IF NOT EXISTS `mydb`.`REF-School` (
`SchoolID` DECIMAL(5) NOT NULL,
`SchoolName` VARCHAR(50) NOT NULL,
PRIMARY KEY (`SchoolID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`MVAlign`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`MVAlign` ;
CREATE TABLE IF NOT EXISTS `mydb`.`MVAlign` (
`SchoolID` DECIMAL(5) NOT NULL,
`values` CHAR(25) NOT NULL,
PRIMARY KEY (`SchoolID`, `values`),
INDEX `fk_MVAlign_REF-School` (`SchoolID` ASC),
CONSTRAINT `fk_MVAlign_REF-School`
FOREIGN KEY (`SchoolID`)
REFERENCES `mydb`.`REF-School` (`SchoolID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`REF-Degree`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`REF-Degree` ;
CREATE TABLE IF NOT EXISTS `mydb`.`REF-Degree` (
`SchoolID` DECIMAL(5) NOT NULL,
`DegreeProgram` VARCHAR(50) NOT NULL,
PRIMARY KEY (`SchoolID`, `DegreeProgram`),
INDEX `fk_REF-Degree_REF-School1` (`SchoolID` ASC),
CONSTRAINT `fk_REF-Degree_REF-School1`
FOREIGN KEY (`SchoolID`)
REFERENCES `mydb`.`REF-School` (`SchoolID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`REF-Courses`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`REF-Courses` ;
CREATE TABLE IF NOT EXISTS `mydb`.`REF-Courses` (
`SchoolID` DECIMAL(5) NOT NULL,
`DegreeProgram` VARCHAR(50) NOT NULL,
`CourseCode` VARCHAR(25) NOT NULL,
`Unit` DECIMAL(2,1) NOT NULL,
`CourseDescription` VARCHAR(200) NOT NULL,
PRIMARY KEY (`SchoolID`, `DegreeProgram`, `CourseCode`),
INDEX `FK_COURSEStoDEGREE` (`SchoolID` ASC, `DegreeProgram` ASC),
CONSTRAINT `FK_COURSEStoDegree`
FOREIGN KEY (`SchoolID` , `DegreeProgram`)
REFERENCES `mydb`.`REF-Degree` (`SchoolID` , `DegreeProgram`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`REF-Courses`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`REF-Courses` ;
CREATE TABLE IF NOT EXISTS `mydb`.`REF-Courses` (
`SchoolID` DECIMAL(5) NOT NULL,
`DegreeProgram` VARCHAR(50) NOT NULL,
`CourseCode` VARCHAR(25) NOT NULL,
`Unit` DECIMAL(2,1) NOT NULL,
`CourseDescription` VARCHAR(200) NOT NULL,
PRIMARY KEY (`SchoolID`, `DegreeProgram`, `CourseCode`),
INDEX `FK_COURSEStoDEGREE` (`SchoolID` ASC, `DegreeProgram` ASC),
CONSTRAINT `FK_COURSEStoDegree`
FOREIGN KEY (`SchoolID` , `DegreeProgram`)
REFERENCES `mydb`.`REF-Degree` (`SchoolID` , `DegreeProgram`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`IGOA`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`IGOA` ;
CREATE TABLE IF NOT EXISTS `mydb`.`IGOA` (
`SchoolID` DECIMAL(5) NOT NULL,
`IGOCode` CHAR(25) NOT NULL,
`IGA` CHAR(50) NOT NULL,
`GradOutcome` VARCHAR(500) NOT NULL,
PRIMARY KEY (`SchoolID`, `IGOCode`),
INDEX `FK_IGOAtoSchool` (`SchoolID` ASC),
CONSTRAINT `FK_IGOAtoSchool`
FOREIGN KEY (`SchoolID`)
REFERENCES `mydb`.`REF-School` (`SchoolID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`IGO-MVAlign`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`IGO-MVAlign` ;
CREATE TABLE IF NOT EXISTS `mydb`.`IGO-MVAlign` (
`SchoolID` DECIMAL(5) NOT NULL,
`values` CHAR(25) NOT NULL,
`IGOCode` CHAR(25) NOT NULL,
PRIMARY KEY (`SchoolID`, `values`, `IGOCode`),
INDEX `fk_IGO-MVAlign_IGOA1_idx` (`SchoolID` ASC, `IGOCode` ASC),
INDEX `fk_IGO-MVAlign_MVAlign1_idx` (`SchoolID` ASC, `values` ASC),
CONSTRAINT `fk_IGO-MVAlign_IGOA1`
FOREIGN KEY (`SchoolID` , `IGOCode`)
REFERENCES `mydb`.`IGOA` (`SchoolID` , `IGOCode`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_IGO-MVAlign_MVAlign1`
FOREIGN KEY (`SchoolID` , `values`)
REFERENCES `mydb`.`MVAlign` (`SchoolID` , `values`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`PEO`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`PEO` ;
CREATE TABLE IF NOT EXISTS `mydb`.`PEO` (
`SchoolID` DECIMAL(5) NOT NULL,
`DegreeProgram` VARCHAR(50) NOT NULL,
`PEOCode` CHAR(4) NOT NULL,
`EducObj` VARCHAR(500) NOT NULL,
PRIMARY KEY (`SchoolID`, `DegreeProgram`, `PEOCode`),
INDEX `FK_PEOtoREF-DEGREE` (`SchoolID` ASC, `DegreeProgram` ASC),
CONSTRAINT `FK_PEOtoREF-DEGREE`
FOREIGN KEY (`SchoolID` , `DegreeProgram`)
REFERENCES `mydb`.`REF-Degree` (`SchoolID` , `DegreeProgram`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`PEO-MVAlign`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`PEO-MVAlign` ;
CREATE TABLE IF NOT EXISTS `mydb`.`PEO-MVAlign` (
`SchoolID` DECIMAL(5) NOT NULL,
`values` CHAR(25) NOT NULL,
`DegreeProgram` VARCHAR(50) NOT NULL,
`PEOCode` CHAR(4) NOT NULL,
PRIMARY KEY (`SchoolID`, `values`, `DegreeProgram`, `PEOCode`),
INDEX `fk_PEO-MVAlign_MVAlign1_idx` (`SchoolID` ASC, `values` ASC),
INDEX `fk_PEO-MVAlign_PEO1_idx` (`SchoolID` ASC, `DegreeProgram` ASC, `PEOCode` ASC),
CONSTRAINT `fk_PEO-MVAlign_MVAlign1`
FOREIGN KEY (`SchoolID` , `values`)
REFERENCES `mydb`.`MVAlign` (`SchoolID` , `values`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_PEO-MVAlign_PEO1`
FOREIGN KEY (`DegreeProgram` , `PEOCode` , `SchoolID`)
REFERENCES `mydb`.`PEO` (`DegreeProgram` , `PEOCode` , `SchoolID`)
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;
-- -----------------------------------------------------
An SQL FOREIGN KEY in one table REFERENCES a corresponding column list in another table. The column correspondence between the FK and the referenced table is not by name but by order of column in the lists.
The referenced column list must be declared UNIQUE NOT NULL or PRIMARY KEY in the referenced table. The types of corresponding columns must have certain agreement (including integer to integer and string to string). Also in MySQL the FK columns must have an index with the columns as its first columns in the same order. Under MySQL with InnoDB the referenced columns must have an index in their table with the columns as its first columns in the same order. (Some indexes can be implicit, eg for PRIMARY KEY or FOREIGN KEY.)
For the PEO-MVAlign PEO FK the referenced columns are not in the same order as the referencing columns. This pairs up differently named columns in PEO-MVAlign and PEO. Which is probably not you want; probably you want the referencing and referenced column lists to be in the same order. But (none of) the types of corresponding columns match, so the correspondence is not allowed. But even if they were allowed, the referenced column list doesn't have the same names in the same order as an index in PEO-MVAlign or in PEO.