INSERT & UPDATE & DELETE (DML_Data Manipulation Language)

Joy๐ŸŒฑยท2023๋…„ 1์›” 17์ผ
0

๐Ÿš Oracle

๋ชฉ๋ก ๋ณด๊ธฐ
7/11
post-thumbnail

๐Ÿ’โ€โ™€๏ธ DML(Data Manipulation Language)์ด๋ž€,
๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ์–ธ์–ด๋กœ์จ, ํ…Œ์ด๋ธ”์— ๊ฐ’์„ ์‚ฝ์ž…ํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ์กฐํšŒ(SELECT)ํ•˜๋Š” ์–ธ์–ด

  • INSERT, UPDATE, DELETE์— SELECT๊นŒ์ง€ DML์— ํฌํ•จ

๐Ÿ‘€ INSERT

๐Ÿ’โ€โ™€๏ธ INSERT(์‚ฝ์ž…)๋ž€,
์ƒˆ๋กœ์šด ํ–‰์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ตฌ๋ฌธ์œผ๋กœ, ํ…Œ์ด๋ธ”์˜ ํ–‰ ๊ฐฏ์ˆ˜๊ฐ€ ์ฆ๊ฐ€


  • ํ…Œ์ด๋ธ”์˜ ์ผ๋ถ€ ์ปฌ๋Ÿผ์— INSERTํ•  ๋•Œ,
    INSERT INTO ํ…Œ์ด๋ธ”๋ช… (์ปฌ๋Ÿผ๋ช…, ์ปฌ๋Ÿผ๋ช…, ...) VALUES (๋ฐ์ดํ„ฐ, ๋ฐ์ดํ„ฐ, ...);
    • ์ž…๋ ฅํ•˜๋Š” ์ปฌ๋Ÿผ๋ช…๊ณผ ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๋Š” ๊ฐ™์•„์•ผํ•จ
  • ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์ปฌ๋Ÿผ์— INSERTํ•  ๋•Œ,
    INSERT INTO ํ…Œ์ด๋ธ”๋ช… VALUES (๋ฐ์ดํ„ฐ, ๋ฐ์ดํ„ฐ, ...);
    • ๋‹จ, ๋ฐ์ดํ„ฐ๋Š” ์ปฌ๋Ÿผ์˜ ์ˆœ์„œ ๋ฐ ๊ฐฏ์ˆ˜์™€ ์™„๋ฒฝํ•˜๊ฒŒ ๋งž์ถฐ์•ผํ•จ

๐Ÿ‘‰ INSERT์˜ ์‚ฌ์šฉ

[1] ์ปฌ๋Ÿผ๋ช…๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ๋ฐ์ดํ„ฐ ๋‚˜์—ดํ•˜์—ฌ ์ž…๋ ฅ

INSERT
  INTO EMPLOYEE
(
  EMP_ID, EMP_NAME, EMP_NO, EMAIL, PHONE, DEPT_CODE, JOB_CODE, SAL_LEVEL, 
  SALARY, BONUS, MANAGER_ID, HIRE_DATE, ENT_DATE, ENT_YN
)
VALUES
(
 '900', '์žฅ์ฑ„ํ˜„', '901123-2080503', 'jang_ch@greedy.com', '01055691254',
 'D1', 'J7', 'S3', 4300000, 0.2, '200', SYSDATE, NULL, DEFAULT 
); 

๐Ÿ“Œ Ref.

* DEFAULT : ๋”ฐ๋กœ ๊ฐ’์„ ์ž…๋ ฅํ•˜์ง€์•Š๊ณ  'N'์ด๋ผ๋Š” ENT_YN์˜ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ง€์ •

[2] VALUES ๋Œ€์‹  ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉ

CREATE TABLE EMP_01 ( -- ํ…Œ์ŠคํŠธ์šฉ ํ…Œ์ด๋ธ”
  EMP_ID NUMBER,
  EMP_NAME VARCHAR2(30),
  DEPT_TITLE VARCHAR2(20)
);

INSERT
  INTO EMP_01
