JiYoung Dev πŸ–₯

[Oracle Database] TCL, DCL (2023.03.27) λ³Έλ¬Έ

full stack/database

[Oracle Database] TCL, DCL (2023.03.27)

Shinjio 2023. 3. 27. 14:01

πŸ”Ž TCL (Transaction Control Language) 

νŠΈλžœμž­μ…˜μ„ μ œμ–΄ν•œλ‹€

 

νŠΈλžœμž­μ…˜ : μ–΄λ–€ 업무λ₯Ό μˆ˜ν–‰ν•˜κΈ° μœ„ν•œ 일련의 단계

 

νŠΈλžœμž­μ…˜ νŠΉμ§• 

1. μ›μžμ„±(atomicity) : νŠΈλžœμž­μ…˜μ΄ λͺ¨λ‘ μ™„λ£Œλ˜κ±°λ‚˜ ν•˜λ‚˜λΌλ„ μ‹€νŒ¨μ‹œ λͺ¨λ‘ 원상볡ꡬ λ˜μ–΄μ•Ό 함 (all or nothing) 

2. 일관성(consistency) : 항상 일관적인 ν˜•νƒœλ₯Ό κ°€μ Έμ•Ό 함. νŠΈλžœμž­μ…˜ μ‹€ν–‰ 전이 정상이라면 μ‹€ν–‰ 후에도 정상이어야 함

3. 고립성(isolation) : νŠΈλžœμž­μ…˜μ€ μ„œλ‘œ κ°„μ„­ν•˜λ©΄ μ•ˆλ¨

4. μ˜μ†μ„±/지속성 (durability) : 영ꡬ μ €μž₯을 ν–ˆμœΌλ©΄ λ°˜λ“œμ‹œ λ°˜μ˜λ˜μ–΄μ•Ό 함. commit을 ν–ˆμœΌλ©΄ λ°μ΄ν„°λŠ” λ°˜λ“œμ‹œ 영ꡬ μ €μž₯ λ˜μ–΄ μžˆμ–΄μ•Ό 함

 

πŸ“– μ’…λ₯˜

COMMIT

ROLLBACK

SAVEPOINT 

 

πŸ“– COMMIT

νŠΈλžœμž­μ…˜μ΄ λͺ¨λ‘ 정상 처리되면 λ³€κ²½λœ 데이터λ₯Ό 영ꡬ λ°˜μ˜ν•˜κΈ° μœ„ν•΄ COMMIT 

λ§ˆμ§€λ§‰ COMMIT μ‹œμ  이후 μ‹€ν–‰ν•œ νŠΈλžœμž­μ…˜ κ²°κ³Όλ₯Ό λ°μ΄ν„°λ² μ΄μŠ€μ— 영ꡬ μ €μž₯

 

πŸ“– ROLLBACK

νŠΈλžœμž­μ…˜ 도쀑에 μ—λŸ¬κ°€ λ°œμƒν•˜λ©΄?

COMMIT λΆˆκ°€

νŠΈλžœμž­μ…˜ 도쀑에 ν•˜λ‚˜λΌλ„ 였λ₯˜κ°€ 생기면  이전에 ν–ˆλ˜ λͺ¨λ“  ν™œλ™μ΄ ROLLBACK → μ›μžμ„±

이전에 μ‹€ν–‰ν–ˆλ˜ νŠΈλžœμž­μ…˜λ„ λͺ¨λ‘ ROLLBACK

ROLLBACK은 νŠΉμ • μ§€μ κΉŒμ§€λ§Œ 원상 볡ꡬλ₯Ό ν•˜λ„λ‘ μ‘°μ ˆν•΄μ£ΌλŠ” 문법

λ§ˆμ§€λ§‰μœΌλ‘œ COMMITν•œ μ‹œμ κΉŒμ§€λ§Œ ROLLBACK κ°€λŠ₯

 

