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;
Related
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've created a model and forward engineered to SQL text for importing. When I start the data import to an existing connection, I receive the following error,
ERROR 1215 (HY000) at line 76: Cannot add foreign key constraint
I've checked for the data type of the parent and the child and etc but not sure where the error comes from..
Using MySQLWorkbench 6.3 with MySQL 5.7
Line 76 is the beginning of the following extract,
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Expense` (
`idExpense` INT NOT NULL,
`Date` DATETIME NULL,
`Description` VARCHAR(100) NULL,
`Amount` DOUBLE NULL,
`Billable` TINYINT(1) NULL,
`Job_idJob` INT NOT NULL,
`Job_idClient` INT NOT NULL,
`Task_idTask` INT NOT NULL,
PRIMARY KEY (`idExpense`),
INDEX `fk_Expense_Job1_idx` (`Job_idJob` ASC, `Job_idClient` ASC),
INDEX `fk_Expense_Task1_idx` (`Task_idTask` ASC),
CONSTRAINT `fk_Expense_Job1`
FOREIGN KEY (`Job_idJob` , `Job_idClient`)
REFERENCES `CostTrackerDB`.`Job` (`idJob` , `Client_idClient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Expense_Task1`
FOREIGN KEY (`Task_idTask`)
REFERENCES `CostTrackerDB`.`Task` (`idTask`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Whole SQL Text,
-- MySQL Script generated by MySQL Workbench
-- 04/11/16 12:31:19
-- 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 CostTrackerDB
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema CostTrackerDB
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `CostTrackerDB` DEFAULT CHARACTER SET utf8 ;
USE `CostTrackerDB` ;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`Staff`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Staff` (
`idStaff` INT NOT NULL,
`Name` VARCHAR(45) NULL,
`Role` VARCHAR(45) NULL,
PRIMARY KEY (`idStaff`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`Client`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Client` (
`idClient` INT NOT NULL,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`idClient`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`Job`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Job` (
`idJob` INT NOT NULL,
`Number` VARCHAR(45) NULL,
`Title` VARCHAR(100) NULL,
`State` VARCHAR(45) NULL,
`Start` DATETIME NULL,
`Due` DATETIME NULL,
`Client_idClient` INT NOT NULL,
PRIMARY KEY (`idJob`),
INDEX `fk_Job_Client1_idx` (`Client_idClient` ASC),
CONSTRAINT `fk_Job_Client1`
FOREIGN KEY (`Client_idClient`)
REFERENCES `CostTrackerDB`.`Client` (`idClient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`Task`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Task` (
`idTask` INT NOT NULL,
`Title` VARCHAR(100) NULL,
`idStaff` INT NULL,
PRIMARY KEY (`idTask`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`Expense`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Expense` (
`idExpense` INT NOT NULL,
`Date` DATETIME NULL,
`Description` VARCHAR(100) NULL,
`Amount` DOUBLE NULL,
`Billable` TINYINT(1) NULL,
`Job_idJob` INT NOT NULL,
`Job_idClient` INT NOT NULL,
`Task_idTask` INT NOT NULL,
PRIMARY KEY (`idExpense`),
INDEX `fk_Expense_Job1_idx` (`Job_idJob` ASC, `Job_idClient` ASC),
INDEX `fk_Expense_Task1_idx` (`Task_idTask` ASC),
CONSTRAINT `fk_Expense_Job1`
FOREIGN KEY (`Job_idJob` , `Job_idClient`)
REFERENCES `CostTrackerDB`.`Job` (`idJob` , `Client_idClient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Expense_Task1`
FOREIGN KEY (`Task_idTask`)
REFERENCES `CostTrackerDB`.`Task` (`idTask`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`StaffAllocation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`StaffAllocation` (
`Job_idJob` INT NOT NULL,
`Staff_idStaff` INT NOT NULL,
`Task_idTask` INT NOT NULL,
`Rate` DOUBLE NULL,
`Role` VARCHAR(45) NULL,
`Hours` DOUBLE NULL,
`Week` DATETIME NULL,
PRIMARY KEY (`Job_idJob`, `Staff_idStaff`, `Task_idTask`),
INDEX `fk_Job_has_Staff_Staff1_idx` (`Staff_idStaff` ASC),
INDEX `fk_Job_has_Staff_Job1_idx` (`Job_idJob` ASC),
INDEX `fk_StaffAllocation_Task1_idx` (`Task_idTask` ASC),
CONSTRAINT `fk_Job_has_Staff_Job1`
FOREIGN KEY (`Job_idJob`)
REFERENCES `CostTrackerDB`.`Job` (`idJob`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Job_has_Staff_Staff1`
FOREIGN KEY (`Staff_idStaff`)
REFERENCES `CostTrackerDB`.`Staff` (`idStaff`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_StaffAllocation_Task1`
FOREIGN KEY (`Task_idTask`)
REFERENCES `CostTrackerDB`.`Task` (`idTask`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`Timesheet`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Timesheet` (
`Task_idTask` INT NOT NULL,
`Staff_idStaff` INT NOT NULL,
`Time` DATETIME NULL,
`Billable` TINYINT(1) NULL,
`hours` DATETIME NULL,
PRIMARY KEY (`Task_idTask`, `Staff_idStaff`),
INDEX `fk_Task_has_Staff_Staff1_idx` (`Staff_idStaff` ASC),
INDEX `fk_Task_has_Staff_Task1_idx` (`Task_idTask` ASC),
CONSTRAINT `fk_Task_has_Staff_Task1`
FOREIGN KEY (`Task_idTask`)
REFERENCES `CostTrackerDB`.`Task` (`idTask`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Task_has_Staff_Staff1`
FOREIGN KEY (`Staff_idStaff`)
REFERENCES `CostTrackerDB`.`Staff` (`idStaff`)
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;
Try:
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Expense` (
`idExpense` INT NOT NULL,
`Date` DATETIME NULL,
`Description` VARCHAR(100) NULL,
`Amount` DOUBLE NULL,
`Billable` TINYINT(1) NULL,
`Job_idJob` INT NOT NULL,
`Job_idClient` INT NOT NULL,
`Task_idTask` INT NOT NULL,
PRIMARY KEY (`idExpense`),
INDEX `fk_Expense_Job1_idx` (`Job_idJob` ASC, `Job_idClient` ASC),
INDEX `fk_Expense_Task1_idx` (`Task_idTask` ASC),
/*CONSTRAINT `fk_Expense_Job1`
FOREIGN KEY (`Job_idJob` , `Job_idClient`)
REFERENCES `CostTrackerDB`.`Job` (`idJob` , `Client_idClient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,*/
CONSTRAINT `fk_Expense_Job1`
FOREIGN KEY (`Job_idJob`)
REFERENCES `CostTrackerDB`.`Job` (`idJob`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Expense_Job2`
FOREIGN KEY (`Job_idClient`)
REFERENCES `CostTrackerDB`.`Job` (`Client_idClient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Expense_Task1`
FOREIGN KEY (`Task_idTask`)
REFERENCES `CostTrackerDB`.`Task` (`idTask`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
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';
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.