(
  EMP_ID
, EMP_NAME
, DEPT_TITLE
)
(
  SELECT >>> VALUES ๋Œ€์‹  SELECT ๊ตฌ๋ฌธ (SELECT๋กœ ์กฐํšŒํ•œ 24๊ฐœ ํ–‰์„ ํ•œ๊บผ๋ฒˆ์— ์‚ฝ์ž…)
        EMP_ID
    ,   EMP_NAME
    ,   DEPT_TITLE
    FROM EMPLOYEE
    LEFT JOIN DEPARTMENT ON (DEPT_CODE = DEPT_ID)
);

[3] INSERT ALL ์‚ฌ์šฉ

INSERT์‹œ์— ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์— INSERT ALL์„ ์ด์šฉํ•˜์—ฌ ํ•œ ๋ฒˆ์— ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์Œ
(๋‹จ, ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์กฐ๊ฑด์ ˆ์ด ๊ฐ™์•„์•ผํ•จ)

โœ… EMP_DEPT_D1 ํ…Œ์ด๋ธ” ์ƒ์„ฑ (๊ตฌ์กฐ๋งŒ ๋ณต์‚ฌ)
CREATE TABLE EMP_DEPT_D1
AS
SELECT
        EMP_ID
    ,   EMP_NAME
    ,   DEPT_CODE
    ,   HIRE_DATE
    FROM EMPLOYEE
    WHERE 1 = 0; 
    >>> 1 = 0์ด๋ผ๋Š” FALSE ์กฐ๊ฑด์„ ๋„ฃ์–ด ํ–‰๋“ค์„ ์ œ์™ธํ•œ ๊ตฌ์กฐ๋งŒ ๋ณต์‚ฌ (์˜๋„์ ์œผ๋กœ ํ–‰ ์ถœ๋ ฅX)
CREATE TABLE EMP_MANAGER
AS
SELECT
        EMP_ID
    ,   EMP_NAME
    ,   MANAGER_ID
    FROM EMPLOYEE
    WHERE 1 = 0; 
โœ… ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ๊ฐ™์€ EMP_DEPT_D1 ์™€ EMP_MANAGER ๋‘ ํ…Œ์ด๋ธ”์— ํ•œ๊บผ๋ฒˆ์— ํ–‰ ์‚ฝ์ž…
INSERT ALL
  INTO EMP_DEPT_D1
VALUES   >>> ์•„๋ž˜์˜ SELECT๊ตฌ๋ฌธ์—์„œ ๊ฐ€์ ธ์˜ด
(
  EMP_ID
, EMP_NAME
, DEPT_CODE
, HIRE_DATE
)
  INTO EMP_MANAGER
VALUES   >>> ์•„๋ž˜์˜ SELECT๊ตฌ๋ฌธ์—์„œ ๊ฐ€์ ธ์˜ด
(
  EMP_ID
, EMP_NAME
, MANAGER_ID
)
SELECT
        EMP_ID
    ,   EMP_NAME
    ,   DEPT_CODE
    ,   HIRE_DATE
    ,   MANAGER_ID
    FROM EMPLOYEE
    WHERE DEPT_CODE = 'D1';

๐Ÿ‘€ UPDATE

๐Ÿ’โ€โ™€๏ธ UPDATE(์ˆ˜์ •)๋ž€,
ํ…Œ์ด๋ธ”์— ๊ธฐ๋ก ๋œ ์ปฌ๋Ÿผ์˜ ๊ฐ’์„ ์ˆ˜์ •ํ•˜๋Š” ๊ตฌ๋ฌธ (ํ…Œ์ด๋ธ”์˜ ์ „์ฒด ํ–‰ ๊ฐฏ์ˆ˜๋Š” ๋ณ€ํ™” X)
UPDATE ํ…Œ์ด๋ธ”๋ช… SET ์ปฌ๋Ÿผ๋ช… = ๋ฐ”๊ฟ€๊ฐ’, ์ปฌ๋Ÿผ๋ช… = ๋ฐ”๊ฟ€๊ฐ’, ... [WHERE ์ปฌ๋Ÿผ๋ช… ๋น„๊ต์—ฐ์‚ฐ์ž ๋น„๊ต๊ฐ’];

