day9

μƒμ€πŸ‘ΈΒ·2024λ…„ 1μ›” 14일
0

λšœλ²…λšœλ²… μ„Έλ²ˆμ§Έ

λͺ©λ‘ 보기
9/11
post-thumbnail

πŸ“– μ‹œν€€μŠ€ SEQUENCE

μžλ™μœΌλ‘œ λ³€κ²½λ˜λŠ” 값을 νšλ“ν•˜κΈ° μœ„ν•œ κΈ°λŠ₯
🚨 μ‹œν€€μŠ€ 호좜 이후 λ‹€μ‹œ λ’€λ‘œ λŒμ•„κ°ˆμˆ˜κ°€ μ—†λ‹€.

1. μ‹œν€€μŠ€ 생성

CREATE SEQUENCE μ‹œν€€μŠ€μ΄λ¦„
START WITH μ‹œμž‘ν•˜λŠ”μˆ«μžκ°’ (κΈ°λ³Έκ°’ 1) -> 1λΆ€ν„°
INCREMENT BY μ¦κ°€ν• μˆ«μžκ°’ (κΈ°λ³Έκ°’ 1) -> 1μ”© 증가
MINVALUE μ΅œμ†Œκ°’    1
MAXVALUE μ΅œλŒ€κ°’    6
CYCLE | NOCYCLE μˆœν™˜μ—¬λΆ€ 
CACHE 숫자 | NOCACHE 캐싱여뢀 (κΈ°λ³Έκ°’ 20) --μΊμ‹œλ©”λͺ¨λ¦¬ : μž„μ‹œμ°½κ³  

2. μ‹œν€€μŠ€ μ‚¬μš©

μ‹œν€€μŠ€μ΄λ¦„.nextval : μ‹œν€€μŠ€κ°’ νšλ“ (SELECT μ‹œν€€μŠ€μ΄λ¦„.nextval FROM dual);
μ‹œν€€μŠ€μ΄λ¦„.currval : ν˜„μž¬ μ‹œν€€μŠ€κ°’ 쑰회(ν™•μΈλ§Œ) (SELECT μ‹œν€€μŠ€μ΄λ¦„.currval FROM dual);

3. μ‹œν€€μŠ€ μ‚­μ œ --> μ²˜μŒλΆ€ν„° μƒˆλ‘œμš΄ 마음으둜 ν•˜λ €λ©΄ μ‚­μ œν•˜κ³  λ‹€μ‹œ λ§Œλ“€μ–΄μ•Ό ν•œλ‹€!

DROP SEQUENCE μ‹œν€€μŠ€μ΄λ¦„;

4. *** μ‹œν€€μŠ€ 1λΆ€ν„° μ‹œμž‘μœΌλ‘œ μ΄ˆκΈ°ν™”! --> 근데 κ·Έλƒ₯ μ‚­μ œν•˜κ³  λ‹€μ‹œλ§Œλ“œλŠ”κ²Œ 더빠름..

μ‹œν€€μŠ€ μ„€μ • λ³€κ²½
ALTER SEQUENCE μ‹œν€€μŠ€μ΄λ¦„
λ°”κΏ€ μ„€μ •

ν˜„μž¬ κ°’ 확인
INCREMENT BY μ‘°μ • (λ‹€μŒ nextval λΆˆλ €μ„λ•Œ 1이 λ˜λ„λ‘)
nextval μ‹€ν–‰
INCREMENT BY μ‘°μ •

πŸ’Ž μ—°μŠ΅λ¬Έμ œ

  1. μ‹œν€€μŠ€λͺ… : sample_seq
    100λΆ€ν„° μ‹œμž‘ν•΄μ„œ 1μ”© 증가
    μ΅œλŒ€ 110 μ΅œμ†Œ 90 싸이클 적용O
--μ‹œν€€μŠ€ 생성
CREATE SEQUENCE sample_seq
START WITH 100
INCREMENT BY 1
MAXVALUE 110
MINVALUE 90
CYCLE;

--μ‹œν€€μŠ€ μ‚¬μš©
SELECT sample_seq.nextval FROM dual;

--ν˜„μž¬ μ‹œν€€μŠ€ 확인
SELECT sample_seq.currval FROM dual;

🚨


--컬럼 : id, no, member_id, point, grade-date
INSERT INTO T_MEMBER_POINT
VALUES (T_MEMBER_POINT_PK_SEQ.nextval, 
        (SELECT NVL(MAX(no), 0)+1 FROM T_MEMBER_POINT 
        WHERE member_id = 'abc1'), 
        'abc1', 
        30, 
        SYSDATE);

==> T_MEMBER_POINT_PK_SEQ μ‹œν€€μŠ€κ°’μ„ id에 μ‚¬μš©ν•˜κ³ 

==> μ΄λ ‡κ²Œ λ‚˜μ˜€κ²Œ ν•˜κΈ°μœ„ν•΄μ„œλŠ” noλŠ” abc1 의 순번, def1 의 μˆœλ²ˆμ„ λœ»ν•˜λ‹ˆκΉŒ
(SELECT NVL(MAX(no), 0)+1
FROM T_MEMBER_POINT
WHERE member_id = 'abc1')

(SELECT NVL(MAX(no), 0)+1
FROM T_MEMBER_POINT
WHERE member_id = 'def1')

NVL(νŠΉμ •μ»¬λŸΌ, κΈ°λ³Έ λŒ€μ²΄κ°’) -> NULL값을 λ‹€λ₯Έ κΈ°λ³Έκ°’μœΌλ‘œ λŒ€μ²΄ -> μ²˜μŒμ— 1이 λ˜λ„λ‘ μ„ΈνŒ…ν•˜κ³ !
각각 μˆœλ²ˆμ— 맞게 λ‚˜μ˜€λ„λ‘ ν•œλ‹€!

profile
λ’€μ£½λ°•μ£½ 벨둜그

0개의 λŒ“κΈ€