• 티스토리 홈
  • 프로필사진
    Song hyun
  • 방명록
  • 공지사항
  • 태그
  • 블로그 관리
  • 글 작성
Song hyun
  • 프로필사진
    Song hyun
    • 분류 전체보기 (780)
      • 백준 (0)
      • 영어 (2)
        • Diary (0)
        • Toast Masters (2)
      • 메모 (13)
      • 설치 메뉴얼 (30)
      • Java (178)
      • MySQL (60)
      • JSP (67)
      • Springboot (46)
      • HTML,CSS, JS (71)
        • HTML (8)
        • CSS (12)
        • JavaScript (37)
        • HTML&CSS 스터디 (13)
      • C++ (7)
      • Linux (7)
      • JPA (34)
      • Kotlin (2)
      • Flutter (42)
      • Error Note (39)
      • 디자인 패턴 (12)
      • 디지털논리회로 (4)
      • 데이터베이스 시스템 (8)
      • 알고리즘 (7)
      • 운영체제 (3)
      • 이산수학 (3)
      • 인공지능 (1)
      • 자료 구조 (14)
        • 기본 개념 (14)
        • 자료구조 스터디 (0)
      • 💡My project (76)
        • 팩맨 : Java Swing 게임 제작 프로젝트 (6)
        • 네이트톡 : Java 소켓 통신 프로젝트 (4)
        • 포켓옥션 : HikariCP&JDBC CRUD 프.. (3)
        • 이지 부산 : BDIA-Devton 2024 프로.. (20)
        • 그린 유니버시티 : JSP를 사용한 학사관리 프로.. (1)
        • 애드 포커 : 웹 소켓과 Spring을 사용한 카.. (1)
        • 셸위 : 게임 친구 매칭 사이트 (21)
        • 다모아 : 개발자 중개 플랫폼 (20)
      • 📗스터디 (13)
        • CNN : 웹개발 스터디 (10)
        • Node&React로 유튜브 사이트 만들기 (3)
      • 📙독서 및 강연 기록 (36)
        • 강연 (14)
        • 독서 (22)
  • 방문자 수
    • 전체:
    • 오늘:
    • 어제:
  • 최근 댓글
      등록된 댓글이 없습니다.
    • 최근 공지
        등록된 공지가 없습니다.
      # Home
      # 공지사항
      #
      # 태그
      # 검색결과
      # 방명록
      • [SpringBoot] 4. 모델링(1) - DB 모델링
        2024년 08월 05일
        • Song hyun
        • 작성자
        • 2024.08.05.:50
        728x90
        반응형

        1. 모델링의 개념

        * 데이터 베이스 모델링은 데이터 구조에 핵심을 둬야 한다!

         

        2. ORM과 TRM 방식

        (1) ORM(Object-Relational Mapping)

        (2) TRM(Table-Relational Mapping)

         

        3. DB 설계

        (1) DB 설계 쿼리

        -- 데이터베이스 생성
        -- mybank
        
        create database mybank;
        use mybank;
        
        -- 외래키 지정 안 함... (only 컬럼)
        -- 유저 테이블 설계
        create table user_tb(
        	id int auto_increment primary key,
            username varchar(50) not null unique key,
            password varchar(100) not null,
            fullname varchar(50) not null,
            created_at timestamp not null default now()
        );
        
        desc user_tb;
        
        -- 계좌 정보 테이블 
        create table account_tb(
        	id int auto_increment primary key,
            number varchar(30) not null unique key,
            password varchar(30) not null,
            balance bigint not null comment '계좌 잔액',
            created_at timestamp not null default now(),
            user_id int
        );
        
        desc account_tb;
        
        -- 거래내역에 대한 식별자(id)
        -- 거래 금액(amount)
        -- 출금 계좌 ID
        -- 입금 계좌 ID
        -- 출금 요청 후, 계좌 잔액
        -- 입금 요청 후, 계좌 잔액
        -- 거래 시간
        create table history_tb(
        	id int auto_increment primary key comment '거래 내역 ID',
            amount bigint not null comment '거래 금액',
            w_account_id int comment '출금 계좌 ID',
            d_account_id int comment '입금 계좌 ID',
            w_balance bigint comment '출금 요청 후 계좌 잔액',
            d_balance bigint comment '입금 요청 후 계좌 잔액',
            created_at timestamp not null default now()
        );
        
        desc history_tb;

         

        (2) 샘플 데이터 쿼리

        desc history_tb;
        
        -- 샘플 데이터 입력 
        -- 유저 
        insert into user_tb(username, password, fullname, created_at)
        values('길동', '1234', '고', now());
        
        insert into user_tb(username, password, fullname, created_at)
        values('둘리', '1234', '애기공룡', now());
        
        insert into user_tb(username, password, fullname, created_at)
        values('마이', '1234', '콜', now());
        
        -- 기본 계좌 등록 
        insert into account_tb
        		(number, password, balance, user_id, created_at)
        values('1111', '1234', 1300, 1, now());        
        
        insert into account_tb
        		(number, password, balance, user_id, created_at)
        values('2222', '1234', 1100, 2, now());        
        
        insert into account_tb
        		(number, password, balance, user_id, created_at)
        values('3333', '1234', 0, 3, now());        
        
        select * from account_tb;

         

        (3) 입출금 내역(history_tb) 추가 연습

        -- 거래 내역 등록 연습
        -- 가상 현재 잔액
        -- 1111 계좌 1000원
        -- 2222 계좌 1000원
        -- 3333 계좌 0원
        
        -- 1. 이체 내역을 기록
        -- 1번 계좌에서 2번 계좌로 100원 이체한다.
        insert into history_tb(amount,w_account_id,d_account_id,w_balance,d_balance,created_at) 
        values (100,1,2,900,1100, now());
        
        -- 2. ATM 기기에서 출금
        -- 1111 계좌에서 100원만 출금하는 히스토리를 만들어보세요
        insert into history_tb(amount,w_account_id,w_balance,created_at) 
        values(100,1,800,now());
        
        -- 3. ATM 기기에서 입금
        -- 1111 계좌에서 500원만 입금하는 히스토리를 만들어보세요
        insert into history_tb(amount,d_account_id,d_balance,created_at) 
        values(500,1,1300,now());
        
        select*from history_tb;

         

        (4) 전체 코드

        -- 데이터베이스 생성
        -- mybank
        
        create database mybank;
        use mybank;
        
        -- 외래키 지정 안 함... (only 컬럼)
        -- 유저 테이블 설계
        create table user_tb(
        	id int auto_increment primary key,
            username varchar(50) not null unique key,
            password varchar(100) not null,
            fullname varchar(50) not null,
            created_at timestamp not null default now()
        );
        
        desc user_tb;
        
        -- 계좌 정보 테이블 
        create table account_tb(
        	id int auto_increment primary key,
            number varchar(30) not null unique key,
            password varchar(30) not null,
            balance bigint not null comment '계좌 잔액',
            created_at timestamp not null default now(),
            user_id int
        );
        
        desc account_tb;
        
        -- 거래내역에 대한 식별자(id)
        -- 거래 금액(amount)
        -- 출금 계좌 ID
        -- 입금 계좌 ID
        -- 출금 요청 후, 계좌 잔액
        -- 입금 요청 후, 계좌 잔액
        -- 거래 시간
        create table history_tb(
        	id int auto_increment primary key comment '거래 내역 ID',
            amount bigint not null comment '거래 금액',
            w_account_id int comment '출금 계좌 ID',
            d_account_id int comment '입금 계좌 ID',
            w_balance bigint comment '출금 요청 후 계좌 잔액',
            d_balance bigint comment '입금 요청 후 계좌 잔액',
            created_at timestamp not null default now()
        );
        
        desc history_tb;
        
        -- 샘플 데이터 입력 
        -- 유저 
        insert into user_tb(username, password, fullname, created_at)
        values('길동', '1234', '고', now());
        
        insert into user_tb(username, password, fullname, created_at)
        values('둘리', '1234', '애기공룡', now());
        
        insert into user_tb(username, password, fullname, created_at)
        values('마이', '1234', '콜', now());
        
        -- 기본 계좌 등록 
        insert into account_tb
        		(number, password, balance, user_id, created_at)
        values('1111', '1234', 1300, 1, now());        
        
        insert into account_tb
        		(number, password, balance, user_id, created_at)
        values('2222', '1234', 1100, 2, now());        
        
        insert into account_tb
        		(number, password, balance, user_id, created_at)
        values('3333', '1234', 0, 3, now());        
        
        select * from account_tb;
        
        
        -- 거래 내역 등록 연습
        -- 가상 현재 잔액
        -- 1111 계좌 1000원
        -- 2222 계좌 1000원
        -- 3333 계좌 0원
        
        -- 1. 이체 내역을 기록
        -- 1번 계좌에서 2번 계좌로 100원 이체한다.
        insert into history_tb(amount,w_account_id,d_account_id,w_balance,d_balance,created_at) 
        values (100,1,2,900,1100, now());
        
        -- 2. ATM 기기에서 출금
        -- 1111 계좌에서 100원만 출금하는 히스토리를 만들어보세요
        insert into history_tb(amount,w_account_id,w_balance,created_at) 
        values(100,1,800,now());
        
        -- 3. ATM 기기에서 입금
        -- 1111 계좌에서 500원만 입금하는 히스토리를 만들어보세요
        insert into history_tb(amount,d_account_id,d_balance,created_at) 
        values(500,1,1300,now());
        
        select*from history_tb;
        728x90
        반응형

        'Springboot' 카테고리의 다른 글

        [SpringBoot] 5. 화면 구현  (0) 2024.08.05
        [SpringBoot] 4. 모델링(2) - DTO 모델링  (0) 2024.08.05
        [SpringBoot] 3. REST API란?  (0) 2024.08.05
        [SpringBoot] 2. 패키지 설정  (0) 2024.08.05
        [SpringBoot] 1. 스프링부트 설치 및 세팅  (0) 2024.08.02
        다음글
        다음 글이 없습니다.
        이전글
        이전 글이 없습니다.
        댓글
      조회된 결과가 없습니다.
      스킨 업데이트 안내
      현재 이용하고 계신 스킨의 버전보다 더 높은 최신 버전이 감지 되었습니다. 최신버전 스킨 파일을 다운로드 받을 수 있는 페이지로 이동하시겠습니까?
      ("아니오" 를 선택할 시 30일 동안 최신 버전이 감지되어도 모달 창이 표시되지 않습니다.)
      목차
      표시할 목차가 없습니다.
        • 안녕하세요
        • 감사해요
        • 잘있어요

        티스토리툴바