๐Ÿ‘‰ UPDATE์˜ ์‚ฌ์šฉ

[1] WHERE์ ˆ๋กœ ์กฐ๊ฑด ์„ค์ •

UPDATE
        DEPT_COPY					>>> ํ…Œ์ด๋ธ”๋ช…
    SET DEPT_TITLE = '์ „๋žต๊ธฐํšํŒ€'	>>> ์—ฌ๊ธฐ์„œ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค ์ „๋žต๊ธฐํšํŒ€์œผ๋กœ ๋ณ€๊ฒฝ๋จ
    WHERE DEPT_ID = 'D9';			>>> DEPT_ID D9 ํ–‰๋งŒ ๋ฐ”๊พธ๋Š” ์กฐ๊ฑด์„ ๋‹ฌ์•„์คŒ

[2] ์„œ๋ธŒ ์ฟผ๋ฆฌ ์‚ฌ์šฉ (๋‹ค์ค‘์—ด)

UPDATE ํ…Œ์ด๋ธ”๋ช… SET ์ปฌ๋Ÿผ๋ช… = (์„œ๋ธŒ์ฟผ๋ฆฌ)

-- ๋ฐฉ๋ช…์ˆ˜ ์‚ฌ์›์˜ ๊ธ‰์—ฌ์™€ ๋ณด๋„ˆ์Šค์œจ์„ ์œ ์žฌ์‹ ์‚ฌ์›๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ณ€๊ฒฝ
UPDATE
        EMP_SALARY
    SET (SALARY, BONUS) = (SELECT >>> ์œ ์žฌ์‹์˜ ๊ธ‰์—ฌ์™€ ๋ณด๋„ˆ์Šค๋ฅผ ์กฐํšŒํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ
                                    SALARY
                                ,   BONUS
                                FROM EMP_SALARY
                                WHERE EMP_NAME = '์œ ์žฌ์‹' 
                                )
    WHERE EMP_NAME = '๋ฐฉ๋ช…์ˆ˜';

[3] WHERE์ ˆ์—์„œ ์„œ๋ธŒ ์ฟผ๋ฆฌ ์‚ฌ์šฉ (๋‹ค์ค‘ํ–‰)

-- ์•„์‹œ์•„ ๊ทผ๋ฌด ์ง€์—ญ์— ๊ทผ๋ฌดํ•˜๋Š” ์ง์›์˜ ๋ณด๋„ˆ์Šค๋ฅผ 0.5๋กœ ๋ณ€๊ฒฝํ•˜๋Š” UPDATE ๊ตฌ๋ฌธ ์ž‘์„ฑ
UPDATE 
        EMP_SALARY ES
    SET ES.BONUS = 0.5
    WHERE ES.EMP_ID IN (SELECT
                                E1.EMP_ID
                            FROM EMPLOYEE E1
                            JOIN DEPARTMENT D1 ON (D1.DEPT_ID = E1.DEPT_CODE)
                            JOIN LOCATION L1 ON (L1.LOCAL_CODE = D1.LOCATION_ID)
                            WHERE L1.LOCAL_NAME LIKE 'ASIA%'
                        );

๐Ÿ‘‰ UPDATE์‹œ์˜ ์ œ์•ฝ ์กฐ๊ฑด

UPDATE์‹œ, ๋ณ€๊ฒฝ ๊ฐ’์€ ํ•ด๋‹น ์ปฌ๋Ÿผ์— ๋Œ€ํ•œ ์ œ์•ฝ ์กฐ๊ฑด์— ์œ„๋ฐฐ๋˜์ง€ ์•Š์•„์•ผ ํ•จ

-- EMPLOYEE ํ…Œ์ด๋ธ”์˜ DEPT_CODE์— ์™ธ๋ž˜ํ‚ค ์ œ์•ฝ์กฐ๊ฑด ์ถ”๊ฐ€
ALTER TABLE EMPLOYEE ADD FOREIGN KEY (DEPT_CODE) REFERENCES DEPARTMENT (DEPT_ID); 

