MySQL error: Duplicate key on write or update - mysql

I have been using MySQL to design a database, when compiled into SQL code and executed it produces two errors. One that states Variable 'unique checks' can't be set to the value of 'NULL' and another that produces message "Duplicate key on write and update" I have a feeling that it is my foreign keys that are causing this issue but still cannot fix the problem. I have attached the query code below.
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 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`idUser` INT NOT NULL,
`first_name` VARCHAR(45) NULL,
`last_name` VARCHAR(45) NULL,
`username` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
`company_pin` VARCHAR(45) NULL,
`student_pin` VARCHAR(45) NULL,
`isAdmin` TINYINT(1) NULL DEFAULT 0,
`isCandidate` TINYINT(1) NULL,
`isVoter` TINYINT(1) NULL,
`votes_left` INT NULL,
`votes_achieved` INT NULL,
PRIMARY KEY (`idUser`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Issue`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Issue` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Issue` (
`idIssue` INT NOT NULL,
`issueTitle` VARCHAR(45) NULL,
`issueDescription` VARCHAR(255) NULL,
`userID` INT NULL,
`isResolved` TINYINT(1) NULL DEFAULT 0,
`upVotes` INT NULL,
PRIMARY KEY (`idIssue`),
INDEX `FK_USERID_idx` (`userID` ASC),
CONSTRAINT `FK_USERID`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Forum`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Forum` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Forum` (
`idForum` INT NOT NULL,
`userID` INT NULL,
`postTitle` VARCHAR(45) NULL,
`postText` VARCHAR(255) NULL,
`upVotes` INT NULL,
PRIMARY KEY (`idForum`),
INDEX `FK_USERID_idx` (`userID` ASC),
CONSTRAINT `FK_USERID`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Vote`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Vote` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Vote` (
`idVote` INT NOT NULL,
`voteFor` ENUM('TOPIC', 'USER', 'ISSUE') NULL,
`topicID` INT NULL,
`userID` INT NULL,
`issueID` INT NULL,
PRIMARY KEY (`idVote`),
INDEX `FK_USER_VOTED_FOR_idx` (`userID` ASC),
INDEX `FK_ISSUE_VOTED_FOR_idx` (`issueID` ASC),
INDEX `FK_FORUM_VOTED_FOR_idx` (`topicID` ASC),
CONSTRAINT `FK_USER_VOTED_FOR`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_ISSUE_VOTED_FOR`
FOREIGN KEY (`issueID`)
REFERENCES `mydb`.`Issue` (`idIssue`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_FORUM_VOTED_FOR`
FOREIGN KEY (`topicID`)
REFERENCES `mydb`.`Forum` (`idForum`)
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 already have a Constraint with the name FK_USERID on table mydb.issue. Give the constraint on mydb.Forum another name. e.g.
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 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`idUser` INT NOT NULL,
`first_name` VARCHAR(45) NULL,
`last_name` VARCHAR(45) NULL,
`username` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
`company_pin` VARCHAR(45) NULL,
`student_pin` VARCHAR(45) NULL,
`isAdmin` TINYINT(1) NULL DEFAULT 0,
`isCandidate` TINYINT(1) NULL,
`isVoter` TINYINT(1) NULL,
`votes_left` INT NULL,
`votes_achieved` INT NULL,
PRIMARY KEY (`idUser`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Issue`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Issue` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Issue` (
`idIssue` INT NOT NULL,
`issueTitle` VARCHAR(45) NULL,
`issueDescription` VARCHAR(255) NULL,
`userID` INT NULL,
`isResolved` TINYINT(1) NULL DEFAULT 0,
`upVotes` INT NULL,
PRIMARY KEY (`idIssue`),
INDEX `FK_USERID_idx` (`userID` ASC),
CONSTRAINT `FK_USERID`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Forum`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Forum` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Forum` (
`idForum` INT NOT NULL,
`userID` INT NULL,
`postTitle` VARCHAR(45) NULL,
`postText` VARCHAR(255) NULL,
`upVotes` INT NULL,
PRIMARY KEY (`idForum`),
INDEX `FK_USERID_idx` (`userID` ASC),
CONSTRAINT `FK_Forum_USERID`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Vote`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Vote` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Vote` (
`idVote` INT NOT NULL,
`voteFor` ENUM('TOPIC', 'USER', 'ISSUE') NULL,
`topicID` INT NULL,
`userID` INT NULL,
`issueID` INT NULL,
PRIMARY KEY (`idVote`),
INDEX `FK_USER_VOTED_FOR_idx` (`userID` ASC),
INDEX `FK_ISSUE_VOTED_FOR_idx` (`issueID` ASC),
INDEX `FK_FORUM_VOTED_FOR_idx` (`topicID` ASC),
CONSTRAINT `FK_USER_VOTED_FOR`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_ISSUE_VOTED_FOR`
FOREIGN KEY (`issueID`)
REFERENCES `mydb`.`Issue` (`idIssue`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_FORUM_VOTED_FOR`
FOREIGN KEY (`topicID`)
REFERENCES `mydb`.`Forum` (`idForum`)
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 got this error at a time of restoring of database & I found below details :
This issue comes into picture when your table/database names are not same like small/capital letters used in same table/database name on both side (needs to maintain same name on both side as it's case_sensitive).

Related

Error 1215: Cannot add foreign key constraint mysql workbench 1

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

Error 121 can't create table

I am trying to import a database created in mySql workbench into phpMyAdmin (WAMP server) , when I try to import it I get error :
MySQL said: Documentation
#1005 - Can't create table '.\mydb\team.frm' (errno: 121)
Below is the executed SQL script
-- MySQL Script generated by MySQL Workbench
-- 03/03/15 13:06:58
-- Model: New Model Version: 1.0
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Team`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Team` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`TeamName` VARCHAR(45) NOT NULL,
`GoverningBody` VARCHAR(45) NOT NULL,
`Country` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Competitor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Competitor` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`FirstName` VARCHAR(45) NOT NULL,
`Surname` VARCHAR(45) NOT NULL,
`Age` INT NOT NULL,
`Nationality` VARCHAR(45) NOT NULL,
`Title` VARCHAR(45) NOT NULL,
`ContactNumber` INT NOT NULL,
`team_id` DATETIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `TeamID_idx` (`team_id` ASC),
CONSTRAINT `TeamID`
FOREIGN KEY (`team_id`)
REFERENCES `mydb`.`Team` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Card`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Card` (
`id` INT NOT NULL,
`referred_as` VARCHAR(45) NULL,
`IssueDate` DATE NOT NULL,
`ExpiryDate` DATE NOT NULL,
`Status` ENUM('Active','Expired','Lost') NOT NULL,
`competitor_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `CompetitorID_idx` (`competitor_id` ASC),
CONSTRAINT `CompetitorID`
FOREIGN KEY (`competitor_id`)
REFERENCES `mydb`.`Competitor` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Venue`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Venue` (
`id` INT NOT NULL,
`referred_as` VARCHAR(45) NULL,
`Stadium` VARCHAR(45) NOT NULL,
`Location` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Fixture`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Fixture` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`Time` TIME NOT NULL,
`Date` DATE NOT NULL,
`Type` VARCHAR(45) NOT NULL,
`venue_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `VenueID_idx` (`venue_id` ASC),
CONSTRAINT `VenueID`
FOREIGN KEY (`venue_id`)
REFERENCES `mydb`.`Venue` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Authorisation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Authorisation` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`fixture_id` INT NOT NULL,
`card_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `FixtureID_idx` (`fixture_id` ASC),
INDEX `CardID_idx` (`card_id` ASC),
CONSTRAINT `FixtureID`
FOREIGN KEY (`fixture_id`)
REFERENCES `mydb`.`Fixture` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `CardID`
FOREIGN KEY (`card_id`)
REFERENCES `mydb`.`Card` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Log`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Log` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`card_id` INT NOT NULL,
`venue_id` INT NOT NULL,
`LogTime` TIME NOT NULL,
`LogDate` DATE NOT NULL,
PRIMARY KEY (`id`),
INDEX `CardID_idx` (`card_id` ASC),
INDEX `VenueID_idx` (`venue_id` ASC),
CONSTRAINT `CardID`
FOREIGN KEY (`card_id`)
REFERENCES `mydb`.`Card` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `VenueID`
FOREIGN KEY (`venue_id`)
REFERENCES `mydb`.`Venue` (`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;
Making the changes in the comments above worked for me (the DATETIME to INT and making unique constraint and index names). As a safe practice, in our organization we always name our indexes/constraints as (table1Identifier)_(table2Identifier)_(constraintType)...so something like Log_Card_idx or Auth_Fixture_FK. Just a thought.
-- MySQL Script generated by MySQL Workbench
-- 03/03/15 13:06:58
-- Model: New Model Version: 1.0
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Team`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Team` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`TeamName` VARCHAR(45) NOT NULL,
`GoverningBody` VARCHAR(45) NOT NULL,
`Country` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Competitor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Competitor` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`FirstName` VARCHAR(45) NOT NULL,
`Surname` VARCHAR(45) NOT NULL,
`Age` INT NOT NULL,
`Nationality` VARCHAR(45) NOT NULL,
`Title` VARCHAR(45) NOT NULL,
`ContactNumber` INT NOT NULL,
`team_id` DATETIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `TeamID_idx` (`team_id` ASC),
CONSTRAINT `TeamID`
FOREIGN KEY (`team_id`)
REFERENCES `mydb`.`Team` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Card`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Card` (
`id` INT NOT NULL,
`referred_as` VARCHAR(45) NULL,
`IssueDate` DATE NOT NULL,
`ExpiryDate` DATE NOT NULL,
`Status` ENUM('Active','Expired','Lost') NOT NULL,
`competitor_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `CompetitorID_idx` (`competitor_id` ASC),
CONSTRAINT `CompetitorID`
FOREIGN KEY (`competitor_id`)
REFERENCES `mydb`.`Competitor` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Venue`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Venue` (
`id` INT NOT NULL,
`referred_as` VARCHAR(45) NULL,
`Stadium` VARCHAR(45) NOT NULL,
`Location` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Fixture`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Fixture` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`Time` TIME NOT NULL,
`Date` DATE NOT NULL,
`Type` VARCHAR(45) NOT NULL,
`venue_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `VenueID_idx` (`venue_id` ASC),
CONSTRAINT `VenueID`
FOREIGN KEY (`venue_id`)
REFERENCES `mydb`.`Venue` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Authorisation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Authorisation` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`fixture_id` INT NOT NULL,
`card_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `Auth_FixtureID_idx` (`fixture_id` ASC),
INDEX `Auth_CardID_idx` (`card_id` ASC),
CONSTRAINT `Auth_FixtureID`
FOREIGN KEY (`fixture_id`)
REFERENCES `Fixture` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Auth_CardID`
FOREIGN KEY (`card_id`)
REFERENCES `Card` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Log`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Log` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`card_id` INT NOT NULL,
`venue_id` INT NOT NULL,
`LogTime` TIME NOT NULL,
`LogDate` DATE NOT NULL,
PRIMARY KEY (`id`),
INDEX `Log_CardID_idx` (`card_id` ASC),
INDEX `Log_VenueID_idx` (`venue_id` ASC),
CONSTRAINT `Log_CardID`
FOREIGN KEY (`card_id`)
REFERENCES `mydb`.`Card` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Log_VenueID`
FOREIGN KEY (`venue_id`)
REFERENCES `Venue` (`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;

Create table failed in MySQL

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

MySQL innodb - Foreign Key: only 1st works?

I have this example bellow where customers_b cannot be created.
Error Code 1005 / errno: 121.
However, if I create customers_b first than customers_a, then customers_a is the one which won't be created.
What is wrong? Why I cant link more than one FK to the PK 'id_state'?
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';
DROP SCHEMA IF EXISTS `testdb` ;
CREATE SCHEMA IF NOT EXISTS `testdb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `testdb` ;
-- -----------------------------------------------------
-- Table `testdb`.`state`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `testdb`.`state` ;
CREATE TABLE IF NOT EXISTS `testdb`.`state` (
`id_state` INT NOT NULL,
`abbr` CHAR(2) NOT NULL,
PRIMARY KEY (`id_state`),
UNIQUE INDEX `id_state_UNIQUE` (`id_state` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testdb`.`customers_a`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `testdb`.`customers_a` ;
CREATE TABLE IF NOT EXISTS `testdb`.`customers_a` (
`id_customer` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`addr_state` INT NOT NULL,
PRIMARY KEY (`id_customer`),
CONSTRAINT `fk_state`
FOREIGN KEY (`addr_state`)
REFERENCES `testdb`.`state` (`id_state`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testdb`.`customers_b`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `testdb`.`customers_b` ;
CREATE TABLE IF NOT EXISTS `testdb`.`customers_b` (
`id_customer` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`addr_state` INT NOT NULL,
PRIMARY KEY (`id_customer`),
CONSTRAINT `fk_state`
FOREIGN KEY (`addr_state`)
REFERENCES `testdb`.`state` (`id_state`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
You can't use the same name fk_state for both constraints. Give one of them a different name.
CREATE TABLE IF NOT EXISTS `customers_b` (
`id_customer` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`addr_state` INT NOT NULL,
PRIMARY KEY (`id_customer`),
CONSTRAINT `fk_state_b`
FOREIGN KEY (`addr_state`)
REFERENCES `state` (`id_state`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Your both tables constraint name is same CONSTRAINT fk_state. for customer_b change that name like below
CREATE TABLE IF NOT EXISTS `customers_a` (
`id_customer` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`addr_state` INT NOT NULL,
PRIMARY KEY (`id_customer`),
CONSTRAINT `fk_state`
FOREIGN KEY (`addr_state`)
REFERENCES `state` (`id_state`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `customers_b` (
`id_customer` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`addr_state` INT NOT NULL,
PRIMARY KEY (`id_customer`),
CONSTRAINT `fk_state1` <-- Here
FOREIGN KEY (`addr_state`)
REFERENCES `state` (`id_state`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

#1005 - Can't create table 'feedback.answer' (errno: 150)

I am getting an error with mysql and I do not understand why:
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 `feedback` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `feedback` ;
-- -----------------------------------------------------
-- Table `feedback`.`application`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`application` (
`application_id` INT NOT NULL AUTO_INCREMENT,
`app_name` VARCHAR(45) NULL,
PRIMARY KEY (`application_id`),
UNIQUE INDEX `app_name_UNIQUE` (`app_name` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`user` (
`user_id` INT NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(45) NOT NULL,
`lastname` VARCHAR(45) NULL,
`email` VARCHAR(45) NOT NULL,
`customer_length` VARCHAR(45) NULL,
PRIMARY KEY (`user_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`users_has_application`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`users_has_application` (
`user_id` INT NOT NULL,
`application_id` INT NOT NULL,
PRIMARY KEY (`user_id`, `application_id`),
INDEX `fk_users_has_application_application1_idx` (`application_id` ASC),
INDEX `fk_users_has_application_users_idx` (`user_id` ASC),
CONSTRAINT `fk_users_has_application_users`
FOREIGN KEY (`user_id`)
REFERENCES `feedback`.`user` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_has_application_application1`
FOREIGN KEY (`application_id`)
REFERENCES `feedback`.`application` (`application_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`survey`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`survey` (
`survey_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`description` VARCHAR(255) NULL,
`is_active` TINYINT(1) NULL,
PRIMARY KEY (`survey_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`question`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`question` (
`question_id` INT NOT NULL,
`question_text` VARCHAR(255) NULL,
PRIMARY KEY (`question_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`option`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`option` (
`option_id` INT NOT NULL AUTO_INCREMENT,
`question_id` INT NOT NULL,
`option_number` INT NOT NULL,
`option_text` TEXT NULL,
INDEX `fk_option_question1_idx` (`question_id` ASC),
PRIMARY KEY (`option_id`),
UNIQUE INDEX `uk_question_option_number_key` (`question_id` ASC, `option_number` ASC),
CONSTRAINT `fk_option_question1`
FOREIGN KEY (`question_id`)
REFERENCES `feedback`.`question` (`question_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`answer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`answer` (
`answer_id` INT NOT NULL AUTO_INCREMENT,
`user_id` INT NOT NULL,
`option_id` INT NOT NULL,
`date_submitted` DATETIME NOT NULL,
PRIMARY KEY (`answer_id`),
INDEX `fk_answer_user1_idx` (`user_id` ASC),
INDEX `fk_answer_option1_idx` (`option_id` ASC),
CONSTRAINT `fk_answer_user1`
FOREIGN KEY (`user_id`)
REFERENCES `feedback`.`user` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_answer_option1`
FOREIGN KEY (`option_id`)
REFERENCES `feedback`.`option` (`option_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`survey_has_question`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`survey_has_question` (
`survey_id` INT NOT NULL,
`question_id` INT NOT NULL,
`question_number` INT NULL,
PRIMARY KEY (`survey_id`, `question_id`),
INDEX `fk_survey_has_question_question1_idx` (`question_id` ASC),
INDEX `fk_survey_has_question_survey1_idx` (`survey_id` ASC),
UNIQUE INDEX `unique_order_key` (`survey_id` ASC, `question_number` ASC),
CONSTRAINT `fk_survey_has_question_survey1`
FOREIGN KEY (`survey_id`)
REFERENCES `feedback`.`survey` (`survey_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_survey_has_question_question1`
FOREIGN KEY (`question_id`)
REFERENCES `feedback`.`question` (`question_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;
Error:
#1005 - Can't create table 'feedback.answer' (errno: 150)
I am creating my table from this as a template:
https://dba.stackexchange.com/questions/16002/survey-database-design-associate-an-answer-to-a-user/16047#16047
My thought process for adding answer_id to the answer table is that I want users to be able to fill out the same survey multiple times.
Why is the answer table throwing an error?
EDIT:
Server version: 5.5.29-0ubuntu0.12.04.2
I am importing this using phpmyadmin
Your code worked on MYSQL server 5.1 without errors.
A common cause of errno: 150 is when you create a FK constraint that references a PK that does not yet exist. Make sure both your "user" and "option" tables are created first before you create the "answer" table.
To help debug you can remove the FK constraints one at a time to see which one is triggering the problem.
If you execute the code in the order shown, I do not see any FK problems that would occur.
try this
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';
-- -----------------------------------------------------
-- Table `feedback`.`application`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `application` (
`application_id` INT NOT NULL AUTO_INCREMENT,
`app_name` VARCHAR(45) NULL,
PRIMARY KEY (`application_id`),
UNIQUE INDEX `app_name_UNIQUE` (`app_name` ASC))
;
your working code in fiddle