I've literally no idea how to find the "via" field from 'sede' table starting from the "cognome" field in 'impiegato' table.
CREATE TABLE IF NOT EXISTS `mydb`.`impiegato` (
`codice` VARCHAR(45) NOT NULL,
`cognome` VARCHAR(45),
`nomedipart` VARCHAR(45),
`sede` VARCHAR(45),
`data` VARCHAR(45),
PRIMARY KEY (`codice`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`sede`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`sede` (
`cittaSede` VARCHAR(45) NOT NULL,
`via` VARCHAR(45) NULL,
`cap` VARCHAR(45) NULL,
PRIMARY KEY (`cittaSede`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`dipartimento`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`dipartimento` (
`nome` VARCHAR(45) NOT NULL,
`cittaSede` VARCHAR(45) NOT NULL,
`Telefono` VARCHAR(45),
`codDirett` VARCHAR(45),
INDEX `fk_dipartimento_idx` (`codDirett` ASC),
PRIMARY KEY (`nome`),
INDEX `fk_sede_idx` (`cittaSede` ASC),
CONSTRAINT `fk_dipartimento`
FOREIGN KEY (`codDirett`)
REFERENCES `mydb`.`impiegato` (`codice`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_sede`
FOREIGN KEY (`cittaSede`)
REFERENCES `mydb`.`sede` (`cittaSede`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`progetto`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`progetto` (
`nomeProgetto` VARCHAR(45) NOT NULL,
`budget` VARCHAR(45),
PRIMARY KEY (`nomeProgetto`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`partecipazione`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`partecipazione` (
`codImpieg` VARCHAR(45) NOT NULL,
`progetto` VARCHAR(45),
PRIMARY KEY (`codImpieg`),
INDEX `fk_progetto_idx` (`progetto` ASC),
CONSTRAINT `fk_impiegato`
FOREIGN KEY (`codImpieg`)
REFERENCES `mydb`.`impiegato` (`codice`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_progetto`
FOREIGN KEY (`progetto`)
REFERENCES `mydb`.`progetto` (`nomeProgetto`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
You have to match the impiegato row with the desired cognome with the row in sede with the same sede value, and that's what joins do:
select via
from sede s
join impiegato i
on s.sede = i.sede
where i.cognome = 'desiredCognome'
Related
It says there is an error but I can't figure out why.
-- -----------------------------------------------------
-- Table `mydb`.`Adresa`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Adresa` (
`Adresa_ID` INT NOT NULL,
`PSC` VARCHAR(45) NULL,
`Ulice` VARCHAR(45) NULL,
`Mesto` VARCHAR(45) NULL,
PRIMARY KEY (`Adresa_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Clenstvi`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Clenstvi` (
`Clenstvi_ID` INT NOT NULL,
`Klub` ENUM("Standard", "Premium") NULL,
`DatumExpirace` DATE NULL,
PRIMARY KEY (`Clenstvi_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Kontakt_zakaznik`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Kontakt_zakaznik` (
`Kontakt_ID` INT NOT NULL,
`Adresa_ID` INT NOT NULL,
`Telefon` VARCHAR(45) NULL,
PRIMARY KEY (`Kontakt_ID`, `Adresa_ID`),
INDEX `Adresa_ID_idx` (`Adresa_ID` ASC) ,
CONSTRAINT `Adresa_ID`
FOREIGN KEY (`Adresa_ID`)
REFERENCES `mydb`.`Adresa` (`Adresa_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Kontakt_ID`
FOREIGN KEY (`Kontakt_ID`)
REFERENCES `mydb`.`Zakaznik_informace` (`Kontakt_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Zakaznik_informace`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Zakaznik_informace` (
`Zakaznik_informace_ID` INT NOT NULL,
`Kontakt_ID` INT NOT NULL,
`Jmeno` VARCHAR(45) NOT NULL,
`Prijmeni` VARCHAR(45) NOT NULL,
`Datum_narozeni` DATE NOT NULL,
`Pohlavi` ENUM("Můž", "Žena") NOT NULL,
PRIMARY KEY (`Zakaznik_informace_ID`, `Kontakt_ID`),
CONSTRAINT `Zakaznik_informace_ID`
FOREIGN KEY (`Zakaznik_informace_ID`)
REFERENCES `mydb`.`Zakaznik` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Zakaznik`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Zakaznik` (
`ID` INT NOT NULL,
`Clenstvi_ID` INT NULL,
`Zakaznik_informace_ID` INT NOT NULL,
PRIMARY KEY (`ID`, `Zakaznik_informace_ID`, `Clenstvi_ID`),
INDEX `Clenstvi_ID_idx` (`Clenstvi_ID` ASC) ,
CONSTRAINT `Clenstvi_ID`
FOREIGN KEY (`Clenstvi_ID` )
REFERENCES `mydb`.`Clenstvi` (`Clenstvi_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
t
This is all I have but it just wont work because of the following error:
#1005 - Can't create table mydb.kontakt_zakaznik (error code:: 150 "Foreign key constraint is incorrectly formed")
The error is likely to be the reference to table Zakaznik_informace, which you have not yet created at the time you get the error.
You must create all referenced tables before you can make another table reference them with a foreign key.
So in this case, just edit your create script to move Zakaznik_informace before Kontakt_zakaznik. You must also move Zakaznik before Zakaznik_informace.
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.
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
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 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