>>> DEPARTMENT ํ…Œ์ด๋ธ”์˜ DEPT_ID๋กœ์„œ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ’์œผ๋กœ UPDATE x
>>> ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ ์กฐ๊ฑด์„ ์œ„๋ฐฐ (๋ถ€๋ชจํ‚ค ์—†์Œ)
UPDATE
        EMPLOYEE
    SET DEPT_CODE = '20'
    WHERE DEPT_CODE = 'D6';
>>> NOT NULL ์ œ์•ฝ ์กฐ๊ฑด ์œ„๋ฐฐ
UPDATE
        EMPLOYEE
    SET EMP_NAME = NULL 
    WHERE EMP_ID = '200';
    
    >>> NULL๋กœ ("C##EMPLOYEE"."EMPLOYEE"."EMP_NAME")์„ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค ์˜ค๋ฅ˜
    >>> (NOT NULL ์„ค์ •๋˜์–ด์žˆ๊ธฐ๋•Œ๋ฌธ)

๐Ÿ‘€ DELETE

๐Ÿ’โ€โ™€๏ธ DELETE(์‚ญ์ œ)๋ž€,
ํ…Œ์ด๋ธ”์˜ ํ–‰์„ ์‚ญ์ œํ•˜๋Š” ๊ตฌ๋ฌธ (ํ…Œ์ด๋ธ”์˜ ํ–‰์˜ ๊ฐฏ์ˆ˜๊ฐ€ ์ค„์–ด๋“ฌ)
DELETE FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด

  • ๋งŒ์•ฝ WHERE์ ˆ๋กœ ์กฐ๊ฑด์„ ์„ค์ •ํ•˜์ง€์•Š์œผ๋ฉด ๋ชจ๋“  ํ–‰์ด ๋‹ค ์‚ญ์ œ๋จ

๐Ÿ‘‰ DELETE์˜ ์‚ฌ์šฉ

DELETE
    FROM EMPLOYEE
    WHERE EMP_NAME = '์žฅ์ฑ„ํ˜„';
    >>> ์ด ํ–‰ ์‚ญ์ œ (ํ™•์ธ ์‹œ, ํ–‰ 23๊ฐœ ๋‚จ์•„์žˆ์Œ)

๐Ÿ‘‰ DELETE์‹œ์˜ ์ œ์•ฝ ์กฐ๊ฑด

FK ์ œ์•ฝ ์กฐ๊ฑด์ด ์„ค์ •๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ, ์ฐธ์กฐ ๋˜๊ณ  ์žˆ๋Š” ๊ฐ’ ๋Œ€ํ•ด์„œ๋Š” ์‚ญ์ œ ๋ถˆ๊ฐ€
(์ฐธ์กฐ ๋˜๊ณ  ์žˆ์ง€ ์•Š๋Š” ๊ฐ’์— ๋Œ€ํ•ด์„œ๋Š” ์‚ญ์ œ ๊ฐ€๋Šฅ)

DELETE
    FROM DEPARTMENT
    WHERE DEPT_ID = 'D1';
    >>> ์‚ญ์ œ๋ถˆ๊ฐ€. D1์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์ž์‹ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ 
    >>> '๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ์กฐ๊ฑด(C##EMPLOYEE.SYS_C008099)์ด ์œ„๋ฐฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค- ์ž์‹ ๋ ˆ์ฝ”๋“œ๊ฐ€ ๋ฐœ๊ฒฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค' ์˜ค๋ฅ˜
>>> FK ์ œ์•ฝ ์กฐ๊ฑด์ด ์„ค์ •๋˜์–ด ์žˆ์–ด๋„ ์ฐธ์กฐ ๋˜๊ณ  ์žˆ์ง€ ์•Š๋Š” ๊ฐ’์— ๋Œ€ํ•ด์„œ๋Š” ์‚ญ์ œ ๊ฐ€๋Šฅ          

DELETE
    FROM DEPARTMENT
    WHERE DEPT_ID = 'D3';

๐Ÿ‘‰ TRUNCATE

