I’m working on my first MySQL database for an assignment at my university. Unfortunately I’ve been stuck for a while trying to create the actual tables with foreign keys between them.
This is the error that the MySQL Workbench forward engineering wizard gives:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'test.fremført' (errno: 150)
CREATE TABLE IF NOT EXISTS `Fremført` (
`Plate` VARCHAR(20) NOT NULL ,
`Verk` VARCHAR(45) NOT NULL ,
`Artist` VARCHAR(45) NOT NULL ,
`Dato` DATE NULL ,
PRIMARY KEY (`Plate`, `Verk`, `Artist`) ,
INDEX `Fremført->Artist_idx` (`Artist` ASC) ,
INDEX `Fremført->Spor_idx` (`Plate` ASC, `Verk` ASC) ,
CONSTRAINT `Fremført->Artist`
FOREIGN KEY (`Artist` )
REFERENCES `Artist` (`ArtistNavn` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Fremført->Spor`
FOREIGN KEY (`Plate` , `Verk` )
REFERENCES `Spor` (`Verk` , `Verk` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
Does anyone know what’s wrong with the above script, and if so have a solution?
Thanks!
edit:
This is the requested spor query
DROP TABLE IF EXISTS `Spor` ;
CREATE TABLE IF NOT EXISTS `Spor` (
`Plate` VARCHAR(45) NOT NULL ,
`Verk` VARCHAR(45) NOT NULL ,
`Spilletid` DECIMAL(3,2) NULL ,
PRIMARY KEY (`Plate`, `Verk`) ,
INDEX `Plate_idx` (`Plate` ASC) ,
CONSTRAINT `Plate`
FOREIGN KEY (`Plate` )
REFERENCES `Plate` (`KatalogNr` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
DROP TABLE IF EXISTS `Artist` ;
CREATE TABLE IF NOT EXISTS `Artist` (
`ArtistNavn` VARCHAR(30) NOT NULL ,
`Artistcol` VARCHAR(45) NULL ,
PRIMARY KEY (`ArtistNavn`) )
ENGINE = InnoDB;
errno150 is very often related to a mismatch between the data types of the primary and related column. They must match exactly, including character length.
I see a data type mismatch between
Fremført.artist(VARCHAR(45)) andArtist.ArtistNavn(VARCHAR(30)). These must be the same for the FOREIGN KEY constraint to succeed.As noted above, if the constraint
Fremført->Sporwas intended to referenceSpor (Plate , Verk )instead ofSpor (Verk , Verk )as you have defined it, then you will also encounter an err150 due to the type mismatch betweenSpor.PlateandFremført.Plate. ChangeFremført.PlatetoVARCHAR(45).