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
Related
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;
I'm trying to forward engineering my schema with MySQL Workbench but this error appears. I'm sure that the foreign key refers to the primary key of the parent table and their are both VARCHAR(45).
This is my schema:
-- MySQL Workbench Forward Engineering
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema PiscineRoma
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema PiscineRoma
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `PiscineRoma` ;
USE `PiscineRoma` ;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Piscina`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Piscina` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Piscina` (
`Nome` VARCHAR(45) NOT NULL,
`NumeroDiTelefono` VARCHAR(45) NOT NULL,
`Indirizzo` VARCHAR(45) NOT NULL,
`NomeResponsabile` VARCHAR(45) NOT NULL,
`DataInizioDisponibilitàVascaEsterna` DATE NULL,
`DataFineDisponibilitàVascaEsterna` DATE NULL,
PRIMARY KEY (`Nome`),
UNIQUE INDEX `NumeroDiTelefono_UNIQUE` (`NumeroDiTelefono` ASC),
UNIQUE INDEX `Indirizzo_UNIQUE` (`Indirizzo` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Persona`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Persona` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Persona` (
`CF` VARCHAR(16) NOT NULL,
`Nome` VARCHAR(45) NOT NULL,
`Cognome` VARCHAR(45) NOT NULL,
`Indirizzo` VARCHAR(45) NOT NULL,
`Età` INT NOT NULL,
`Telefono` VARCHAR(20) NULL,
`Cellulare` VARCHAR(20) NULL,
`IndirizzoEMail` VARCHAR(45) NULL,
PRIMARY KEY (`CF`),
UNIQUE INDEX `Telefono_UNIQUE` (`Telefono` ASC),
UNIQUE INDEX `Cellulare_UNIQUE` (`Cellulare` ASC),
UNIQUE INDEX `IndirizzoEMail_UNIQUE` (`IndirizzoEMail` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`IngressoSingolo`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`IngressoSingolo` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`IngressoSingolo` (
`NomePiscina` VARCHAR(45) NOT NULL,
`Persona_CF` VARCHAR(16) NOT NULL,
`DataIngresso` DATETIME NOT NULL,
PRIMARY KEY (`NomePiscina`, `Persona_CF`),
INDEX `fk_Piscina_has_PersonaNonIscritta_Piscina_idx` (`NomePiscina` ASC),
INDEX `fk_IngressoSingolo_Persona1_idx` (`Persona_CF` ASC),
CONSTRAINT `fk_Piscina_has_PersonaNonIscritta_Piscina`
FOREIGN KEY (`NomePiscina`)
REFERENCES `PiscineRoma`.`Piscina` (`Nome`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_IngressoSingolo_Persona1`
FOREIGN KEY (`Persona_CF`)
REFERENCES `PiscineRoma`.`Persona` (`CF`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Insegnante`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Insegnante` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Insegnante` (
`CF` VARCHAR(16) NOT NULL,
`Nome` VARCHAR(45) NOT NULL,
`Cognome` VARCHAR(45) NOT NULL,
`Telefono` VARCHAR(20) NULL,
PRIMARY KEY (`CF`),
UNIQUE INDEX `Telefono_UNIQUE` (`Telefono` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Qualifica`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Qualifica` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Qualifica` (
`Nome` VARCHAR(45) NOT NULL,
PRIMARY KEY (`Nome`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Possiede`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Possiede` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Possiede` (
`Insegnante` VARCHAR(16) NOT NULL,
`Qualifica` VARCHAR(45) NOT NULL,
PRIMARY KEY (`Insegnante`, `Qualifica`),
INDEX `fk_Insegnante_has_Qualifica_Qualifica1_idx` (`Qualifica` ASC),
INDEX `fk_Insegnante_has_Qualifica_Insegnante1_idx` (`Insegnante` ASC),
CONSTRAINT `fk_Insegnante_has_Qualifica_Insegnante1`
FOREIGN KEY (`Insegnante`)
REFERENCES `PiscineRoma`.`Insegnante` (`CF`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Insegnante_has_Qualifica_Qualifica1`
FOREIGN KEY (`Qualifica`)
REFERENCES `PiscineRoma`.`Qualifica` (`Nome`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Impiego`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Impiego` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Impiego` (
`Codice` INT NOT NULL AUTO_INCREMENT,
`Inizio` DATE NOT NULL,
`Fine` DATE NULL,
`Tipo` VARCHAR(45) NOT NULL DEFAULT 'impiego_corrente',
`NomePiscina` VARCHAR(45) NOT NULL,
`Insegnante` VARCHAR(16) NOT NULL,
PRIMARY KEY (`Codice`),
INDEX `fk_Impiego_Piscina1_idx` (`NomePiscina` ASC),
INDEX `fk_Impiego_Insegnante1_idx` (`Insegnante` ASC),
CONSTRAINT `fk_Impiego_Piscina1`
FOREIGN KEY (`NomePiscina`)
REFERENCES `PiscineRoma`.`Piscina` (`Nome`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_Impiego_Insegnante1`
FOREIGN KEY (`Insegnante`)
REFERENCES `PiscineRoma`.`Insegnante` (`CF`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Corso`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Corso` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Corso` (
`NomeAttivita` VARCHAR(45) NOT NULL,
`NomePiscina` VARCHAR(45) NOT NULL,
`Costo` SMALLINT(2) NOT NULL,
`NumeroMinimoDiPartecipanti` SMALLINT(2) NOT NULL,
`NumeroMassimoDiPartecipanti` SMALLINT(2) NOT NULL,
PRIMARY KEY (`NomeAttivita`, `NomePiscina`),
INDEX `fk_Corso_Piscina1_idx` (`NomePiscina` ASC),
CONSTRAINT `fk_Corso_Piscina1`
FOREIGN KEY (`NomePiscina`)
REFERENCES `PiscineRoma`.`Piscina` (`Nome`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB
PACK_KEYS = DEFAULT
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Lezione`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Lezione` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Lezione` (
`Codice` INT NOT NULL AUTO_INCREMENT,
`NomeAttivitaCorso` VARCHAR(45) NOT NULL,
`NomePiscina` VARCHAR(45) NOT NULL,
`Insegnante` VARCHAR(16) NOT NULL,
`Dat` DATETIME NOT NULL,
`Numero` INT NOT NULL,
PRIMARY KEY (`Codice`),
INDEX `fk_Lezione_Insegnante1_idx` (`Insegnante` ASC),
CONSTRAINT `fk_Lezione_Corso1`
FOREIGN KEY (`NomeAttivitaCorso` , `NomePiscina`)
REFERENCES `PiscineRoma`.`Corso` (`NomeAttivita` , `NomePiscina`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_Lezione_Insegnante1`
FOREIGN KEY (`Insegnante`)
REFERENCES `PiscineRoma`.`Insegnante` (`CF`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Iscrizione`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Iscrizione` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Iscrizione` (
`Codice` INT NOT NULL AUTO_INCREMENT,
`DataIscrizione` DATE NOT NULL,
`CodiceCertificato` INT NULL,
`DataCertificato` DATE NULL,
`Medico` VARCHAR(45) NULL,
`NomeAttivitaCorso` VARCHAR(45) NOT NULL,
`NomePiscina` VARCHAR(45) NOT NULL,
`PersonaIscritta` VARCHAR(16) NOT NULL,
PRIMARY KEY (`Codice`),
INDEX `fk_Iscrizione_Corso1_idx` (`NomeAttivitaCorso` ASC, `NomePiscina` ASC),
INDEX `fk_Iscrizione_PersonaIscritta1_idx` (`PersonaIscritta` ASC),
UNIQUE INDEX `CodiceCertificato_UNIQUE` (`CodiceCertificato` ASC),
CONSTRAINT `fk_Iscrizione_Corso1`
FOREIGN KEY (`NomeAttivitaCorso` , `NomePiscina`)
REFERENCES `PiscineRoma`.`Corso` (`NomeAttivita` , `NomePiscina`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_Iscrizione_PersonaIscritta1`
FOREIGN KEY (`PersonaIscritta`)
REFERENCES `PiscineRoma`.`Persona` (`CF`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
USE `PiscineRoma` ;
The message log from MySQL Workbench tells that error is in the PiscineRoma.Corso.
I also tryed to run the command SHOW ENGINE INNODB STATUS and under the label the error is:
2019-02-14 19:52:03 0x7f9fa9700700 Error in foreign key constraint of table PiscineRoma/Iscrizione:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
CONSTRAINT `fk_Iscrizione_Corso1` FOREIGN KEY (`NomeAttivita`, `NomePiscina`) REFERENCES `Corso` (`NomeAttività`, `NomePiscina`) ON DELETE NO ACTION ON UPDATE CASCADE
The index in the foreign key in table is fk_Iscrizione_Corso1_idx
Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html for correct foreign key definition.
Thank you all for answers.
I'm creating a database with mysql and I have a problem with duplicated key, I don't find the error but I know which is the table error, I think the problem is with the FK's in "historiaLaboral" table the SP is this:
Im using mysql workbench I would like to you advising me how improve the model
-- 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 SGD
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema SGD
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `SGD` DEFAULT CHARACTER SET utf8 ;
USE `SGD` ;
-- -----------------------------------------------------
-- Table `SGD`.`tipoIdentificación`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`tipoIdentificación` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`empresa`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`empresa` (
`id` INT NOT NULL,
`razonSocial` VARCHAR(45) NOT NULL,
`NIT` VARCHAR(45) NOT NULL,
`telefono` VARCHAR(45) NOT NULL,
`direccion` VARCHAR(45) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`empleado`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`empleado` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombres` VARCHAR(45) NOT NULL,
`apellidos` VARCHAR(45) NOT NULL,
`id_tipoDocumento` INT NOT NULL,
`numeroDocumento` INT(20) NULL,
`aspiracionSalarial` DOUBLE NOT NULL,
`telefono` INT NOT NULL,
`direccion` VARCHAR(30) NOT NULL,
`id_empresa` INT NOT NULL,
`genero` ENUM('F', 'M') NOT NULL,
`aspirante` BIT(1) NULL,
PRIMARY KEY (`id`),
INDEX `id_tipoDocumento_idx` (`id_tipoDocumento` ASC),
INDEX `id_empresa_idx` (`id_empresa` ASC),
CONSTRAINT `id_tipoDocumento`
FOREIGN KEY (`id_tipoDocumento`)
REFERENCES `SGD`.`tipoIdentificación` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_empresa`
FOREIGN KEY (`id_empresa`)
REFERENCES `SGD`.`empresa` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`rol`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`rol` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`usuario`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`usuario` (
`id` INT NOT NULL AUTO_INCREMENT,
`usuario` VARCHAR(45) NOT NULL,
`contrasena` VARCHAR(45) NOT NULL,
`id_rol` INT NOT NULL,
`id_empleado` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_empleado_idx` (`id_empleado` ASC),
INDEX `id_rol_idx` (`id_rol` ASC),
CONSTRAINT `id_empleado`
FOREIGN KEY (`id_empleado`)
REFERENCES `SGD`.`empleado` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_rol`
FOREIGN KEY (`id_rol`)
REFERENCES `SGD`.`rol` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`historiaLaboral`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`historiaLaboral` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_empleado` INT NOT NULL,
`id_usuarioRegistra` INT NOT NULL,
`fechaIngreso` DATE NOT NULL,
`fechaModificacion` DATE NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_empleado_idx` (`id_empleado` ASC),
INDEX `id_usuarioRegistra_idx` (`id_usuarioRegistra` ASC),
CONSTRAINT `id_empleado`
FOREIGN KEY (`id_empleado`)
REFERENCES `SGD`.`empleado` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_usuarioRegistra`
FOREIGN KEY (`id_usuarioRegistra`)
REFERENCES `SGD`.`usuario` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`referencia`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`referencia` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombres` VARCHAR(45) NOT NULL,
`apellidos` VARCHAR(45) NOT NULL,
`relacion` VARCHAR(45) NOT NULL,
`telefono` VARCHAR(45) NOT NULL,
`telefono_2` VARCHAR(45) NULL,
`id_HLaboral` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_Empleado_idx` (`id_HLaboral` ASC),
CONSTRAINT `id_HLaboral`
FOREIGN KEY (`id_HLaboral`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`experienciaLaboral`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`experienciaLaboral` (
`id` INT NOT NULL AUTO_INCREMENT,
`empresa` VARCHAR(45) NOT NULL,
`fechaInicio` DATE NOT NULL,
`fechaFin` DATE NOT NULL,
`motivoRetiro` VARCHAR(45) NOT NULL,
`id_HLaboral` INT NOT NULL,
`certificado` BIT(1) NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_Empleado_idx` (`id_HLaboral` ASC),
CONSTRAINT `id_Empleado`
FOREIGN KEY (`id_HLaboral`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`estudios`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`estudios` (
`id` INT NOT NULL AUTO_INCREMENT,
`institución` VARCHAR(45) NOT NULL,
`fechaInicio` DATE NOT NULL,
`fechaFin` DATE NOT NULL,
`certificado` BIT(1) NOT NULL DEFAULT 0,
`id_HLaboral` INT NOT NULL,
`id_tipoEstudio` INT NULL,
PRIMARY KEY (`id`),
INDEX `id_HLaboral_idx` (`id_tipoEstudio` ASC),
CONSTRAINT `id_HLaboral`
FOREIGN KEY (`id_tipoEstudio`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`serie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`serie` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`soporte`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`soporte` (
`id` INT NOT NULL,
`papel` BIT(1) NOT NULL,
`digital` BIT(1) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`retencion`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`retencion` (
`id` INT NOT NULL AUTO_INCREMENT,
`archivoGestion` INT NOT NULL,
`archivoCentral` INT NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`dispFinal`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`dispFinal` (
`id` INT NOT NULL AUTO_INCREMENT,
`conservacionTotal` BIT(1) NULL,
`eliminacion` BIT(1) NULL,
`digitalizacion` BIT(1) NULL,
`seleccion` BIT(1) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`TRD`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`TRD` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_serie` INT NOT NULL,
`detalle` VARCHAR(2000) NOT NULL,
`id_soporte` INT NOT NULL,
`id_retencion` INT NOT NULL,
`id_dispFinal` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_serie_idx` (`id_serie` ASC),
INDEX `id_soporte_idx` (`id_soporte` ASC),
INDEX `id_retencion_idx` (`id_retencion` ASC),
INDEX `id_dispFinal_idx` (`id_dispFinal` ASC),
CONSTRAINT `id_serie`
FOREIGN KEY (`id_serie`)
REFERENCES `SGD`.`serie` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_soporte`
FOREIGN KEY (`id_soporte`)
REFERENCES `SGD`.`soporte` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_retencion`
FOREIGN KEY (`id_retencion`)
REFERENCES `SGD`.`retencion` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_dispFinal`
FOREIGN KEY (`id_dispFinal`)
REFERENCES `SGD`.`dispFinal` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`documento`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`documento` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_HLaboral` INT NOT NULL,
`id_TRD` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_HLaboral_idx` (`id_HLaboral` ASC),
INDEX `id_TRD_idx` (`id_TRD` ASC),
CONSTRAINT `id_HLaboral`
FOREIGN KEY (`id_HLaboral`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_TRD`
FOREIGN KEY (`id_TRD`)
REFERENCES `SGD`.`TRD` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`ubicacion`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`ubicacion` (
`id` INT NOT NULL AUTO_INCREMENT,
`analquel` VARCHAR(45) NOT NULL,
`estante` VARCHAR(45) NOT NULL,
`id_documento` INT NULL,
PRIMARY KEY (`id`),
INDEX `id_documento_idx` (`id_documento` ASC),
CONSTRAINT `id_documento`
FOREIGN KEY (`id_documento`)
REFERENCES `SGD`.`documento` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`subserie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`subserie` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_serie` INT NOT NULL,
`nombre` VARCHAR(45) NOT NULL,
`id_subSerie` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_serie_idx` (`id_subSerie` ASC),
CONSTRAINT `id_subSerie`
FOREIGN KEY (`id_subSerie`)
REFERENCES `SGD`.`serie` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`permisos`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`permisos` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
`id_rol` INT NOT NULL,
`acceso` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_rol_idx` (`id_rol` ASC),
CONSTRAINT `id_rol`
FOREIGN KEY (`id_rol`)
REFERENCES `SGD`.`rol` (`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;
I'm based on this model
Thanks for helping me!
I'm trying to create these tables but I'm getting Error 1022 when creating the table cadastro_fornecedor.
I don't know where to start to solve this issue so any kind of help is welcome.
Here is the .sql:
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 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`administrador`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`administrador` (
`id_administrador` INT NOT NULL,
`nome` VARCHAR(45) NULL,
`senha` VARCHAR(45) NULL,
`CPF` INT NULL,
`endereço` VARCHAR(45) NULL,
`data_nascimento` DATE NULL,
PRIMARY KEY (`id_administrador`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`cadastro_usuario`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`cadastro_usuario` (
`id_cadastro` INT NOT NULL AUTO_INCREMENT,
`data_cadastro` DATETIME NOT NULL,
`id_administrador` INT NULL,
`usuario` INT NULL,
PRIMARY KEY (`id_cadastro`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `matricula_idx` (`usuario` ASC),
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `matricula`
FOREIGN KEY (`usuario`)
REFERENCES `mydb`.`usuario_cadastrado` (`matricula`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`usuario_cadastrado`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`usuario_cadastrado` (
`matricula` INT NOT NULL,
`nome` VARCHAR(45) NULL,
`endereco` VARCHAR(45) NULL,
`senha` VARCHAR(45) NULL,
`nacionalidade` VARCHAR(45) NULL DEFAULT 'brasileiro',
`naturalidade` VARCHAR(45) NULL,
`sexo` CHAR NULL,
`data_nascimento` DATE NULL,
`CPF` INT NULL,
`nome_pai` VARCHAR(45) NULL,
`nome_mae` VARCHAR(45) NULL,
`telefone_celular` VARCHAR(15) NULL,
`telefone_fixo` VARCHAR(15) NULL,
`id_cadastro` INT NULL,
PRIMARY KEY (`matricula`),
INDEX `id_cadastro_idx` (`id_cadastro` ASC),
CONSTRAINT `id_cadastro`
FOREIGN KEY (`id_cadastro`)
REFERENCES `mydb`.`cadastro_usuario` (`id_cadastro`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`obra`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`obra` (
`ISBN` VARCHAR(20) NOT NULL,
`titulo` VARCHAR(45) NULL,
`autor` VARCHAR(45) NULL,
`ano` YEAR NULL,
`editora` VARCHAR(45) NULL,
`categoria` VARCHAR(45) NULL,
`assunto` VARCHAR(45) NULL,
`descricao` VARCHAR(45) NULL,
PRIMARY KEY (`ISBN`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`fornecedores`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`fornecedores` (
`CNPJ` VARCHAR(45) NOT NULL,
`nome` VARCHAR(45) NOT NULL,
`endereco` VARCHAR(45) NULL,
`telefone` VARCHAR(45) NULL,
PRIMARY KEY (`CNPJ`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`exemplar`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`exemplar` (
`codigo_exemplar` INT NOT NULL,
`data_aquisicao` DATE NULL,
`edição` INT NULL,
`volume` INT NULL,
`paginas` INT NULL,
`ISBN` VARCHAR(20) NULL,
`CNPJ` VARCHAR(45) NULL,
PRIMARY KEY (`codigo_exemplar`),
INDEX `ISBN_idx` (`ISBN` ASC),
INDEX `CNPJ_idx` (`CNPJ` ASC),
CONSTRAINT `ISBN`
FOREIGN KEY (`ISBN`)
REFERENCES `mydb`.`obra` (`ISBN`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `CNPJ`
FOREIGN KEY (`CNPJ`)
REFERENCES `mydb`.`fornecedores` (`CNPJ`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`cadastro_fornecedor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`cadastro_fornecedor` (
`id_cadastro` INT NOT NULL AUTO_INCREMENT,
`data` DATETIME NULL,
`id_administrador` INT NULL,
`CNPJ` VARCHAR(45) NULL,
PRIMARY KEY (`id_cadastro`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `CNPJ_idx` (`CNPJ` ASC),
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `CNPJ`
FOREIGN KEY (`CNPJ`)
REFERENCES `mydb`.`fornecedores` (`CNPJ`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`emprestimo`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`emprestimo` (
`id_emprestimo` INT NOT NULL AUTO_INCREMENT,
`data_emprestimo` DATE NULL,
`data_devolucao_prevista` DATE NULL,
`matricula` INT NULL,
`id_administrador` INT NULL,
`codigo_exemplar` INT NULL,
PRIMARY KEY (`id_emprestimo`),
INDEX `matricula_idx` (`matricula` ASC),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `codigo_exemplar_idx` (`codigo_exemplar` ASC),
CONSTRAINT `matricula`
FOREIGN KEY (`matricula`)
REFERENCES `mydb`.`usuario_cadastrado` (`matricula`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `codigo_exemplar`
FOREIGN KEY (`codigo_exemplar`)
REFERENCES `mydb`.`exemplar` (`codigo_exemplar`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`devolucao`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`devolucao` (
`id_devolucao` INT NOT NULL,
`data_devolucao` DATE NULL,
`multa` INT NULL,
`matricula` INT NULL,
`id_administrador` INT NULL,
PRIMARY KEY (`id_devolucao`),
INDEX `matricula_idx` (`matricula` ASC),
INDEX `id_administrador_idx` (`id_administrador` ASC),
CONSTRAINT `matricula`
FOREIGN KEY (`matricula`)
REFERENCES `mydb`.`usuario_cadastrado` (`matricula`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`exclusao`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`exclusao` (
`id_exclusao` INT NOT NULL AUTO_INCREMENT,
`data_exclusao` DATE NULL,
`id_administrador` INT NULL,
`codigo_exemplar` INT NULL,
PRIMARY KEY (`id_exclusao`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `codigo_exemplar_idx` (`codigo_exemplar` ASC),
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `codigo_exemplar`
FOREIGN KEY (`codigo_exemplar`)
REFERENCES `mydb`.`exemplar` (`codigo_exemplar`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`registro`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`registro` (
`id_registro` INT NOT NULL AUTO_INCREMENT,
`data_registro` DATE NULL,
`id_administrador` INT NULL,
`codigo_exemplar` INT NULL,
PRIMARY KEY (`id_registro`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `codigo_exemplar_idx` (`codigo_exemplar` ASC),
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `codigo_exemplar`
FOREIGN KEY (`codigo_exemplar`)
REFERENCES `mydb`.`exemplar` (`codigo_exemplar`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`reserva`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`reserva` (
`id_reserva` INT NOT NULL,
`data_reserva` DATE NULL,
`matricula` INT NULL,
`ISBN` VARCHAR(20) NULL,
PRIMARY KEY (`id_reserva`),
INDEX `matricula_idx` (`matricula` ASC),
INDEX `ISBN_idx` (`ISBN` ASC),
CONSTRAINT `matricula`
FOREIGN KEY (`matricula`)
REFERENCES `mydb`.`usuario_cadastrado` (`matricula`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ISBN`
FOREIGN KEY (`ISBN`)
REFERENCES `mydb`.`obra` (`ISBN`)
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 appreciate any kind of help.
Thanks.
You're reusing existing constraint names (id_administrador is already used as a constraint name in the table cadastro_usuario and CNPJ is used in the table exemplar), you'll have to give your constraints unique names (here I renamed them to cf_id_administrador/CF_CNPJ);
-- -----------------------------------------------------
-- Table `mydb`.`cadastro_fornecedor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`cadastro_fornecedor` (
`id_cadastro` INT NOT NULL AUTO_INCREMENT,
`data` DATETIME NULL,
`id_administrador` INT NULL,
`CNPJ` VARCHAR(45) NULL,
PRIMARY KEY (`id_cadastro`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `CNPJ_idx` (`CNPJ` ASC),
CONSTRAINT `cf_id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `CF_CNPJ`
FOREIGN KEY (`CNPJ`)
REFERENCES `mydb`.`fornecedores` (`CNPJ`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
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.