๐Ÿ’โ€โ™€๏ธ TRUNCATE ๋ž€,
ํ…Œ์ด๋ธ”์˜ ์ „์ฒด ํ–‰์„ ์‚ญ์ œํ•  ์‹œ ์‚ฌ์šฉ

  • DELETE๋ณด๋‹ค ์ˆ˜ํ–‰ ์†๋„๊ฐ€ ๋” ๋น ๋ฅด๋ฉฐ ROLLBACK์„ ํ†ตํ•ด ๋ณต๊ตฌ X
-- ํ˜„์žฌ ์‹œ์  ์ €์žฅ
COMMIT; 

DELETE
    FROM EMP_SALARY;
    
SELECT
        ES.*
    FROM EMP_SALARY ES;
    
ROLLBACK;

TRUNCATE TABLE EMP_SALARY;
-- 'Table EMP_SALARY์ด(๊ฐ€) ์ž˜๋ ธ์Šต๋‹ˆ๋‹ค.' ์ถœ๋ ฅ
>>> DELETE์™€ ๋‹ฌ๋ฆฌ ROLLBACK์œผ๋กœ ๋ณต๊ตฌ ๋ถˆ๊ฐ€

ROLLBACK;
>>> ROLLBACKํ•ด์„œ ์กฐํšŒํ•ด๋ด๋„ ํ–‰์€ ๋ณต๊ตฌ๋˜์ง€์•Š์Œ           

๐Ÿ‘€ MERGE

๐Ÿ’โ€โ™€๏ธ MERGE(๋ณ‘ํ•ฉ)๋ž€,
๊ตฌ์กฐ๊ฐ€ ๊ฐ™์€ ๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋Š” ๊ธฐ๋Šฅ
ํ…Œ์ด๋ธ”์—์„œ ์ง€์ •ํ•˜๋Š” ์กฐ๊ฑด์˜ ๊ฐ’์ด ์กด์žฌํ•˜๋ฉด UPDATE(์ˆ˜์ •), ์กฐ๊ฑด์˜ ๊ฐ’์ด ์—†์œผ๋ฉด INSERT(์‚ฝ์ž…)๋จ
MERGE INTO ๋ณ‘ํ•ฉํ• ํ…Œ์ด๋ธ” USING ์ด์šฉํ• ํ…Œ์ด๋ธ” ON (๊ธฐ์ค€) WHEN MATCHED THEN UPDATE SET ์ผ์น˜ํ•˜๋Š”๊ฒƒ๋งค์นญ WHEN NOT MATCHED THEN INSERT (๋ณ‘ํ•ฉํ• ํ…Œ์ด๋ธ”์ปฌ๋Ÿผ๋ช…) VALUES (์ด์šฉํ• ํ…Œ์ด๋ธ”์ปฌ๋Ÿผ๋ช…)


๐Ÿ‘‰ MERGE์˜ ์‚ฌ์šฉ

MERGE
 INTO EMP_M01 M1
USING EMP_M02 M2
   ON (M1.EMP_ID = M2.EMP_ID) -- EMP_ID๊ฐ€ ๊ฐ™์€ ๊ฒƒ๋ผ๋ฆฌ ๋ณ‘ํ•ฉ
 WHEN MATCHED THEN
UPDATE
   SET M1.EMP_NAME = M2.EMP_NAME -- EMP_ID๊ฐ€ ์ด๋ฏธ ๊ธฐ์ค€์ด๋ฏ€๋กœ ์—ฌ๊ธฐ์—๋Š” ์†ํ•˜์ง€ X
     , M1.EMP_NO = M2.EMP_NO
     , M1.EMAIL = M2.EMAIL
     , M1.PHONE = M2.PHONE
     , M1.DEPT_CODE = M2.DEPT_CODE
     , M1.JOB_CODE = M2.JOB_CODE
     , M1.SAL_LEVEL = M2.SAL_LEVEL
     , M1.SALARY = M2.SALARY
     , M1.BONUS = M2.BONUS
     , M1.MANAGER_ID = M2.MANAGER_ID
     , M1.HIRE_DATE = M2.HIRE_DATE
     , M1.ENT_DATE = M2.ENT_DATE
     , M1.ENT_YN = M2.ENT_YN
 WHEN NOT MATCHED THEN