-- μ˜€λΌν΄μ—μ„œλŠ”
DML(λ°μ΄ν„°μ‘°μž‘μ–΄) : INSERT, UPDATE, DELETE
→ μ§μ ‘ COMMIT을 ν•΄μ€˜μ•Ό 영ꡬ 반영이 됨
DDL(λ°μ΄ν„°μ •μ˜μ–΄) : CREATE, ALTER, DROP, TRUCATE
→ μ‹€ν–‰ν•  λ•Œλ§ˆλ‹€ μžλ™μœΌλ‘œ μ•Œμ•„μ„œ COMMIT이 됨

1. 
INSERT ...
CREATE ...
ν•˜κ²Œ 되면 μžλ™μœΌλ‘œ COMMIT이 됨

2. 
INSERT INTO...
COMMIT ; -- λ§ˆμ§€λ§‰ 컀밋 μ‹œμ 

INSERT INTO ...
DELETE ...
UPDATE .....

ROLLBACK ; 둀백은 λ§ˆμ§€λ§‰ 컀밋 μ‹œμ κΉŒμ§€ λ˜λŒμ•„κ° 

 

πŸ“– SAVEPOINT 

νŠΉμ • μ§€μ κΉŒμ§€λ§Œ 원상 볡ꡬλ₯Ό ν•˜λ„λ‘ μ‘°μ ˆν•΄μ£ΌλŠ” 문법

 

 

πŸ“– LOCK 

λ‘˜ μ΄μƒμ˜ μ„Έμ…˜μ΄ λ˜‘κ°™μ€ 행을 μ‘°μž‘ν•˜λ €κ³  ν•  λ•Œ μΆ©λŒν•˜λŠ” ν˜„μƒ

νŠΈλž™μž­μ…˜μ˜ νŠΉμ§•μΈ 고립성과 연관됨

 

 

 

πŸ”Ž DCL(Data Control Language)

객체에 κΆŒν•œμ„ λΆ€μ—¬ν•œλ‹€

 

πŸ“– μ’…λ₯˜

GRANT 

REVOKE

ROLE

 

λ°μ΄ν„°λ² μ΄μŠ€λŠ” κΆŒν•œμ„ λΆ€μ—¬/회수 ν•˜λ©΄μ„œ 객체λ₯Ό 보호 → DBAκ°€ μ‚¬μš© (일반 κ°œλ°œμžλŠ” 잘 μ‚¬μš©ν•˜μ§€ μ•ŠμŒ)

 

SQL COMMAND LINEμ—μ„œ 계정 생성 (SYSTEM κ΄€λ¦¬μž κ³„μ •μœΌλ‘œ 접속)

 

μ‚¬μš©μžλŠ” λ§Œλ“€μ—ˆλŠ”λ° λ°μ΄ν„°λ² μ΄μŠ€μ— 접속할 κΆŒν•œμ€ 주지 μ•ŠμŒ → ν…ŒμŠ€νŠΈ μ‹€νŒ¨

 

πŸ“– GRANT

κΆŒν•œμ„ λΆ€μ—¬ν•  λ•Œ μ‚¬μš©ν•˜λŠ” 문법

 

GRANT λΆ€μ—¬ν• κΆŒν•œ [ ON λŒ€μƒκ°μ²΄ ] TO 뢀여받을계정 [ WITH GRANT OPTION | WITH ADMIN OPTION ]

 

κΆŒν•œ λΆ€μ—¬ ν›„ λ°μ΄ν„°λ² μ΄μŠ€ 접속 성곡

 

πŸ“– ROLL 

μ—¬λŸ¬ κΆŒν•œμ„ ν•œλ²ˆμ— λΆ€μ—¬ν•  수 있음

CONNECT, RESOURCE

 

 

CONNECT RESOURCE
ALTER SESSION CREATE CLUSTER
CREATE SESSION CREATE PROCEDURE
CREATE SYNONYM CREATE TABLE
CREATE TABLE CREATE TREIGGER
CREATE VIEW CREATE ...

 

DCL TEST κ³„μ •μ—κ²Œ SERVICE 계정이 가지고 μžˆλŠ” 직원 ν…Œμ΄λΈ”μ— μ‘°νšŒν•  κΆŒν•œμ„ λΆ€μ—¬

 

 

