-- MySQL Script generated by MySQL Workbench
-- mar 22 feb 2022 22:19:28
-- 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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema GWAS_DM
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema GWAS_DM
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `GWAS_DM` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci ;
USE `GWAS_DM` ;

-- -----------------------------------------------------
-- Table `GWAS_DM`.`user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `GWAS_DM`.`user` (
  `user_id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) NOT NULL,
  `email` VARCHAR(50) NOT NULL,
  `affiliation` VARCHAR(100) NOT NULL,
  PRIMARY KEY (`user_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;


-- -----------------------------------------------------
-- Table `GWAS_DM`.`study`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `GWAS_DM`.`study` (
  `study_id` INT NOT NULL AUTO_INCREMENT,
  `pmid` INT NOT NULL,
  `year` INT NOT NULL,
  `population` VARCHAR(50) NULL DEFAULT NULL,
  `N` INT NOT NULL,
  `Ncase` INT NOT NULL,
  `Ncontrol` INT NOT NULL,
  `Nsnps` INT NOT NULL,
  `Nhits` INT NOT NULL,
  `Hsnp` FLOAT(20,10) NULL DEFAULT NULL,
  PRIMARY KEY (`study_id`),
  CONSTRAINT `study_ibfk_1`
    FOREIGN KEY (`study_id`)
    REFERENCES `GWAS_DM`.`user` (`user_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;


-- -----------------------------------------------------
-- Table `GWAS_DM`.`array`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `GWAS_DM`.`array` (
  `array_id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) NOT NULL,
  `suplier` VARCHAR(100) NOT NULL,
  `Nsnps` INT NOT NULL,
  PRIMARY KEY (`array_id`),
  CONSTRAINT `array_ibfk_1`
    FOREIGN KEY (`array_id`)
    REFERENCES `GWAS_DM`.`study` (`study_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;


-- -----------------------------------------------------
-- Table `GWAS_DM`.`variant`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `GWAS_DM`.`variant` (
  `variant_id` INT NOT NULL AUTO_INCREMENT,
  `dbsnp` VARCHAR(20) NOT NULL,
  `chr` INT NOT NULL,
  `bp` VARCHAR(50) NOT NULL,
  `pvalue` FLOAT(20,10) NOT NULL,
  `effect_allele` VARCHAR(20) NOT NULL,
  `other_allele` VARCHAR(50) NULL DEFAULT NULL,
  `maf` FLOAT(20,10) NOT NULL,
  `effect_allele_freq` FLOAT(20,10) NOT NULL,
  `oddsratio` FLOAT(20,10) NOT NULL,
  `beta` FLOAT(20,10) NULL DEFAULT NULL,
  PRIMARY KEY (`variant_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;


-- -----------------------------------------------------
-- Table `GWAS_DM`.`gene`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `GWAS_DM`.`gene` (
  `gene_id` INT NOT NULL AUTO_INCREMENT,
  `ensembl` VARCHAR(50) NOT NULL,
  `symbol` VARCHAR(20) NOT NULL,
  `chr` INT NOT NULL,
  `start` INT NOT NULL,
  `end` INT NOT NULL,
  `strand` INT NOT NULL,
  PRIMARY KEY (`gene_id`),
  CONSTRAINT `gene_ibfk_1`
    FOREIGN KEY (`gene_id`)
    REFERENCES `GWAS_DM`.`variant` (`variant_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;


-- -----------------------------------------------------
-- Table `GWAS_DM`.`trait`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `GWAS_DM`.`trait` (
  `trait_id` INT NOT NULL AUTO_INCREMENT,
  `phenotype_unique` VARCHAR(100) NOT NULL,
  `phenotype_synonims` VARCHAR(100) NULL DEFAULT NULL,
  `chapter_icd10` VARCHAR(100) NOT NULL,
  `subchapter_icd10` VARCHAR(100) NOT NULL,
  PRIMARY KEY (`trait_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;


-- -----------------------------------------------------
-- Table `GWAS_DM`.`trait_has_variant`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `GWAS_DM`.`trait_has_variant` (
  `trait_trait_id` INT NOT NULL,
  `variant_variant_id` INT NOT NULL,
  PRIMARY KEY (`trait_trait_id`, `variant_variant_id`),
  INDEX `fk_trait_has_variant_variant1_idx` (`variant_variant_id` ASC) VISIBLE,
  INDEX `fk_trait_has_variant_trait1_idx` (`trait_trait_id` ASC) VISIBLE,
  CONSTRAINT `fk_trait_has_variant_trait1`
    FOREIGN KEY (`trait_trait_id`)
    REFERENCES `GWAS_DM`.`trait` (`trait_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_trait_has_variant_variant1`
    FOREIGN KEY (`variant_variant_id`)
    REFERENCES `GWAS_DM`.`variant` (`variant_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;


-- -----------------------------------------------------
-- Table `GWAS_DM`.`array_has_variant`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `GWAS_DM`.`array_has_variant` (
  `array_array_id` INT NOT NULL,
  `variant_variant_id` INT NOT NULL,
  PRIMARY KEY (`array_array_id`, `variant_variant_id`),
  INDEX `fk_array_has_variant_variant1_idx` (`variant_variant_id` ASC) VISIBLE,
  INDEX `fk_array_has_variant_array1_idx` (`array_array_id` ASC) VISIBLE,
  CONSTRAINT `fk_array_has_variant_array1`
    FOREIGN KEY (`array_array_id`)
    REFERENCES `GWAS_DM`.`array` (`array_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_array_has_variant_variant1`
    FOREIGN KEY (`variant_variant_id`)
    REFERENCES `GWAS_DM`.`variant` (`variant_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;


-- -----------------------------------------------------
-- Table `GWAS_DM`.`study_has_variant`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `GWAS_DM`.`study_has_variant` (
  `study_study_id` INT NOT NULL,
  `variant_variant_id` INT NOT NULL,
  PRIMARY KEY (`study_study_id`, `variant_variant_id`),
  INDEX `fk_study_has_variant_variant1_idx` (`variant_variant_id` ASC) VISIBLE,
  INDEX `fk_study_has_variant_study1_idx` (`study_study_id` ASC) VISIBLE,
  CONSTRAINT `fk_study_has_variant_study1`
    FOREIGN KEY (`study_study_id`)
    REFERENCES `GWAS_DM`.`study` (`study_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_study_has_variant_variant1`
    FOREIGN KEY (`variant_variant_id`)
    REFERENCES `GWAS_DM`.`variant` (`variant_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