INSERT
(
  M1.EMP_ID, M1.EMP_NAME, M1.EMP_NO, M1.EMAIL, M1.PHONE
, M1.DEPT_CODE, M1.JOB_CODE, M1.SAL_LEVEL, M1.SALARY, M1.BONUS
, M1.MANAGER_ID, M1.HIRE_DATE, M1.ENT_DATE, M1.ENT_YN
)
VALUES
(
  M2.EMP_ID, M2.EMP_NAME, M2.EMP_NO, M2.EMAIL, M2.PHONE
, M2.DEPT_CODE, M2.JOB_CODE, M2.SAL_LEVEL, M2.SALARY, M2.BONUS
, M2.MANAGER_ID, M2.HIRE_DATE, M2.ENT_DATE, M2.ENT_YN
);

๐Ÿ“Œ TCL(Transaction Control Language)

๐Ÿ’โ€โ™€๏ธ ํŠธ๋žœ์žญ์…˜ (Transaction)์ด๋ž€,
ํ•œ๊บผ๋ฒˆ์— ์ˆ˜ํ–‰๋˜์–ด์•ผํ•  ์ตœ์†Œ์˜ ์ž‘์—… ๋‹จ์œ„ ๋ฐ ๋…ผ๋ฆฌ์ ์ธ ์ž‘์—… ๋‹จ์œ„(Logical Unit of Work : LUW)

  • ํ•˜๋‚˜์˜ ํŠธ๋žœ์ ์…˜์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ž‘์—…์€ ๋ฐ˜๋“œ์‹œ ํ•œ๊บผ๋ฒˆ์— ์™„๋ฃŒ(COMMIT) ๋˜์–ด์•ผํ•˜๋ฉฐ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ํ•œ๊บผ๋ฒˆ์— ์ทจ์†Œ(ROLLBACK)๋˜์–ด์•ผํ•จ

๐Ÿ‘€ COMMIT & ROLLBACK

๐Ÿ’โ€โ™€๏ธ COMMIT์ด๋ž€,
ํŠธ๋žœ์žญ์…˜ ์ž‘์—…์ด ์ •์ƒ ์™„๋ฃŒ ๋˜๊ณ  ๋‚˜๋ฉด ๋ณ€๊ฒฝ ๋‚ด์šฉ์„ ์˜๊ตฌํžˆ ์ €์žฅ

  • DML(INSERT, UPDATE, DELETE) ๊ตฌ๋ฌธ์€ ๋ฐ˜๋“œ์‹œ COMMIT์„ ํ•ด์•ผ ์ตœ์ข…์ ์œผ๋กœ ๋ฐ˜์˜ ๐Ÿ”ฅ์ค‘์š”๐Ÿ”ฅ

๐Ÿ’โ€โ™€๏ธ ROLLBACK์ด๋ž€,
ํŠธ๋žœ์žญ์…˜ ์ž‘์—…์„ ์ทจ์†Œํ•˜๊ณ  ์ตœ๊ทผ COMMITํ•œ ์‹œ์ ์œผ๋กœ ์ด๋™

๐Ÿ™‹โ€ ์ž ๊น ! ๋น„์Šทํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” SAVEPOINT๋„ ์•Œ์•„๋ด…์‹œ๋‹ค !

  • SAVEPOINT ์„ธ์ด๋ธŒํฌ์ธํŠธ๋ช… : ํ˜„์žฌ ํŠธ๋žœ์ ์…˜ ์ž‘์—… ์‹œ์ ์— ์ด๋ฆ„์„ ์ •ํ•ด์คŒ. ํ•˜๋‚˜์˜ ํŠธ๋žœ์ ์…˜ ์•ˆ์—์„œ ๊ตฌ์—ญ์„ ๋‚˜๋ˆ”
  • ROLLBACK TO ์„ธ์ด๋ธŒํฌ์ธํŠธ๋ช… : ํŠธ๋žœ์ ์…˜ ์ž‘์—…์„ ์ทจ์†Œํ•˜๊ณ  SAVEPOINT ์‹œ์ ์œผ๋กœ ์ด๋™

๐Ÿ‘‰ COMMIT & ROLLBACK์˜ ์‚ฌ์šฉ

