我的SQL重复密钥

时间:2017-09-30 14:10:41

标签: mysql sql database

我正在使用mysql创建一个数据库,我遇到了重复密钥的问题,我没有找到错误,但我知道哪个是表错误,我认为问题出在FK's的“historiaLaboral”表中SP是这样的:

我正在使用mysql workbench我想建议你如何改进模型

    -- 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;

我基于这个模型

enter image description here

感谢您的帮助!

0 个答案:

没有答案