[MySQL] Foreign Key 포함 스키마 설정하기

아현·2022년 4월 28일
1

Database

목록 보기
4/8
post-custom-banner

1. MySQL Workbench 로 DB 설계하기

참고




2. 스키마 설계하기







3. 적용시키기



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

-- -----------------------------------------------------
-- Schema msa
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `msa` DEFAULT CHARACTER SET utf8 ;
-- -----------------------------------------------------
-- Schema pro
-- -----------------------------------------------------

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

-- -----------------------------------------------------
-- Table `msa`.`ranking`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `msa`.`ranking` (
  `Bidx` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NULL,
  `score` INT NULL,
  `bonus` INT NULL,
  `total` INT NULL,
  PRIMARY KEY (`Bidx`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `msa`.`question`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `msa`.`question` (
  `idx` INT NOT NULL,
  `a` INT NULL,
  `b` INT NULL,
  `result` INT NULL,
  `Bidx` INT NULL,
  PRIMARY KEY (`idx`),
  INDEX `f_Bidx_idx` (`Bidx` ASC) VISIBLE,
  CONSTRAINT `f_Bidx`
    FOREIGN KEY (`Bidx`)
    REFERENCES `msa`.`ranking` (`Bidx`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

USE `pro` ;

-- -----------------------------------------------------
-- Table `pro`.`inform`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `pro`.`inform` (
  `idx` INT NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(45) NULL DEFAULT NULL,
  `factorA` INT NULL DEFAULT NULL,
  `factorB` INT NULL DEFAULT NULL,
  `checkAttempt` TINYINT NULL DEFAULT NULL,
  PRIMARY KEY (`idx`))
ENGINE = InnoDB
AUTO_INCREMENT = 50
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;




profile
For the sake of someone who studies computer science
post-custom-banner

0개의 댓글