โœ… ํ…Œ์ด๋ธ” ์ƒ์„ฑ
CREATE TABLE TBl_USER(
    USERNO NUMBER UNIQUE,
    ID VARCHAR2(20) PRIMARY KEY,
    PASSWORD CHAR(20) NOT NULL
    );
โœ… ํ…Œ์ด๋ธ”์— ๊ฐ’ ์‚ฝ์ž… ํ›„ COMMIT
INSERT -- ์‹คํ–‰ ์‹œ, ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘ (์ตœ์ข… ๋ฐ˜์˜X)
    INTO TBL_USER
(
    USERNO, ID, PASSWORD
)
VALUES
(
    1, 'test1', 'pass1'
);
INSERT -- ์‹คํ–‰ ์‹œ, ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘ (์ตœ์ข… ๋ฐ˜์˜X)
    INTO TBL_USER
(
    USERNO, ID, PASSWORD
)
VALUES
(
    2, 'test2', 'pass2'
);
INSERT -- ์‹คํ–‰ ์‹œ, ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘ (์ตœ์ข… ๋ฐ˜์˜X)
    INTO TBL_USER
(
    USERNO, ID, PASSWORD
)
VALUES
(
    3, 'test3', 'pass3'
);

COMMIT; >>> ์ตœ์ข… ๋ฐ˜์˜ ์™„๋ฃŒ
โœ… TBL_USER ํ…Œ์ด๋ธ” ์กฐํšŒ (์ตœ์ข… ๋ฐ˜์˜ํ•œ ๊ฒฐ๊ณผ ํ™•์ธ)
SELECT
        UT.*
    FROM TBL_USER UT;
โœ… COMMIT์ดํ›„ ๊ฐ’ ์‚ฝ์ž…
>>> ์‹คํ–‰ํ•ด๋„ ๋ณด์ด๊ธฐ๋Š” ํ•จ, ํŠธ๋žœ์žญ์…˜ ์ž‘์—…์ด ์‹คํ–‰๋œ ๊ฒƒ ๋ฟ
INSERT
    INTO TBL_USER
(
    USERNO, ID, PASSWORD
)
VALUES
(
    4, 'test4', 'pass4'
);
โœ… ROLLBACK
ROLLBACK; >>> ์ปค๋ฐ‹ํ•œ ์‹œ์ ์œผ๋กœ ๋Œ์•„๊ฐ€ 4๋ฒˆ์ด ์‚ฌ๋ผ์ง

๐Ÿ‘‰ SAVEPOINT์˜ ์‚ฌ์šฉ

โœ… 4๋ฒˆ ๋‹ค์‹œ INSERT
INSERT
    INTO TBL_USER
(
    USERNO, ID, PASSWORD
)
VALUES
(
    4, 'test4', 'pass4'
);
โœ… SAVEPOINT ๋ฐ SAVEPOINT๋ช… ์ƒ์„ฑ
SAVEPOINT SP1; 
    
INSERT 
    INTO TBL_USER
(
    USERNO, ID, PASSWORD
)
VALUES
(
    5, 'test5', 'pass5'
);   
โœ… TBL_USER ํ…Œ์ด๋ธ” ์กฐํšŒ
>>> 12345 ๋‹ค ์žˆ๋Š” ์ƒํƒœ

SELECT
        UT.*
    FROM TBL_USER UT; 
โœ… ROLLBACK TO (SAVEPOINT ์ง€์ ์œผ๋กœ ๋Œ์•„๊ฐ)
>>> ์‹คํ–‰ ์‹œ, 1234๋งŒ ์žˆ๋Š” ์ƒํƒœ๋กœ ๋Œ์•„๊ฐ

ROLLBACK TO SP1; 
โœ… ROLLBACK (COMMIT ์ง€์ ์œผ๋กœ ๋Œ์•„๊ฐ)
>>> ์‹คํ–‰ ์‹œ, 123๋งŒ ์žˆ๋Š” ์ƒํƒœ๋กœ ๋Œ์•„๊ฐ

ROLLBACK; 

profile
Tiny little habits make me

0๊ฐœ์˜ ๋Œ“๊ธ€