ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • SQL - DML, ํŠธ๋žœ์žญ์…˜ TCL
    DATABASE 2020. 7. 23. 08:31

    ๐ŸŽฏ SQL DML ๋ช…๋ น์–ด์™€ ํŠธ๋žœ์žญ์…˜์„ ์•Œ์•„๋ณธ๋‹ค.

     

     

     

     

    DML(Data Manipulation Language)

    ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€(INSERT), ์ˆ˜์ •(UPDATE), ์‚ญ์ œ(DELETE), ๋ณ‘ํ•ฉ(MERGE) ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด

     

     

    INSERT

    ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด

     

     

    ๋‹จ์ผํ–‰ INSERT : ํ•œ๋ฒˆ์— ํ•˜๋‚˜์˜ ํ–‰์„ ํ…Œ์ด๋ธ”์— ์ €์žฅ

    ๋‹ค์ค‘ํ–‰ INSERT : ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ํ•œ๊บผ๋ฒˆ์— ์—ฌ๋Ÿฌํ–‰ ๋™์‹œ์ €์žฅ

     

     

    ๋‹จ์ผํ–‰ INSERT ๋ฌธ

     

    INSERT INTO DEPT(DEPTNO, DNAME, LOC)
    VALUES (90, '์ธ์‚ฌ๊ณผ', '์„œ์šธ');
    
    
    -- ์–ด๋–ค ํ…Œ์ด๋ธ”์— ๋„ฃ์„๊ฑฐ๋‹ค (์ปฌ๋Ÿผ๋ช…)
    -- ๋ฐ์ดํ„ฐ๊ฐ’์€ (์œ„ ์ปฌ๋Ÿผ์ด๋ž‘ ์ผ๋Œ€์ผ ๋Œ€์‘์ด ๋˜์–ด์•ผํ•จ)

     

    ์ปฌ๋Ÿผ 3๊ฐœ ๋„ฃ์–ด์ฃผ๊ธฐ๋กœ ํ–ˆ์œผ๋ฉด ๋ฐธ๋ฅ˜๋„ ๋˜‘๊ฐ™์ด 3๊ฐœ๋ฅผ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค.

    ์ž…๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํ„ฐํƒ€์ž…๊ณผ ๊ฐ™์•„์•ผํ•œ๋‹ค.

    ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋Š” ํ…Œ์ด๋ธ” ๋งŒ๋“ค๋•Œ ๋งŒ๋“ค์–ด์ง„ ์ปฌ๋Ÿผํฌ๊ธฐ์™€ ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์•„์•ผํ•œ๋‹ค.

    ๋ฌธ์ž ๋ฐ์ดํ„ฐํƒ€์ž…์ด VARCHAR(10) ์ด๋ผ๋ฉด 10๊ธ€์ž๋ฅผ ๋„˜์œผ๋ฉด ์•ˆ๋จ!

     

    INSERT INTO DEPT
    VALUES(80, '์„œ์šธ', '์ธ์‚ฌ๊ณผ');

     

    INTO์ ˆ์— ์ปฌ๋Ÿผ์„ ์ƒ๋žตํ•ด๋„ ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ ์ˆœ์„œ์— ๋”ฐ๋ผ VALUES์ ˆ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์•Œ์•„์„œ ์ž˜ ๋„ฃ์–ด์ค€๋‹ค. ์ด ๊ฒฝ์šฐ์—” VALUES ์ ˆ์—๋Š” ๋ฐ˜๋“œ์‹œ ๊ทธ ์ปฌ๋Ÿผ์˜ ๋ชจ๋“  ์ปฌ๋Ÿผ๊ฐ’์„ ๋ˆ„๋ฝํ•˜์ง€ ์•Š๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค. ๋น ๋œจ๋ฆฌ๋Š”๊ฒŒ ์žˆ์œผ๋ฉด ๊ฐ’์˜ ์ˆ˜๊ฐ€ ์ถฉ๋ถ„ํ•˜์ง€ ์•Š๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

    DEPTNO๋Š” ์ค‘๋ณต๋ถˆ๊ฐ€ ์ œ์•ฝ์กฐ๊ฑด(๊ธฐ๋ณธํ‚ค)์ด ๊ฑธ๋ ค์žˆ์–ด์„œ ๋˜‘๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ ๋งŒ๋“ค๊ณ  ๋˜ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค

     

     

    NULL๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•

     

    ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์‹œ์ ์—์„œ ํ•ด๋‹น ์ปฌ๋Ÿผ๊ฐ’์„ ๋ชจ๋ฅด๊ฑฐ๋‚˜ ํ™•์ •๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ ํ•ด๋‹น ์ปฌ๋Ÿผ๊ฐ’์— ๋„๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ๋ฌต์‹œ์  ๋ฐฉ๋ฒ•

    INSERT INTO DEPT(DEPTNO, DNAME) 
    VALUES(91, '์ธ์‚ฌ๊ณผ');

     

    INTO์ ˆ์—์„œ ํ•ด๋‹น ์ปฌ๋Ÿผ๋ช…๊ณผ ๊ฐ’์„ ์ƒ๋žตํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋„๊ฐ’์„ ์ €์žฅํ•ด์ค€๋‹ค. ๋‹จ, ํ•ด๋‹น ์ปฌ๋Ÿผ์— NOT NULL ์ œ์•ฝ์กฐ๊ฑด์ด ์ง€์ •๋œ ๊ฒฝ์šฐ์—” ์ƒ๋žต ๋ถˆ๊ฐ€.

    ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” LOC ์ปฌ๋Ÿผ์„ ์“ฐ์ง€ ์•Š์•„์„œ ์ž๋™์œผ๋กœ NULL ๊ฐ’์ด ์ €์žฅ๋œ๋‹ค. 

     

    ๋ช…์‹œ์  ๋ฐฉ๋ฒ•

    VALUES์ ˆ ์ปฌ๋Ÿผ๊ฐ’์— ์ง์ ‘ NULL ๊ฐ’ ๋˜๋Š” '' ๋นˆ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•œ๋‹ค.

     

     

    ๋‹ค์ค‘ํ–‰ INSERT ๋ฌธ

     

    ์„œ๋ธŒ์ฟผ๋ฆฌ ์ ˆ์„ ์ด์šฉํ•˜์—ฌ ํ•˜๋‚˜์˜ INSERT ๋ช…๋ น๋ฌธ์— ์—ฌ๋Ÿฌ ํ–‰์„ ๋™์‹œ์— ์ž…๋ ฅ๊ฐ€๋Šฅํ•˜๋‹ค.

     

    -- EMP ์—ฐ์Šต์šฉ ๋ณต์ œ ํ…Œ์ด๋ธ” ์ƒ์„ฑ 
    
    CREATE TABLE COPY_EMP 
    AS 
    SELECT EMPNO, ENAME FROM EMP 
    WHERE 1=2; 
    
    
    
    -- EMP ํ…Œ์ด๋ธ”์—์„œ EMPNO, ENAME ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๋“ค์„ ๋ณต์ œ 
    
    INSERT INTO COPY_EMP(EMPNO, ENAME) 
    SELECT EMPNO, ENAME FROM EMP;

     

    ๊ธฐ์กด์˜ ํ…Œ์ด๋ธ”์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ๋ณต์‚ฌํ•œ ํ›„ INSERT๋ฌธ์œผ๋กœ ์ƒˆ๋กœ์šด ํ–‰์„ ์ƒ์„ฑํ•œ๋‹ค. (*CTAS)

    INTO์ ˆ์—์„œ ์ง€์ •ํ•œ ์ปฌ๋Ÿผ ๊ฐœ์ˆ˜์™€ ๋ฐ์ดํ„ฐํƒ€์ž…์ด ์„œ๋ธŒ์ฟผ๋ฆฌ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ์™€ ๋ฐ˜๋“œ์‹œ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค.

     

     

    ์กด์žฌํ•˜๋Š” ํ…Œ์ด๋ธ” ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ํ…Œ์ด๋ธ” ์ƒ์ƒํ•˜๋Š” ๋ฐฉ๋ฒ• CTAS

     

    CREATE TABLE ํ…Œ์ด๋ธ”๋ช… [(์ปฌ๋Ÿผ๋ช…, ์ปฌ๋Ÿผ๋ช…2)]
    AS
    SELECT * FROM ํ…Œ์ด๋ธ”  -- ์„œ๋ธŒ์ฟผ๋ฆฌ ์‹œ์ž‘
    WHERE 1=2;
    
    
    ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ WHERE์ ˆ ์กฐ๊ฑด์ด FALSE์ด๋ฉด ๊ฒ€์ƒ‰๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์—
    ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ ์‚ฌ์šฉํ•œ ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋งŒ ๋ณต์‚ฌ๋˜์–ด ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ” ์ƒ์„ฑ >> ๋นˆ ํ…Œ์ด๋ธ”
    
    ์กฐ๊ฑด์ด TRUE ๊ฑฐ๋‚˜ WHERE์ ˆ์„ ์ƒ๋žตํ•˜๋ฉด ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋œ ๋ฐ์ดํ„ฐ๊นŒ์ง€ ํฌํ•จํ•˜์—ฌ ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ” ์ƒ์„ฑ
    
    ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋งŒ ๋ณต์‚ฌํ•˜๊ธฐ ์œ„ํ•ด WHERE ์ ˆ์˜ ์กฐ๊ฑด์‹์ด FALSE๊ฐ€ ๋˜๋„๋ก 1=2 ๊ฐ™์ด ์„ค์ •ํ•˜๋Š”๊ฒƒ.
    
    ์ด๊ฒŒ ๋˜ NOT NULL ์ œ์•ฝ์กฐ๊ฑด ์ œ์™ธํ•œ ์ œ์•ฝ์กฐ๊ฑด์€ ๋ณต์‚ฌ๋˜์ง€ ์•Š์Œ
    ๋”ฐ๋ผ์„œ DEPTNO ์ปฌ๋Ÿผ์€ ๊ธฐ๋ณธํ‚ค ์„ค์ • ์•ˆ๋˜์–ด์žˆ๋Š” ์ƒํƒœ๋กœ ์ƒ์„ฑ๋จ.
    
    as๋Š” ๋ณ„์นญ์ด ์•„๋‹ˆ๋ผ ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค ๋ผ๋Š” ์˜๋ฏธ๋กœ์“ฐ์ž„
    
    
    
    CREATE TABLE MYDEPT
    AS
    SELECT * FROM DEPT
    WHERE 1=2;
    -- DEPT ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋งŒ ๋ณต์‚ฌํ•จ
    
    
    INSERT INTO MYDEPT
    SELECT DEPTNO, DNAME, LOC FROM DEPT;
    -- DEPT ํ…Œ์ด๋ธ”์˜ ๋ช‡๊ฐ€์ง€ ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ ํ…Œ์ด๋ธ”์— ๋„ฃ์–ด์ค€๋‹ค

     

    ๋ฌธ์žฅ์˜ ์•ž๊ธ€์ž๋ฅผ ๋”ฐ์„œ CTAS๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. INTO์ ˆ ๋‹ค์Œ์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ ์‹คํ–‰ํ•œ๋‹ค. ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ์ด์šฉํ•ด INSERT๋ฌธ์ด ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์‹คํ–‰๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ๋ฉด ์ž๋™์œผ๋กœ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ INSERT๋œ๋‹ค.

     

     

    CREATE TABLE COPY_EMP
    AS
    SELECT EMPNO, ENAME FROM EMP
    WHERE 1=2;
    
    
    INSERT INTO COPY_EMP (EMPNO, ENAME)
    SELECT EMPNO, ENAME FROM EMP;
    
    
    SELECT * FROM COPY_EMP;

     

    ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ๋จผ์ € ๋ณด๋ฉด, EMPํ…Œ์ด๋ธ”์—์„œ ์ € ๋‘์ปฌ๋Ÿผ๋งŒ ๊ฐ€์ง„ ๊ตฌ์กฐ๋ฅผ ๋ณต์‚ฌํ•ด์™€์„œ ์ƒˆ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ ๋‹ค. ๊ทธ ๋‹ค์Œ EMP์˜ ๋‘ ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ ํ…Œ์ด๋ธ” ์•ˆ์— ๊ทธ๋Œ€๋กœ ๋„ฃ๋Š”๋‹ค. ํ•ญ์ƒ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‹คํ–‰๊ฒฐ๊ณผ์™€ INTO์ ˆ ์ปฌ๋Ÿผ ๊ฐœ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•  ๊ฒƒ.

     

     

    ๋‹ค์ค‘ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ๋‹ค์ค‘ํ–‰ INSERT ๋ฌธ

     

    ํ•œ๋ฒˆ์˜ INSERT๋กœ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์— ๋ณต์ˆ˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ๋ฌด์กฐ๊ฑด INSERT ALL

    ์กฐ๊ฑด INSERT ALL

    ์กฐ๊ฑด INSERT FIRST

     

     

    ๋ฌด์กฐ๊ฑด INSERT ALL

     

    ํ•œ๋ฒˆ์— ๋‹ค์ค‘ํ…Œ์ด๋ธ”์— ๋‹ค์ค‘ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

    ํŠน๋ณ„ํ•œ ์กฐ๊ฑด ์—†์ด ์—ฌ๋Ÿฌํ…Œ์ด๋ธ”์— ์ €์žฅ์ด ๋œ๋‹ค.

    when์ ˆ ์กฐ๊ฑด์‹์„ ์ƒ๋žตํ•˜๋Š” ๊ฒฝ์šฐ(์กฐ๊ฑด์ด ์—†์Œ)๋ฅผ ๋ฌด์กฐ๊ฑด INSERT ALL ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

    ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๊ฐ€ INTO์ ˆ์—์„œ ์ง€์ •ํ•œ ํ…Œ์ด๋ธ”1๊ณผ ํ…Œ์ด๋ธ”2์— ์ž๋™์œผ๋กœ INSERT๋œ๋‹ค.

    ๋จผ์ € ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ ํ•„์š”ํ•œ ์ž๋ฃŒ๋ฅผ ๋‹ค ๋ฝ‘์•„์˜ค๊ณ 

    INSERT๋ฌธ์—์„œ ํ•„์š”ํ•œ ์ปฌ๋Ÿผ๋“ค์„ ํ…Œ์ด๋ธ” ๊ฐ๊ฐ์— ๋”ฐ๋กœ ์ ์–ด์ค€๋‹ค.

    ๋ฐธ๋ฅ˜ ์ ˆ์— ์‚ฌ์šฉ๋œ ์ปฌ๋Ÿผ๋ช…๊ณผ ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ ์‚ฌ์šฉ๋œ ์ปฌ๋Ÿผ๋ช…์ด ๋ฐ˜๋“œ์‹œ ๋™์ผํ•ด์•ผ๋œ๋‹ค.

     

    -- ํ…Œ์ด๋ธ” 2๊ฐœ ๋จผ์ € ์ƒ์„ฑ
    
    CREATE TABLE SAL_HISTORY
    AS
    SELECT EMPNO, HIREDATE, SAL
    FROM EMP
    WHERE 1=2; 
    
    
    CREATE TABLE MGR_HISTORY
    AS
    SELECT EMPNO, MGR, SAL
    FROM EMP
    WHERE 1=2;
    
    
    -- ๋ฐ์ดํ„ฐ ์ž…๋ ฅ
    
    INSERT ALL
    INTO SAL_HISTORY VALUES (EMPNO, HIREDATE, SAL)
    INTO MGR_HISTORY VALUES (EMPNO, MGR, SAL)
    SELECT EMPNO, HIREDATE, SAL, MGR
    FROM EMP;
    
    
    -- ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜ ๋“ค์–ด๊ฐ”๋Š”์ง€ ํ™•์ธ
    
    SELECT * FROM SAL_HISTORY;
    SELECT * FROM MGR_HISTORY;

     

     

     

    ์กฐ๊ฑด INSERT ALL

     

    WHEN์ ˆ์—์„œ ์ง€์ •ํ•œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰๋งŒ INSERTํ•˜๋Š” ๋ฐฉ๋ฒ•

     

    -- ๋“œ๋ž์œผ๋กœ ์ด๋ฏธ ์ƒ์„ฑ๋˜์–ด ์žˆ๋Š” ํ…Œ์ด๋ธ”์„ ๋จผ์ € ์‚ญ์ œํ•ด์ค˜์•ผ ๋‹ค์‹œ ์˜ˆ์ œ์—ฐ์Šต์„ ํ• ์ˆ˜์žˆ์Œ
    
    DROP TABLE SAL_HISTORY;
    DROP TABLE MGR_HISTORY;
    
    
    -- ํ…Œ์ด๋ธ” ์ƒ์„ฑ
    
    CREATE TABLE SAL_HISTORY
    AS SELECT EMPNO, HIREDATE, SAL
    FROM EMP
    WHERE 1=2;
    
    CREATE TABLE MGR_HISTORY
    AS
    SELECT EMPNO, MGR, SAL
    FROM EMP
    WHERE 1=2;
    
    
    -- ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ €์žฅ
    
    INSERT ALL
    WHEN SAL < 2500 THEN
    INTO SAL_HISTORY VALUES(EMPNO, HIREDATE, SAL)
    WHEN SAL > 2500 THEN
    INTO MGR_HISTORY VALUES (EMPNO, MGR, SAL)
    SELECT EMPNO, HIREDATE, SAL, MGR
    FROM EMP;
    
    
    -- ํ™•์ธ
    
    SELECT * FROM SAL_HISTORY;
    SELECT * FROM MGR_HISTORY;

     

     

     

    ์กฐ๊ฑด INSERT FIRST

     

    WHEN ์ ˆ์— ์ง€์ •๋œ ์กฐ๊ฑด์ด ์ค‘๋ณต๋˜๋Š” ๊ฒฝ์šฐ์— ์ฒซ๋ฒˆ์งธ ์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋Š” ํ…Œ์ด๋ธ”๋งŒ ์ €์žฅ๋˜๊ณ  ์ดํ›„์—๋Š” ์กฐ๊ฑด์ด ์ผ์น˜ํ•ด๋„ ํ…Œ์ด๋ธ”์— ์ €์žฅ๋˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•. INSERT ALL ์„ INSERT FIRST๋กœ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค.

     

    DROP TABLE SAL_HISTORY;
    DROP TABLE MGR_HISTORY;
    
    
    
    -- ํ…Œ์ด๋ธ” 3๊ฐœ ์ƒ์„ฑ
    
    CREATE TABLE SAL_HISTORY
    AS
    SELECT EMPNO, HIREDATE, SAL
    FROM EMP
    WHERE 1=2;
    
    CREATE TABLE MGR_HISTORY
    AS
    SELECT EMPNO, MGR, SAL
    FROM EMP
    WHERE 1=2;
    
    CREATE TABLE TEST_HISTORY
    AS
    SELECT EMPNO, SAL
    FROM EMP
    WHERE 1=2;
    
    
    
    -- ๋ฐ์ดํ„ฐ ์ €์žฅ
    
    INSERT FIRST
    WHEN SAL=800 THEN
    INTO SAL_HISTORY VALUES(EMPNO, HIREDATE, SAL)
    WHEN SAL <2500 THEN
    INTO MGR_HISTORY VALUES (EMPNO, MGR, SAL)
    ELSE
    INTO TEST_HISTORY VALUES (EMPNO, SAL)
    SELECT EMPNO, HIREDATE, SAL, MGR
    FROM EMP;
    
    
    -- sal 800์€ ๋‘๊ฐ€์ง€ ์กฐ๊ฑด์— ์ค‘๋ณต๋˜์–ด ๋‘ ํ…Œ์ด๋ธ”์— ๋™์‹œ์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.
    -- INSERT FIRST๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด sal 800์€ ์ฒซ๋ฒˆ์งธ ์กฐ๊ฑด์—๋งŒ ์ €์žฅ๋œ๋‹ค.
    -- ELSE๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ ๋ฐ–์˜ ๋ฐ์ดํ„ฐ๋„ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
    
    
    
    
    SELECT * FROM SAL_HISTORY;
    -- ์›”๊ธ‰์ด 800 ์ธ ์• ๋งŒ ์—ฌ๊ธฐ ๋“ค์–ด๊ฐ”๊ณ 
    SELECT * FROM MGR_HISTORY;
    -- 2500 ๋ณด๋‹ค ๋‚ฎ์€ ์• ๋“ค์€ ์—ฌ๊ธฐ
    SELECT * FROM TEST_HISTORY;
    -- ๊ทธ๋ฐ–์˜ ์• ๋“ค์€ ๋‹ค ์—ฌ๊ธฐ.

     

     

     

    UPDATE

     

    ํ…Œ์ด๋ธ”์— ์ €์žฅ๋œ ํ–‰๋“ค์„ ์ˆ˜์ •ํ•˜๋Š” ๋ช…๋ น์–ด๋‹ค.

    ํ•œ๋ฒˆ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ–‰์„ ์—…๋ฐ์ดํŠธ ๊ฐ€๋Šฅ.

     

    UPDATE DEPT
    SET DNAME = '๊ฒฝ๋ฆฌ๊ณผ', LOC = '๋ถ€์‚ฐ'
    WHERE DEPTNO = 90;
    
    SELECT * FROM DEPT;

     

    DEPT ํ…Œ์ด๋ธ”์—์„œ WHERE ์กฐ๊ฑด์— ํ•ด๋‹น๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์•„๊ฐ€์„œ SET ์ ˆ์— ์ ํžŒ๋Œ€๋กœ ์ˆ˜์ •ํ•œ๋‹ค.

    WHERE๊ฐ€ ๋น ์ ธ๋ฒ„๋ฆฌ๋ฉด ์ด ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ์ „์ฒด๊ฐ€ ์ˆ˜์ •๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์กฐ๊ฑด์ ˆ ์žก๊ณ  ์ˆ˜์ •ํ•ด์•ผํ•œ๋‹ค

     

     

    ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•œ ๋ณต์ˆ˜ ์ปฌ๋Ÿผ ๋ณ€๊ฒฝ
    
    
    UPDATE EMP
    SET JOB = (SELECT JOB
    	    FROM EMP
    	    WHERE EMPNO =7900),
        SAL = (SELECT SAL
    	    FROM EMP
    	    WHERE EMPNO =7844)
    WHERE EMPNO = 9001;
    
    
    SELECT * FROM EMP;

     

    set์ ˆ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ์ปฌ๋Ÿผ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค

     

     

    MYDEPTํ…Œ์ด๋ธ”์—์„œ ๋ถ€์„œ๋ฒˆํ˜ธ๊ฐ€ 60์ธ ์‚ฌ์›์˜ ๋ถ€์„œ๋ช…์„
    DEPT ํ…Œ์ด๋ธ”์˜ ๋ถ€์„œ๋ฒˆํ˜ธ๊ฐ€ 10์ธ ๋ถ€์„œ์˜ ๋ถ€์„œ๋ช…์œผ๋กœ ์ˆ˜์ •ํ•˜๊ณ  
    ๋ถ€์„œ์œ„์น˜๋Š” DEPT์—์„œ ๋ถ€์„œ๋ฒˆํ˜ธ๊ฐ€ 20์ธ ๋ถ€์„œ์œ„์น˜๋กœ ์ˆ˜์ •
    
    
    UPDATE MYDEPT
    SET DNAME = (SELECT DEPTNO
    	      FROM DEPT
    	      WHERE DEPTNO = 10),
    	LOC = (SELECT LOC
    	       FROM DEPT
        	       WHERE DEPTNO = 20)
    WHERE DEPTNO = 60;

     

    ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” SET ์ ˆ์— ์จ์ฃผ๊ณ  ์—…๋ฐ์ดํŠธ ๋ฌธ ์ž์ฒด์˜ WHERE ์กฐ๊ฑด์ ˆ์„ ๋‹ฌ์•„์ฃผ๋Š” ๊ฑฐ ์žŠ์ง€๋ง๊ธฐ.

     

     

     

    DELETE

     

    ํ…Œ์ด๋ธ”์— ์ €์žฅ๋œ ํ–‰๋“ค์„ ์‚ญ์ œํ•˜๋Š” ๋ช…๋ น๋ฌธ

     

    DELETE [FROM] TABLE 
    [WHERE ์กฐ๊ฑด];

     

    WHERE์ ˆ์— ์กฐ๊ฑด์„ ์ง€์ •ํ•ด์„œ ์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋Š” ํ–‰๋“ค๋งŒ ์‚ญ์ œํ•œ๋‹ค.

    ์กฐ๊ฑด์‹์ด ์—†์œผ๋ฉด ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋˜๋‹ˆ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค.

     

    DELETE FROM DEPT
    WHERE DEPTNO =91;
    
    
    SELECT * FROM DEPT;
    
    
    
    -- ์„œ๋ธŒ์ฟผ๋ฆฌ ๋ฐ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ” ๊ธฐ๋ฐ˜์„ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ์‚ญ์ œ
    
    DELETE FROM EMP
    WHERE DEPTNO = (SELECT DEPTNO
    FROM DEPT
    WHERE DNAME = '๊ฒฝ๋ฆฌ๊ณผ');

     

    DELETE๋ฌธ์˜ WHERE์ ˆ์— ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์„œ๋ธŒ์ฟผ๋ฆฌ ์‹คํ–‰๋œ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋ฅธํ…Œ์ด๋ธ”์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๋ฐ์ดํ„ฐ ์‚ญ์ œ ๊ฐ€๋Šฅ!

     

     

     

    MERGE

     

    ์ปฌ๋Ÿผ์˜ ๊ตฌ์กฐ๊ฐ€ ๊ฐ™์€ 2๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ๋น„๊ตํ•˜์—ฌ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ํ•ฉ์น˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น์–ด.

     

    MERGE INTO ํ…Œ์ด๋ธ”1 ๋ณ„์นญ  -- ๋ณ‘ํ•ฉํ•œ ๊ฒฐ๊ณผ ์ €์žฅ๋˜๋Š” ํ…Œ์ด๋ธ”๋ช… ์ง€์ •
    USING (ํ…Œ์ด๋ธ”๋ช…2 | ๋ทฐ | ์„œ๋ธŒ์ฟผ๋ฆฌ) ๋ณ„์นญ -- ๋ณ‘ํ•ฉํ•  ํ…Œ์ด๋ธ”๋ช… OR์„ญ์ฟผ๋ฆฌ
    ON (์กฐ์ธ์กฐ๊ฑด)  -- INTO์ ˆ์˜ ํ…Œ์ด๋ธ”๊ณผ USING์ ˆ์˜ ํ…Œ์ด๋ธ”๊ฐ„์˜ ์กฐ์ธ์กฐ๊ฑด
    WHEN MATCHED THEN    -- ์กฐ์ธ์กฐ๊ฑด์ด ์ผ์น˜ํ•˜๋ฉด UPDATE ๋ฌธ์žฅ ์‹คํ–‰
    UPDATE SET
    ์ปฌ๋Ÿผ๋ช… = ๊ฐ’,
    ์ปฌ๋Ÿผ๋ช…1=๊ฐ’1
    [WHERE ์กฐ๊ฑด์‹]
    [DELETE WHERE ์กฐ๊ฑด์‹]
    WHEN NOT MATCHED THEN  --์กฐ์ธ์กฐ๊ฑด ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด INSERT์‹คํ–‰
    INSERT (์ปฌ๋Ÿผ๋ชฉ๋ก)
    VALUES (๊ฐ’ ๋ชฉ๋ก)
    [WHERE ์กฐ๊ฑด์‹];

     

     

    WHEN ์ ˆ์˜ ์กฐ๊ฑด์ ˆ์—์„œ ๋Œ€์ƒ ํ…Œ์ด๋ธ”์— ํ•ด๋‹น ํ–‰์ด ์ด๋ฏธ ์กด์žฌํ•˜๋ฉด UPDATE ๊ฐ€ ์‹คํ–‰ ๋˜๊ณ  ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด INSERT๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

    ์ „์ž์ƒ๊ฑฐ๋ž˜ ํšŒ์‚ฌ์—์„œ ํŒ๋งค ํ˜„ํ™ฉ์„ ์›”๋ณ„๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์—ฐ๋ง์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ‘ํ•ฉํ•  ๋•Œ MERGE๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     

    -- ํ…Œ์ด๋ธ” ์ƒ์„ฑ
    
    -- 1์›”ํŒ๋งค์ •๋ณด
    CREATE TABLE PT_01
    (ํŒ๋งค๋ฒˆํ˜ธ VARCHAR2(8),
    ์ œํ’ˆ๋ฒˆํ˜ธ NUMBER,
    ์ˆ˜๋Ÿ‰ NUMBER,
    ๊ธˆ์•ก NUMBER);
    
    -- 2์›” ํŒ๋งค์ •๋ณด
    CREATE TABLE PT_02
    (ํŒ๋งค๋ฒˆํ˜ธ VARCHAR2(8),
    ์ œํ’ˆ๋ฒˆํ˜ธ NUMBER,
    ์ˆ˜๋Ÿ‰ NUMBER,
    ๊ธˆ์•ก NUMBER);
    
    -- ํ†ตํ•ฉ ํŒ๋งค์ •๋ณด
    CREATE TABLE P_TOTAL
    (ํŒ๋งค๋ฒˆํ˜ธ VARCHAR2(8),
    ์ œํ’ˆ๋ฒˆํ˜ธ NUMBER,
    ์ˆ˜๋Ÿ‰ NUMBER,
    ๊ธˆ์•ก NUMBER);
    
    
    
    -- ๋ฐ์ดํ„ฐ ์ž…๋ ฅ
    
    INSERT INTO PT_01 VALUES ('20150101', '1000', 10, 500);
    INSERT INTO PT_01 VALUES ('20150102', '1000', 10, 400);
    INSERT INTO PT_01 VALUES ('20150103', '1000', 10, 300);
    
    INSERT INTO PT_02 VALUES ('20150201', '1000', 5, 500);
    INSERT INTO PT_02 VALUES ('20150202', '1000', 5, 400);
    INSERT INTO PT_02 VALUES ('20150203', '1000', 5, 300);
    COMMIT;
    
    
    
    -- ๋ณ‘ํ•ฉ
    
    MERGE INTO P_TOTAL TOTAL  -- ๋ณ‘ํ•ฉ๋œ ๋ฐ์ดํ„ฐ ์ €์žฅ์„ ์œ„ํ•œ ํ†ตํ•ฉํ…Œ์ด๋ธ”
    USING PT_01 P01
    ON (TOTAL.ํŒ๋งค๋ฒˆํ˜ธ = P01.ํŒ๋งค๋ฒˆํ˜ธ)
    WHEN MATCHED THEN
    UPDATE SET TOTAL.์ œํ’ˆ๋ฒˆํ˜ธ = P01.์ œํ’ˆ๋ฒˆํ˜ธ
    WHEN NOT MATCHED THEN
    INSERT VALUES (P01.ํŒ๋งค๋ฒˆํ˜ธ, P01.์ œํ’ˆ๋ฒˆํ˜ธ, P01.์ˆ˜๋Ÿ‰, P01.๊ธˆ์•ก);
    
    MERGE INTO P_TOTAL TOTAL
    USING PT_02 P02
    ON (TOTAL.ํŒ๋งค๋ฒˆํ˜ธ = P02.ํŒ๋งค๋ฒˆํ˜ธ)
    WHEN MATCHED THEN
    UPDATE SET TOTAL.์ œํ’ˆ๋ฒˆํ˜ธ = P02.์ œํ’ˆ๋ฒˆํ˜ธ
    WHEN NOT MATCHED THEN
    INSERT VALUES (P02.ํŒ๋งค๋ฒˆํ˜ธ, P02.์ œํ’ˆ๋ฒˆํ˜ธ, P02.์ˆ˜๋Ÿ‰, P02.๊ธˆ์•ก);
    
    
    
    -- ํ™•์ธ
    
    SELECT * FROM P_TOTAL;

     

    ์›”๋ณ„ ํ…Œ์ด๋ธ”๊ณผ ํ†ตํ•ฉ ํ…Œ์ด๋ธ”์˜ ์กฐ์ธ์กฐ๊ฑด์€ ON์ ˆ์— ์“ด ํŒ๋งค๋ฒˆํ˜ธ์ธ๋ฐ, ํ†ตํ•ฉํ…Œ์ด๋ธ”์—” ์•„๋ฌด๊ฒƒ๋„ ๋“ค์–ด์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ํ…Œ์ด๋ธ”๊ฐ„ ์ผ์น˜ํ•˜๋Š”๋ฐ์ดํ„ฐ๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋Š” ์ƒํƒœ์ด๋‹ค. ๋”ฐ๋ผ์„œ WHEN NOT MATCHED์˜ INSERT๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค.

    ์‹คํ–‰ ๊ฒฐ๊ณผ ํ† ํƒˆ ํ…Œ์ด๋ธ”์—” ์›”๋ณ„ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋œ๋‹ค. 

    ๋งŒ์•ฝ ์›”๋ณ„ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜๋ชป๋˜์–ด ์ˆ˜์ •ํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด ํ† ํƒˆ ํ…Œ์ด๋ธ”๊ณผ ๋‹ค์‹œ ๋ณ‘ํ•ฉํ•  ๋•Œ WHEN์ ˆ ์กฐ๊ฑด์ด ์ผ์น˜ํ•ด์„œ UPDATE๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค.

     

     

    CREATE TABLE EMP_M2
    AS
    SELECT EMPNO, JOB, SAL
    FROM EMP
    WHERE 1=2;
    
    
    MERGE INTO EMP_M2 M2
    USING EMP B
    ON (M2.EMPNO = B.EMPNO)
    
    WHEN MATCHED THEN
    UPDATE SET
    M2.JOB = B.JOB,
    M2.SAL = B.SAL
    WHERE B.JOB='CLERK'  -- ์กฐ๊ฑด๋ฌธ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค
    
    WHEN NOT MATCHED THEN
    INSERT(M2.EMPNO, M2.JOB, M2.SAL)
    VALUES (B.EMPNO, B.JOB, B.SAL)
    WHERE B.JOB='CLERK';  -- ์กฐ๊ฑด๋ฌธ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค
    
    
    SELECT * FROM EMP_M2;

     

    CREATE TABLE EMP_M3
    AS
    SELECT EMPNO, JOB, SAL
    FROM EMP;
    
    
    MERGE INTO EMP_M3 M3
    USING EMP B
    ON (M3.EMPNO = B.EMPNO)
    
    WHEN MATCHED THEN
    UPDATE SET
    M3.JOB = B.JOB,
    M3.SAL = B.SAL + 1000
    DELETE WHERE (M3.JOB='CLERK')  -- MERGE UPDATE๋ฌธ์— DELETE๋ฌธ์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.
    
    
    WHEN NOT MATCHED THEN
    INSERT(M3.EMPNO, M3.JOB, M3.SAL)
    VALUES (B.EMPNO, B.JOB, B.SAL+500);
    
    
    SELECT * FROM EMP_M3;

     

     

     

    ํŠธ๋žœ์žญ์…˜

    ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋…ผ๋ฆฌ์ ์ธ ์ž‘์—… ๋‹จ์œ„

    ํŠธ๋žœ์žญ์…˜ ๋Œ€์ƒ์ด ๋˜๋Š” SQL ๋ฌธ์€ DML๋ฌธ์ด๋‹ค.

    — ์…€๋ ‰ํŠธ๋ฌธ์€ ์ง์ ‘์  ํŠธ๋žœ์žญ์…˜ ๋Œ€์ƒ ์•„๋‹ˆ์ง€๋งŒ SELECT * FROM EMP FOR UPDATE; ๋“ฑ์˜ ๋ฌธ์žฅ์€ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.

     

    DML๋ช…๋ น์ด ์‹œ์ž‘๋˜๋ฉด(์ถ”๊ฐ€,์ˆ˜์ •,์‚ญ์ œ ์ด๊ฒƒ๋“ค์€) ํŠธ๋žœ์žญ์…˜ ๊ณผ์ •์ด ์ž๋™๋ฐœ์ƒ๋˜๋ฉฐ ๋ฐ˜๋“œ์‹œ ํŠธ๋žœ์žญ์…˜ ์ข…๋ฃŒํ•ด์ค˜์•ผ ํ•œ๋‹ค.

    ํŠธ๋žœ์žญ์…˜์€ ALL(์ •์ƒ์ฒ˜๋ฆฌ ๋˜์–ด ๋๋‚œ ์ƒํƒœ) or NOTHING(์›์ƒ๋ณต๊ตฌ ๋˜์–ด ์•„๋ฌด์ผ๋„ ์ผ์–ด๋‚˜์ง€ ์•Š์€ ์ƒํƒœ) ์ด๋‹ค. ์ž‘์—… ๋‹จ์œ„๋ฅผ ์ „๋ถ€ ์ ์šฉํ•˜๊ฑฐ๋‚˜ ์ „๋ถ€ ์ทจ์†Œํ•œ๋‹ค.

    ํŠธ๋žœ์žญ์…˜์ด ์‹คํ–‰๋œ ๋‹ค์Œ์— ๋งˆ์ง€๋ง‰์— ์ปค๋ฐ‹(๋ฐ˜์˜)์„ ํ• ๊ฑฐ๋ƒ ๋กค๋ฐฑ(์›์ƒ๋ณต๊ตฌ)์„ ํ• ๊ฑฐ๋ƒ ๋ช…๋ น์„ ์ค˜์•ผํ•œ๋‹ค. ์š”๊ฒŒ TCL๋ฌธ์žฅ์ด๋‹ค.(ํŠธ๋žœ์žญ์…˜ ์ œ์–ด ๋ช…๋ น์–ด)

     

    ์ผ๋‹จ DML๋ฌธ์„ ์ž‘์„ฑํ•˜๊ณ  ์…€๋ ‰ํŠธ๋กœ ํ™•์ธ๋œ ๊ฒฐ๊ณผ๋Š” ๋‚˜๋งŒ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

    ์ปค๋ฐ‹์„ ๋‚ ๋ฆฌ๊ธฐ ์ „๊นŒ์ง„ ๋‚˜๋งŒ๋ณด๋Š” ์ž๋ฃŒ๋‹ค.

    ๋ชจ๋“  ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์‚ฌํ•ญ์€ ํŠธ๋žœ์žญ์…˜์ด ์ข…๋ฃŒ๋˜๊ธฐ ์ „๊นŒ์ง€๋Š” ๋ชจ๋‘ ์ž„์‹œ์ ์ธ ์ƒํƒœ๋‹ค.

    ๋ณ€๊ฒฝ๋œ ํ–‰์€ LOCK์ƒํƒœ๊ฐ€ ๋˜๋ฉฐ ํŠธ๋žœ์žญ์…˜ ์ข…๋ฃŒ ํ›„ ์ž ๊ธˆ์ด ํ•ด์ œ๋œ๋‹ค.

     

     

    ๋ฐ์ดํ„ฐ ์ผ๊ด€์„ฑ

    ์‚ฌ์šฉ์ž๋“ค์—๊ฒŒ ๊ฐ€์žฅ ์ตœ๊ทผ์— ์ปค๋ฐ‹๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

    ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๋“ค์ด ๋ณ€๊ฒฝ์ค‘์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณผ ์ˆ˜ ์—†๊ฒŒ ํ•จ์œผ๋กœ์จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ์‚ฌ์šฉ์ž์™€ ๋ณ€๊ฒฝํ•˜๋Š” ์‚ฌ์šฉ์ž๋“ค ์‚ฌ์ด์— ์ผ๊ด€์ ์ธ ๊ด€์ ์„ ์ œ๊ณตํ•œ๋‹ค.

     

     

    TCL ํŠธ๋žœ์žญ์…˜ ์ œ์–ด ๋ช…๋ น์–ด

     

    COMMIT

    ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์˜๊ตฌ ๋ฐ˜์˜ํ•œ๋‹ค.

     

    SAVEPOINT

    ์ง„ํ–‰์ค‘์ธ ํŠธ๋žœ์žญ์…˜์„ ํŠน์ • ์ด๋ฆ„์œผ๋กœ ์ง€์ •ํ•˜๋Š” ๋ช…๋ น์–ด. ์ฑ…๊ฐˆํ”ผ ๊ธฐ๋Šฅ์ด๋‹ค.

    ํ•˜๋‚˜์˜ DML ๋‹ค์Œ์— 'SAVEPOINT ์ด๋ฆ„' ์ด๋ ‡๊ฒŒ ์ง€์ •ํ•ด๋‘๋ฉด ๋‚˜์ค‘์— ๋กค๋ฐฑ์„ ํ•˜๋”๋ผ๋„ ํŠน์ • ์ž‘์—…๋งŒ ์ทจ์†Œํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ROLLBACK

    ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ทจ์†Œํ•˜๊ณ  ๋ณ€๊ฒฝ์ „์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต๊ตฌํ•œ๋‹ค.

     

     

    LOCK ๊ฒฝํ•ฉ

     

    DML ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐ˜๋“œ์‹œ ํŠธ๋žœ์žญ์…˜์„ ์ข…๋ฃŒ์‹œ์ผœ์•ผ ํ•œ๋‹ค.

    A์‚ฌ์šฉ์ž์™€ B์‚ฌ์šฉ์ž๊ฐ€ ๋™์‹œ์— ํ…Œ์ด๋ธ”์˜ ํŠน์ • ๋ ˆ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค๊ณ  ํ•˜์ž. A๊ฐ€ ๋จผ์ € ์‹คํ–‰ํ–ˆ์„ ๊ฒฝ์šฐ ์–˜๊ฐ€ ํŠธ๋žœ์žญ์…˜์„ ๋ช…์‹œ์ ์œผ๋กœ ์ข…๋ฃŒํ•˜์ง€ ์•Š์œผ๋ฉด B์˜ ๋ช…๋ น์€ ์‹คํ–‰๋˜์ง€ ์•Š๊ณ  ๋ฌดํ•œ ๋Œ€๊ธฐํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ์„ LOCK๊ฒฝํ•ฉ์ด๋ผ๊ณ  ํ•˜๋ฉฐ ๋ฌดํ•œ๋Œ€๊ธฐ๋Š” ์˜ค๋ผํด์˜ ์„ฑ๋Šฅ์„ ๊ฐ์†Œ์‹œํ‚ค๋‹ˆ๊นŒ ์ฃผ์˜ํ• ๊ฒƒ.

     

     

     

    ๐Ÿ˜œ NEXT : SQL DDL ๊ณต๋ถ€ํ•˜๊ธฐ

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

๊ฐœ๋ฐœ๊ณต๋ถ€