MySQL Server Error 1005 - mysql

When I try to run SQL script I get the log error in Workbench Forward Engineer:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'regional_budget.budget' (errno: 121)
SQL Code:
-- -----------------------------------------------------
-- Table `regional_budget`.`budget`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`budget` (
`id` INT(3) NOT NULL AUTO_INCREMENT,
`region_id` INT(3) NOT NULL,
`balance` DECIMAL(11,2) NULL,
`income_id` INT(3) NOT NULL,
`income` DECIMAL(11,2) NULL,
`outcome_id` INT(3) NOT NULL,
`outcome` DECIMAL(11,2) NULL,
PRIMARY KEY (`id`),
INDEX `region_id_idx` (`region_id` ASC),
INDEX `outcome_id_idx` (`outcome_id` ASC),
INDEX `income_id_idx` (`income_id` ASC),
CONSTRAINT `region_id`
FOREIGN KEY (`region_id`)
REFERENCES `regional_budget`.`regions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `income_id`
FOREIGN KEY (`income_id`)
REFERENCES `regional_budget`.`income` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `outcome_id`
FOREIGN KEY (`outcome_id`)
REFERENCES `regional_budget`.`outcome` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 13 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
SQL script:
-- 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 regional_budget
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema regional_budget
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `regional_budget` DEFAULT CHARACTER SET utf8 ;
USE `regional_budget` ;
-- -----------------------------------------------------
-- Table `regional_budget`.`regions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`regions` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regional_budget`.`subjects`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`subjects` (
`id` INT NOT NULL,
`name` VARCHAR(20) NULL,
`region_id` INT NULL,
PRIMARY KEY (`id`),
INDEX `region_id_idx` (`region_id` ASC),
CONSTRAINT `region_id`
FOREIGN KEY (`region_id`)
REFERENCES `regional_budget`.`regions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regional_budget`.`income`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`income` (
`id` INT NOT NULL AUTO_INCREMENT,
`tax_and_non_tax` DECIMAL(11,2) NULL,
`receipts` DECIMAL(11,2) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regional_budget`.`outcome`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`outcome` (
`id` INT NOT NULL AUTO_INCREMENT,
`transfers` DECIMAL(11,2) NULL,
`subventions` DECIMAL(11,2) NULL,
`subsidies` DECIMAL(11,2) NULL,
`dotations` DECIMAL(11,2) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regional_budget`.`population`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`population` (
`subject_id` INT NOT NULL,
`count` DECIMAL(11,2) NULL,
PRIMARY KEY (`subject_id`),
CONSTRAINT `subject_id`
FOREIGN KEY (`subject_id`)
REFERENCES `regional_budget`.`subjects` (`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;
-- -----------------------------------------------------
-- Table `regional_budget`.`budget`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`budget` (
`id` INT(3) NOT NULL AUTO_INCREMENT,
`region_id` INT(3) NOT NULL,
`balance` DECIMAL(11,2) NULL,
`income_id` INT(3) NOT NULL,
`income` DECIMAL(11,2) NULL,
`outcome_id` INT(3) NOT NULL,
`outcome` DECIMAL(11,2) NULL,
PRIMARY KEY (`id`),
INDEX `region_id_idx` (`region_id` ASC),
INDEX `outcome_id_idx` (`outcome_id` ASC),
INDEX `income_id_idx` (`income_id` ASC),
CONSTRAINT `region_id`
FOREIGN KEY (`region_id`)
REFERENCES `regional_budget`.`regions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `income_id`
FOREIGN KEY (`income_id`)
REFERENCES `regional_budget`.`income` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `outcome_id`
FOREIGN KEY (`outcome_id`)
REFERENCES `regional_budget`.`outcome` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
P.S. I tried to create 'regional_budget'.'budget' with foreign keys after other reference tables. Also I tried to use foreign_key_checks, but nothing helps.

I suggest you try changing the identifiers of the foreign key constraints to be unique, and not conflict with identifiers already used for columns (or other constraints.
For example:
CONSTRAINT `FK_budget_regions` FOREIGN KEY ...
CONSTRAINT `FK_budget_income` FOREIGN KEY ...
CONSTRAINT `FK_budget_outcome` FOREIGN KEY ...

Related

Failed to add the foreign key constraint. Missing column 'gym_id' for constraint 'fk_gyms_instructors_gyms' in the referenced table 'gyms'

I'm forward engineering a database from an ERD that I created in MySqlWorkbench. I have a very basic many to many relationship between the "gyms" table and the "instructors" table, but I'm getting the error above. To me, the error reads that MySql can't find the "gym_id" column in the "gyms" table when it tries to create the many-to-many table.
I don't know why that would be the case.
I've went through the tables to make sure there were no typos and that the data types matched. I also went through the execution script to make sure the "gyms" table was being created before the many-to-many. It was. I can't sniff out what is causing the error. So any help or suggestion is appreciated
This is the error:
Executing SQL script in server
ERROR: Error 3734: Failed to add the foreign key constraint. Missing column 'gym_id' for constraint 'fk_gyms_instructors_gyms' in the referenced table 'gyms'
SQL Code:
-- -----------------------------------------------------
-- Table `fitness`.`gyms_instructors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`gyms_instructors` (
`id` INT NOT NULL AUTO_INCREMENT,
`gym_id` SMALLINT NOT NULL,
`instructor_id` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_gyms_instructors_gyms_idx` (`gym_id` ASC) VISIBLE,
INDEX `fk_gyms_instructors_instructors_idx` (`instructor_id` ASC) VISIBLE,
CONSTRAINT `fk_gyms_instructors_gyms`
FOREIGN KEY (`gym_id`)
REFERENCES `fitness`.`gyms` (`gym_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_gyms_instructors_instructors`
FOREIGN KEY (`instructor_id`)
REFERENCES `fitness`.`instructors` (`instructor_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB
SQL script execution finished: statements: 10 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
The other potentially useful information is:
1. On the many to many table, in the foreign keys section both gym_id and instructor_id are SMALLINTS and NOT NULL,
2. On their respective table the gym_id and instructor_id are autoincrementing primary keys, NOT NULL
3. I pasted the full script below if you want to look.
-- 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 fitness
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema fitness
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `fitness` DEFAULT CHARACTER SET utf8 ;
USE `fitness` ;
-- -----------------------------------------------------
-- Table `fitness`.`fitness_classes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`fitness_classes` (
`fitness_class_id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(45) NOT NULL,
`price` DECIMAL(2,2) NOT NULL,
`description` VARCHAR(500) NOT NULL,
`vacancies` INT NOT NULL,
`start_time` DATETIME NOT NULL,
PRIMARY KEY (`fitness_class_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`categories`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`categories` (
`category_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`description` TEXT(1200) NULL,
PRIMARY KEY (`category_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`tags`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`tags` (
`id` INT NOT NULL AUTO_INCREMENT,
`fitness_class_id` INT NOT NULL,
`category_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_tags_fitness_classes_idx` (`fitness_class_id` ASC) VISIBLE,
INDEX `fk_tags_categories_idx` (`category_id` ASC) VISIBLE,
CONSTRAINT `fk_tags_fitness_classes`
FOREIGN KEY (`fitness_class_id`)
REFERENCES `fitness`.`fitness_classes` (`fitness_class_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tags_categories`
FOREIGN KEY (`category_id`)
REFERENCES `fitness`.`categories` (`category_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`gyms`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`gyms` (
`gym_id` SMALLINT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(55) NOT NULL,
`address` VARCHAR(55) NOT NULL,
`city` VARCHAR(25) NOT NULL,
`state` VARCHAR(25) NOT NULL,
`phone` VARCHAR(10) NOT NULL,
`latitude` DECIMAL(10,8) NULL,
`longitude` DECIMAL(10,8) NULL,
`neighborhood` VARCHAR(45) NOT NULL,
`create_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`gym_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`instructors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`instructors` (
`instructor_id` SMALLINT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(55) NOT NULL,
`last_name` VARCHAR(55) NOT NULL,
`create_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`instructor_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`gyms_instructors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`gyms_instructors` (
`id` INT NOT NULL AUTO_INCREMENT,
`gym_id` SMALLINT NOT NULL,
`instructor_id` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_gyms_instructors_gyms_idx` (`gym_id` ASC) VISIBLE,
INDEX `fk_gyms_instructors_instructors_idx` (`instructor_id` ASC) VISIBLE,
CONSTRAINT `fk_gyms_instructors_gyms`
FOREIGN KEY (`gym_id`)
REFERENCES `fitness`.`gyms` (`gym_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_gyms_instructors_instructors`
FOREIGN KEY (`instructor_id`)
REFERENCES `fitness`.`instructors` (`instructor_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`listings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`listings` (
`id` INT NOT NULL AUTO_INCREMENT,
`gym_id` SMALLINT NOT NULL,
`fitness_class_id` INT NOT NULL,
`instructor_id` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_listings_gym_idx` (`gym_id` ASC) VISIBLE,
INDEX `fk_listings_fitness_class_idx` (`fitness_class_id` ASC) VISIBLE,
INDEX `fk_listings_instructor_idx` (`instructor_id` ASC) VISIBLE,
CONSTRAINT `fk_listings_gym`
FOREIGN KEY (`gym_id`)
REFERENCES `fitness`.`gyms` (`gym_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_listings_fitness_class`
FOREIGN KEY (`fitness_class_id`)
REFERENCES `fitness`.`fitness_classes` (`fitness_class_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_listings_instructor`
FOREIGN KEY (`instructor_id`)
REFERENCES `fitness`.`instructors` (`instructor_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`users` (
`user_id` INT NOT NULL AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(32) NOT NULL,
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`username` VARCHAR(16) NOT NULL,
PRIMARY KEY (`user_id`));
-- -----------------------------------------------------
-- Table `fitness`.`reviews`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`reviews` (
`review_id` INT NOT NULL AUTO_INCREMENT,
`fitness_class_id` INT NOT NULL,
`rating` TINYINT(1) NOT NULL,
`review_text` TEXT(1200) NULL,
`create_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`review_id`),
INDEX `fk_reviews_classes_idx` (`fitness_class_id` ASC) VISIBLE,
CONSTRAINT `fk_reviews_classes`
FOREIGN KEY (`fitness_class_id`)
REFERENCES `fitness`.`fitness_classes` (`fitness_class_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`bookings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`bookings` (
`booking_id` INT NOT NULL AUTO_INCREMENT,
`fitness_class_id` INT NOT NULL,
`user_id` INT NOT NULL,
`price` DECIMAL(2,2) NOT NULL,
`class_start_time` DATETIME NOT NULL,
`purchase_date` DATETIME NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`booking_id`),
CONSTRAINT `user_id`
FOREIGN KEY ()
REFERENCES `fitness`.`users` ()
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fitness_class_id`
FOREIGN KEY ()
REFERENCES `fitness`.`fitness_classes` ()
ON DELETE NO ACTION
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;

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 #1215 - Cannot add foreign key constraint

I have generated a sql script with MySql Workbench.
When I try to create my database I have "Error 1215 1215 - Cannot add foreign key constraint".
The problem seems to be with my last foreign key in the last create table :
CONSTRAINT `fk_tb_animation_option_col_debut`
FOREIGN KEY (`DT_ANO_DEBUT`)
REFERENCES `brest2016`.`tb_animation_option` (`DT_ANO_DEBUT`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
This is my entire script:
-- MySQL Script generated by MySQL Workbench
-- 01/16/16 00:46:22
-- 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 brest2016
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `brest2016` DEFAULT CHARACTER SET utf8 ;
USE `brest2016` ;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_visiteur`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_visiteur` (
`D_VI_CODE` INT(3) NOT NULL AUTO_INCREMENT,
`V_VI_NOM` VARCHAR(45) NOT NULL,
`V_VI_PRENOM` VARCHAR(45) NOT NULL,
`V_VI_EMAIL` VARCHAR(45) NOT NULL,
`V_VI_TELEPHONE` VARCHAR(45) NULL,
`DT_VI_DEBUT` DATETIME NOT NULL,
`DT_VI_FIN` DATETIME NOT NULL,
PRIMARY KEY (`D_VI_CODE`),
UNIQUE INDEX `idVisiteur_UNIQUE` (`D_VI_CODE` ASC),
UNIQUE INDEX `email_UNIQUE` (`V_VI_EMAIL` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_type_animation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_type_animation` (
`D_TY_CODE` INT(3) NOT NULL AUTO_INCREMENT,
`V_TY_LIBELLE` VARCHAR(45) NULL,
PRIMARY KEY (`D_TY_CODE`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_animation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_animation` (
`D_AN_CODE` INT(3) NOT NULL AUTO_INCREMENT,
`V_AN_LIBELLE` VARCHAR(45) NOT NULL,
`V_AN_LIEU` VARCHAR(45) NOT NULL,
`V_AN_IMAGE` VARCHAR(45) NULL,
`V_AN_DESCRIPTIF` VARCHAR(45) NULL,
`D_AN_PRIORITE` VARCHAR(45) NOT NULL,
`D_TY_CODE` INT(3) NULL,
PRIMARY KEY (`D_AN_CODE`),
UNIQUE INDEX `idAnimation_UNIQUE` (`D_AN_CODE` ASC),
INDEX `fk_tb_type_animation_col_code_1_idx` (`D_TY_CODE` ASC),
CONSTRAINT `fk_tb_type_animation_col_code`
FOREIGN KEY (`D_TY_CODE`)
REFERENCES `brest2016`.`tb_type_animation` (`D_TY_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_option`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_option` (
`D_OP_CODE` INT(3) NOT NULL AUTO_INCREMENT,
`V_OP_LIBELLE` VARCHAR(45) NOT NULL,
PRIMARY KEY (`D_OP_CODE`),
UNIQUE INDEX `idOption_UNIQUE` (`D_OP_CODE` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_animation_option`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_animation_option` (
`D_AN_CODE` INT(3) NOT NULL,
`D_OP_CODE` INT(3) NOT NULL,
`DT_ANO_DEBUT` DATETIME NOT NULL,
`DT_ANO_FIN` DATETIME NOT NULL,
`D_ANO_PLACES_MAX` INT(3) NOT NULL,
`D_ANO_DUREE` INT(3) NOT NULL,
PRIMARY KEY (`D_AN_CODE`, `D_OP_CODE`, `DT_ANO_DEBUT`),
INDEX `fk_tb_option_idx` (`D_OP_CODE` ASC),
CONSTRAINT `fk_tb_animation_col_code`
FOREIGN KEY (`D_AN_CODE`)
REFERENCES `brest2016`.`tb_animation` (`D_AN_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_option_col_code`
FOREIGN KEY (`D_OP_CODE`)
REFERENCES `brest2016`.`tb_option` (`D_OP_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = 'association entre une animation et une option';
-- -----------------------------------------------------
-- Table `brest2016`.`tb_administrateur`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_administrateur` (
`D_AD_CODE` INT(3) NOT NULL,
`V_AD_ID` VARCHAR(12) NOT NULL,
`V_AD_MDP` VARCHAR(12) NOT NULL,
PRIMARY KEY (`D_AD_CODE`),
UNIQUE INDEX `idAdministrateur_UNIQUE` (`D_AD_CODE` ASC),
UNIQUE INDEX `V_UT_LOGIN_UNIQUE` (`V_AD_ID` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_reservation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_reservation` (
`D_VI_CODE` INT(3) NOT NULL,
`D_AN_CODE` INT(3) NOT NULL,
`D_OP_CODE` INT(3) NOT NULL,
`DT_ANO_DEBUT` DATETIME NOT NULL,
PRIMARY KEY (`D_VI_CODE`, `D_AN_CODE`, `D_OP_CODE`, `DT_ANO_DEBUT`),
INDEX `fk_tb_animation_option_col_code_animation_idx` (`D_AN_CODE` ASC),
INDEX `fk_tb_animation_option_col_code_option_idx` (`D_OP_CODE` ASC),
INDEX `fk_tb_animation_option_col_debut_idx` (`DT_ANO_DEBUT` ASC),
CONSTRAINT `fk_tb_visiteur_col_code`
FOREIGN KEY (`D_VI_CODE`)
REFERENCES `brest2016`.`tb_visiteur` (`D_VI_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_animation_option_col_code_animation`
FOREIGN KEY (`D_AN_CODE`)
REFERENCES `brest2016`.`tb_animation_option` (`D_AN_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_animation_option_col_code_option`
FOREIGN KEY (`D_OP_CODE`)
REFERENCES `brest2016`.`tb_animation_option` (`D_OP_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_animation_option_col_debut`
FOREIGN KEY (`DT_ANO_DEBUT`)
REFERENCES `brest2016`.`tb_animation_option` (`DT_ANO_DEBUT`)
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;
The column named in a foreign key must be indexed.
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_animation_option` (
`D_AN_CODE` INT(3) NOT NULL,
`D_OP_CODE` INT(3) NOT NULL,
`DT_ANO_DEBUT` DATETIME NOT NULL,
`DT_ANO_FIN` DATETIME NOT NULL,
`D_ANO_PLACES_MAX` INT(3) NOT NULL,
`D_ANO_DUREE` INT(3) NOT NULL,
PRIMARY KEY (`D_AN_CODE`, `D_OP_CODE`, `DT_ANO_DEBUT`),
INDEX `fk_tb_option_idx` (`D_OP_CODE` ASC),
INDEX `dt_ano_debut_idx` (`T_ANO_DEBUT`), -- Add this line
CONSTRAINT `fk_tb_animation_col_code`
FOREIGN KEY (`D_AN_CODE`)
REFERENCES `brest2016`.`tb_animation` (`D_AN_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_option_col_code`
FOREIGN KEY (`D_OP_CODE`)
REFERENCES `brest2016`.`tb_option` (`D_OP_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = 'association entre une animation et une option';

#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

Trying to import SQL file in a xampp server returns error

I have done a ER diagram in Mysql Workbench, and I am trying load in my server with phpMyAdmin, but it returns me the next error:
Error
SQL Query:
-- ----------------------------------------------------- -- Table `BDA`.`UG` -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `BDA`.`UG` ( `numero_ug` INT NOT NULL, `nombre` VARCHAR(45) NOT NULL, `segunda_firma_autorizada` VARCHAR(45) NOT NULL, `fecha_creacion` DATE NOT NULL, `nombre_depto` VARCHAR(140) NOT NULL, `dni` INT NOT NULL, `anho_contable` INT NOT NULL, PRIMARY KEY (`numero_ug`), INDEX `nombre_depto_idx` (`nombre_depto` ASC), INDEX `dni_idx` (`dni` ASC), INDEX `anho_contable_idx` (`anho_contable` ASC), CONSTRAINT `nombre_depto` FOREIGN KEY (`nombre_depto`) REFERENCES `BDA`.`Departamento` (`nombre_depto`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `dni` FOREIGN KEY (`dni`) REFERENCES `BDA`.`Trabajador` (`dni`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `anho_contable` FOREIGN KEY (`anho_contable`) REFERENCES `BDA`.`Capitulo_Contable` (`anho_contable`) [...]
MySQL said: Documentation
#1022 - Can't write; duplicate key in table 'ug'
I export the result of the diagram from Mysql Workbench to a SQL file, and this file is what I'm trying to upload.
This is the file. I can not find the duplicate key.
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 `BDA` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `BDA` ;
-- -----------------------------------------------------
-- Table `BDA`.`Departamento`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Departamento` (
`nombre_depto` VARCHAR(140) NOT NULL,
`area_depto` VARCHAR(140) NOT NULL,
PRIMARY KEY (`nombre_depto`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`Trabajador`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Trabajador` (
`dni` INT NOT NULL,
`direccion` VARCHAR(140) NOT NULL,
`nombre` VARCHAR(45) NOT NULL,
`apellidos` VARCHAR(140) NOT NULL,
`fecha_nacimiento` DATE NOT NULL,
`fecha_contrato` DATE NOT NULL,
`titulacion` VARCHAR(140) NULL,
`nombre_depto` VARCHAR(45) NOT NULL,
PRIMARY KEY (`dni`),
INDEX `nombre_depto_idx` (`nombre_depto` ASC),
CONSTRAINT `nombre_depto`
FOREIGN KEY (`nombre_depto`)
REFERENCES `BDA`.`Departamento` (`nombre_depto`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`Capitulo_Contable`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Capitulo_Contable` (
`anho_contable` INT NOT NULL,
`numero_ug` INT NOT NULL,
`debe` DOUBLE NOT NULL,
`haber` DOUBLE NOT NULL,
PRIMARY KEY (`anho_contable`),
INDEX `numero_ug_idx` (`numero_ug` ASC),
CONSTRAINT `numero_ug`
FOREIGN KEY (`numero_ug`)
REFERENCES `BDA`.`UG` (`numero_ug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`UG`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`UG` (
`numero_ug` INT NOT NULL,
`nombre` VARCHAR(45) NOT NULL,
`segunda_firma_autorizada` VARCHAR(45) NOT NULL,
`fecha_creacion` DATE NOT NULL,
`nombre_depto` VARCHAR(140) NOT NULL,
`dni` INT NOT NULL,
`anho_contable` INT NOT NULL,
PRIMARY KEY (`numero_ug`),
INDEX `nombre_depto_idx` (`nombre_depto` ASC),
INDEX `dni_idx` (`dni` ASC),
INDEX `anho_contable_idx` (`anho_contable` ASC),
CONSTRAINT `nombre_depto`
FOREIGN KEY (`nombre_depto`)
REFERENCES `BDA`.`Departamento` (`nombre_depto`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `dni`
FOREIGN KEY (`dni`)
REFERENCES `BDA`.`Trabajador` (`dni`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `anho_contable`
FOREIGN KEY (`anho_contable`)
REFERENCES `BDA`.`Capitulo_Contable` (`anho_contable`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`Cliente`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Cliente` (
`cif_cliente` INT NOT NULL,
`nombre_cliente` VARCHAR(140) NOT NULL,
PRIMARY KEY (`cif_cliente`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`Ingreso`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Ingreso` (
`id` INT NOT NULL,
`concepto` VARCHAR(45) NOT NULL,
`importe` DOUBLE NOT NULL,
`fecha` DATE NOT NULL,
`cif_cliente` INT NOT NULL,
`numero_ug` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `cif_cliente_idx` (`cif_cliente` ASC),
INDEX `numero_ug_idx` (`numero_ug` ASC),
CONSTRAINT `cif_cliente`
FOREIGN KEY (`cif_cliente`)
REFERENCES `BDA`.`Cliente` (`cif_cliente`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `numero_ug`
FOREIGN KEY (`numero_ug`)
REFERENCES `BDA`.`UG` (`numero_ug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`Proveedor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Proveedor` (
`cif_proveedor` INT NOT NULL,
`nombre_proveedor` VARCHAR(140) NOT NULL,
PRIMARY KEY (`cif_proveedor`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `BDA`.`Gasto`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BDA`.`Gasto` (
`id` INT NOT NULL,
`concepto` VARCHAR(45) NOT NULL,
`importe` DOUBLE NOT NULL,
`fecha` DATE NOT NULL,
`factura` INT NOT NULL,
`cif_proveedor` INT NOT NULL,
`numero_ug` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `cif_proveedor_idx` (`cif_proveedor` ASC),
INDEX `numero_ug_idx` (`numero_ug` ASC),
CONSTRAINT `cif_proveedor`
FOREIGN KEY (`cif_proveedor`)
REFERENCES `BDA`.`Proveedor` (`cif_proveedor`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `numero_ug`
FOREIGN KEY (`numero_ug`)
REFERENCES `BDA`.`UG` (`numero_ug`)
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;
Thanks for your advices.
As Kevin Horgan said, the problem was foreign keys, which can not be repeated.
Thank you.