μˆ˜μ • κΆŒν•œ λΆ€μ—¬ μ•ˆν–ˆμ„ λ•Œ

 

πŸ“– REVOKE 

κΆŒν•œ 회수 

 

REVOKE νšŒμˆ˜ν• κΆŒν•œ [ ON νšŒμˆ˜ν• κ°μ²΄ ] FROM νšŒμˆ˜λ‹Ήν• κ³„μ •

 

 

 

쑰회 
SELECT -- DML에 ν¬ν•¨μ‹œν‚€κΈ°λ„ 함

DML(Data Manupulation Language)
INSERT 
UPDATE
DELETE 
MERGE

DDL(Data Definition Language)
CREATE
ALTER
DROP
TRUNCATE
RENAME

TCL(Transaction Control Language) -- DCL에 ν¬ν•¨μ‹œν‚€κΈ°λ„ 함
COMMIT
ROLLBACK
SAVEPOINT

DCL(Data Control Language)
GRANT
REVOKE
ROLE

 

βš™ κΆŒν•œλΆ€μ—¬ μ‹€μŠ΅

1. SQL COMMAND LINE을 μ—΄μ–΄ SYSTEM κ΄€λ¦¬μž κ³„μ •μœΌλ‘œ 접속 

SYSTEM 계정 : 였라클 λ°μ΄ν„°λ² μ΄μŠ€λ₯Ό μ„€μΉ˜ν•  λ•Œ μƒμ„±ν•œ 계정

(DISC :  μ—°κ²° ν•΄μ œ)

 

CONN SYSTEM/12345;

 

2. HACKER 계정 생성

 

CREATE USER HACKER IDENTIFIED BY 12345 ;

 

3. κΆŒν•œλΆ€μ—¬

(1) λ°μ΄ν„°λ² μ΄μŠ€ 접속이 κ°€λŠ₯ν•˜λ„λ‘ CREATE SESSION λΆ€μ—¬

β€» 였라클 λ°μ΄ν„°λ² μ΄μŠ€ μ„Έμ…˜μ€ λ°μ΄ν„°λ² μ΄μŠ€ 접속을 μ‹œμž‘μœΌλ‘œ, μ—¬λŸ¬ μž‘μ—…μ„ μˆ˜ν–‰ν•œ ν›„ 접속 μ’…λ£ŒκΉŒμ§€μ˜ 전체 기간을 μ˜λ―Έν•¨. μ„Έμ…˜ λ‚΄λΆ€μ—λŠ” ν•˜λ‚˜ μ΄μƒμ˜ νŠΈλžœμž­μ…˜μ΄ μ‘΄μž¬ν•¨. 일반적으둜 λ°μ΄ν„°λ² μ΄μŠ€λŠ” μ—¬λŸ¬ κ³³μ—μ„œ λ™μ‹œμ— μ ‘κ·Όν•˜μ—¬ μˆ˜λ§Žμ€ μ„Έμ…˜μ΄ λ™μ‹œμ— μ—°κ²°λ˜μ–΄ 있음

 

GRANT CREATE SESSION TO HACKER ;

 

(2) μ—¬λŸ¬ κΆŒν•œμ„ ν•œλ²ˆμ— μ²˜λ¦¬ν•  수 있게 CONNECT와 RESOURCE ROLE λΆ€μ—¬

 

GRANT CONNECT, RESOURCE TO HACKER ;

 

(3) SERVICE κ³„μ •μ˜ 직원 ν…Œμ΄λΈ”μ„ 쑰회, μˆ˜μ •ν•  수 있게 SELECT, UPDATE κΆŒν•œ λΆ€μ—¬

 

GRANT SELECT, UPDATE ON SERVICE.직원 TO HACKER ; 

 

(4) HACKER κ³„μ •μœΌλ‘œλΆ€ν„° SERVICE κ³„μ •μ˜ 직원 ν…Œμ΄λΈ”μ— λŒ€ν•œ UPDATE, SELECT κΆŒν•œ 제거

 

REVOKE SELECT, UPDATE ON SERVICE.직원 FROM HACKER ;