Home
>
DB
>
πΎ[Database] Primary Key μμ± μ λ΅ - DB auto increment(2)
Database
Primary Key
DBMS
βπΎ[Database] Primary Key μμ± μ λ΅ - DB auto increment(2)β
β
1οΈβ£ PKμ Unique Indexλ₯Ό λΆλ¦¬νλ μ΄μ .
βPKλ λ°μ΄ν°λ² μ΄μ€ λ΄μμλ§ μλ³μλ‘ μ¬μ©νκ³ , μ ν리μΌμ΄μ
μμμ μλ³μλ λ³λμ Unique Indexλ₯Ό μ¬μ©ν μλ μλ€.β
μ κ°λ
μ λ°μ΄ν°λ² μ΄μ€ λ΄μμ κ΄λ¦¬νλ PK μ μ ν리μΌμ΄μ
μμ μ¬μ©νλ μλ³μλ₯Ό λΆλ¦¬νλ λ°©μμ
λλ€.
μ¦, PKλ‘ id(Auto Increment)λ₯Ό μ¬μ©νκ³ , μ ν리μΌμ΄μ
μμ article_id(Snowflake λλ UUID)λ₯Ό νμ©νλ ꡬ쑰 μ
λλ€.
β
2οΈβ£ μμ μλ리μ€.
π1οΈβ£ PK: id(AUTO_INCREMENT)
DBμμ κΈ°λ³Έ ν€ μν μ μννλ©°, μ μ(BIGINT) νμ
.
λ°μ΄ν°κ° μΆκ°λ λ 1μ© μλ μ¦κ° .
λ°μ΄ν°λ² μ΄μ€ λ΄μμ κ΄κ³(Join, Indexing, κ²μ μλ)λ₯Ό μ΅μ ν νλ μ©λλ‘ μ¬μ©.
π2οΈβ£ Unique Index: article_id(Snowflake λλ UUID)
μ ν리μΌμ΄μ
μμ μΈλΆ μμ€ν
κ³Ό μ°λνκ±°λ API μλ΅μμ μ¬μ©νλ μλ³μ .
UUID λλ Snowflakeλ₯Ό μ¬μ©νμ¬ μ μμ μΌλ‘ κ³ μ ν κ° μ μμ±.
Unique Indexλ₯Ό μ€μ νμ¬ μ€λ³΅λμ§ μλλ‘ λ³΄μ₯ .
β
3οΈβ£ DDL μμ (MySQL κΈ°μ€).
CREATE TABLE article (
id BIGINT AUTO_INCREMENT PRIMARY KEY , -- PK (DB λ΄λΆμμ μ¬μ©)
article_id VARCHAR ( 36 ) UNIQUE NOT NULL , -- UUID λλ Snowflake (APIμμ μ¬μ©)
title VARCHAR ( 255 ) NOT NULL ,
content TEXT NOT NULL ,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
id β AUTO_INCREMENTλ₯Ό μ¬μ©νμ¬ κΈ°λ³Έ ν€ μν μν.
article_id β UUID λλ Snowflakeλ₯Ό μ¬μ©νλ©°, UNIQUE INDEX μ€μ .
article_idλ₯Ό APIλ₯Ό ν΄λΌμ΄μΈνΈμ λ°μ΄ν°λ₯Ό μ£Όκ³ λ°μ λ μ¬μ© (μ: REST API)
β
4οΈβ£ Spring Boot JPA Entity μμ .
import jakarta.persistence.* ;
import lombok.* ;
import java.time.LocalDateTime ;
@Getter
@ToString
@Entity
@Table ( name = "article" )
@NoArgsconstructor ( access = AccessLevel . PROTECTED )
public class Article {
@Id
@GeneratedValue ( strategy = GenerationType . IDENTITY ) // DB Auto Increment
private Long id ;
@Column ( unique = true , nullable = false , length = 36 ) // UUID or Snowflake
private String articleId ;
private String title ;
private String content ;
private LocalDateTime createdAt ;
private LocalDateTime modifiedAt ;
// μ μ ν©ν 리 λ©μλλ‘ UUID λλ Snowflake ID μμ±
public static Article create ( String title , String content ) {
Article article = new Article ();
article . articleId = generateUniqueId (); // UUID λλ Snowflake μ¬μ©
article . title = title ;
article . content = content ;
article . createdAt = LocalDateTime . now ();
article . modifiedAt = article . createdAt ;
return article ;
}
public void update ( String title , String content ) {
this . title = title ;
this . content = content ;
this . modifiedAt = LocalDateTime . now ();
}
// Snowflake λλ UUID ID μμ± λ©μλ
private static String generateUniqueId () {
return java . util . UUID . randomUUID (). toString (); // UUID μ¬μ© μμ
}
}
id: λ°μ΄ν°λ² μ΄μ€κ° κ΄λ¦¬νλ μλ μ¦κ° κ°.
articleId: μ ν리μΌμ΄μ
μμ UUIDλ₯Ό μ¬μ©νμ¬ μμ±.
generateUniqueId()λ UUIDλ₯Ό μ¬μ©νμ§λ§ Snowflakeλ₯Ό μ μ©ν μλ μμ
β
5οΈβ£ λ°μ΄ν° μ½μ
μμ .
β
JPAλ₯Ό μ¬μ©ν λ°μ΄ν° μ μ₯.
Article article = Article . create ( "My Title" , "This is content." );
articleRepository . save ( article );
β
DBμ μ μ₯λ λ°μ΄ν° μμ.
id
article_id
title
content
created_at
modified_at
1
550e8400-e29b-41d4-a716-446655440000
My Title
This is content
2024-02-05 12:00:00
2024-02-05 12:00:00
idλ μλ μ¦κ°(AUTO_INCREMENT)
article_idλ UUIDλ₯Ό κΈ°λ°μΌλ‘ μμ±λ¨
API μλ΅μμ article_idλ₯Ό μ 곡νμ¬ ν΄λΌμ΄μΈνΈμ λ°μ΄ν° μ£Όκ³ λ°κΈ° μ©λλ‘ μ¬μ©
β
6οΈβ£ μ PKμ Unique Indexλ₯Ό λΆλ¦¬ν κΉ?
1οΈβ£ Auto Increment PKλ DB μ±λ₯ μ΅μ νμ μ 리.
BIGINT AUTO_INCREMENTλ μ μ κ°μ΄λ―λ‘ Join, Indexingμ΄ λΉ λ¦.
UUID(VARCHAR)λ₯Ό PKλ‘ μ¬μ©νλ©΄ μΈλ±μ€ μ±λ₯μ΄ λ¨μ΄μ§ (νΉν MySQL InnoDBμμ).
Primary Keyλ λ΄λΆμ μΈ λ°μ΄ν° κ΄λ¦¬ μ©λλ‘λ§ μ¬μ© νλ κ²μ΄ μΌλ°μ .
2οΈβ£ UUID(Snowflake)λ μ ν리μΌμ΄μ
μΈλΆ μλ³μλ‘ μ ν©.
UUIDλ Snowflakeλ μ μμ μΌλ‘ κ³ μ ν κ° μ μμ±νλ―λ‘ λ€λ₯Έ μμ€ν
κ³Ό μ°λν λ μ 리.
id(Auto Increment)λ₯Ό μ¬μ©νλ©΄ λ€λ₯Έ DBλ‘ λ°μ΄ν°λ₯Ό μ΄λν λ μΆ©λν κ°λ₯μ±μ΄ μμ.
3οΈβ£ λ°μ΄ν° λ§μ΄ν¬λ μ΄μ
& λΆμ° μμ€ν
μ μ 리.
UUID/Snowflakeλ μλ²κ° μ¬λ¬ κ° μμ΄λ κ³ μ ν κ° μμ± κ°λ₯.
Auto Incrementλ λ¨μΌ DBμμλ§ κ³ μ 보μ₯λ¨ β μ€λ©(Sharding) νκ²½μμλ μΆ©λ κ°λ₯.
β
7οΈβ£ API μ€κ³μμμ μ°¨μ΄μ .
1οΈβ£ id(Auto Increment)λ₯Ό APIμμ λ
ΈμΆν κ²½μ°.
{
"id" : 1 ,
"title" : "My Title" ,
"content" : "This is content" ,
"createdAt" : "2024-02-05T12:00:00"
}
보μμ λ¬Έμ κ° λ μ μμ(μ: IDλ₯Ό μ°μμ μΌλ‘ μ¦κ°μν€λ©° μ‘°ν κ°λ₯)
2οΈβ£ articleId(UUID/Snowflake)λ₯Ό APIμμ λ
ΈμΆν κ²½μ° (κΆμ₯)
{
"articleId" : "550e8400-e29b-41d4-a716-446655440000" ,
"title" : "My Title" ,
"content" : "This is content" ,
"createdAt" : "2024-02-05T12:00:00"
}
UUID λλ Snowflakeλ 보μμ±μ΄ λκ³ , λΆμ° νκ²½μμλ μμ ν¨.
ν΄λΌμ΄μΈνΈλ articleIdλ₯Ό μ¬μ©νμ¬ λ°μ΄ν°λ₯Ό μ‘°ννκ³ μ
λ°μ΄νΈν μ μμ.
β
8οΈβ£ API 컨νΈλ‘€λ¬μμ articleId νμ©.
@GetMapping ( "/v1/articles/{articleId}" )
public ArticleResponse getArticle ( @PathVariable String articleId ) {
Article article = articleRepository . findByArticleId ( articleId )
. orElseThrow (() -> new RuntimeException ( "Article not found" ));
return ArticleResponse . from ( article );
}
id(PK)κ° μλλΌ articleIdλ₯Ό κΈ°λ°μΌλ‘ μ‘°ν β ν΄λΌμ΄μΈνΈκ° μμΈ‘ λΆκ°λ₯ν ID μ¬μ© κ°λ₯.
findByArticleId()λ Unique Indexλ₯Ό νμ©νμ¬ λΉ λ₯΄κ² μ‘°ν κ°λ₯.
β
9οΈβ£ κ²°λ‘ .
PK(id)
Unique Index(article_id)
AUTO_INCREMENTλ‘ μμ±λ¨
UUID λλ Snowflakeλ‘ μμ±λ¨
DBμμλ§ μ¬μ© (Join, Index μ΅μ ν)
APIμ μΈλΆ μμ€ν
μμ μ¬μ©
μ°μμ μΈ μ«μ (μ: 1,2,3β¦)
λλ€ κ° (μ: UUID 550e8400-e29bβ¦)
λ°μ΄ν°λ² μ΄μ€ μ±λ₯ μ΅μ ν
보μμ±κ³Ό νμ₯μ± μ¦κ°
λ§μ΄κ·Έλ μ΄μ
μ μΆ©λ μν
λΆμ° μμ€ν
μμ μμ ν¨
π λ§λ¬΄λ¦¬
DB λ΄λΆ κ΄λ¦¬μ© ID(AUTO_INCREMENT)μ API μλ³μ© ID(UUID/Snowflake)λ₯Ό λΆλ¦¬νλ κ²μ΄ λ² μ€νΈ νλν°μ€.
APIμμλ articleId(UUID/Snowflake)λ§ λ
ΈμΆ νμ¬ λ³΄μμ±κ³Ό νμ₯μ±μ ν보.
λ°μ΄ν°λ² μ΄μ€ μ±λ₯μ μ μ§νλ©΄μ μ ν리μΌμ΄μ
λ 벨μμ μ λν¬ν μλ³μλ₯Ό κ°μ§ μ μμ.