Springboot

[SpringBoot] 4. 모델링(1) - DB 모델링

Song hyun 2024. 8. 5. 10: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
반응형