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

CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`Clientes`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`Clientes` (
  `idCliente` INT NOT NULL AUTO_INCREMENT ,
  `Nombre` VARCHAR(45) NULL ,
  PRIMARY KEY (`idCliente`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Operaciones`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`Operaciones` (
  `idOperacion` INT NOT NULL AUTO_INCREMENT ,
  `FechaRegistro` TIMESTAMP NOT NULL ,
  `Cliente` INT NOT NULL ,
  PRIMARY KEY (`idOperacion`) ,
  INDEX `fk_Operaciones_Clientes` (`Cliente` ASC) ,
  CONSTRAINT `fk_Operaciones_Clientes`
    FOREIGN KEY (`Cliente` )
    REFERENCES `mydb`.`Clientes` (`idCliente` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`RoutingOrders`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`RoutingOrders` (
  `idRoutingOrder` INT NOT NULL AUTO_INCREMENT ,
  `Operacion` INT NOT NULL ,
  `Cliente` INT NOT NULL ,
  `FechaRegistro` TIMESTAMP NOT NULL ,
  PRIMARY KEY (`idRoutingOrder`) ,
  UNIQUE INDEX `fk_RoutingOrders_Operaciones1` (`Cliente` ASC, `Operacion` ASC) ,
  CONSTRAINT `fk_RoutingOrders_Operaciones1`
    FOREIGN KEY (`Cliente` , `Operacion` )
    REFERENCES `mydb`.`Operaciones` (`Cliente` , `idOperacion` )
    ON DELETE CASCADE
    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;
