DAY-27. Oracle DML,TCL,DCL,View,Sequence

5 minute read

πŸ” 2022-04-07

λ°μ΄ν„°λ² μ΄μŠ€

❗ μˆ˜μ—…μ„ λ“£κ³ , 개인이 κ³΅λΆ€ν•œ λ‚΄μš©μ„ 적은 것 이기에 였λ₯˜κ°€ λ§Žμ„ μˆ˜λ„ 있음

1️⃣ DML (Data Manipulation Language)

데이터 μ‘°μž‘μ–΄

  • 데이터λ₯Ό μ‚½μž…, μˆ˜μ •, μ‚­μ œ, μ‘°νšŒν•˜λŠ” μ–Έμ–΄ β†’ insert, update, delete, select

insert

ν…Œμ΄λΈ”μ— μƒˆλ‘œμš΄ 행을 μΆ”κ°€ν•  λ•Œ μ‚¬μš©ν•˜λŠ” ꡬ문

1) λͺ¨λ“  행에 λŒ€ν•œ 데이터λ₯Ό μΆ”κ°€ν•˜κ³ μž ν• λ•Œ μ‚¬μš©ν•˜λŠ” ꡬ문 (ν•œ 행도 λΉΌμ§€μ•Šκ³  넣을 λ•Œ)

insert into ν…Œμ΄λΈ”λͺ… values(μž…λ ₯ν•  데이터, μž…λ ₯ν•  데이터…)

2) νŠΉμ •ν•œ μ»¬λŸΌμ—λ§Œ 데이터λ₯Ό λ„£κ³  싢은 경우 μ‚¬μš©ν•˜λŠ” ꡬ문 :

insert into ν…Œμ΄λΈ”λͺ… (컬럼λͺ…1, 컬럼λͺ…2…) values (μž…λ ₯데이터1, μž…λ ₯데이터2…)

// ν…Œμ΄λΈ” 생성
create table member (
    id varchar2(100) primary key
    , pw varchar2(100) not null
    , nickname varchar2(100) unique
    , email varchar2(100)
);

// ν…Œμ΄λΈ” 쑰회
select * from member;

// ν…Œμ΄λΈ” μ‚½μž…
insert into member values ('abc123','abc','ABC초콜릿','abc@naver.com');
insert into member (id,pw) values ('eee555','eEE');

// λ¬΅μ‹œμ  ν˜•λ³€ν™˜ -> 였라클이 μžλ™μœΌλ‘œ μžλ£Œν˜•μ„ μΆ”μΈ‘ν•˜μ—¬ λ³€ν™˜ν•΄μ£ΌλŠ”κ²ƒ
insert into member values(123,sysdate,'ffd','abc@naver.com');

update

μ»¬λŸΌμ— μ €μž₯된 데이터λ₯Ό μˆ˜μ •ν•˜λŠ” ꡬ문 β†’ ν…Œμ΄λΈ”μ˜ 전체 ν–‰ κ°œμˆ˜μ— λ³€ν™”λ₯Ό 주지 μ•ŠμŒ

update ν…Œμ΄λΈ”λͺ… set λ³€κ²½ν•  컬럼λͺ… = λ³€κ²½ν•  값… where 쑰건

//μ΄λŒ€λ‘œ ν•˜λ©΄ κΈ°μ‘΄ν–‰μ˜ λͺ¨λ“  이메일이 eee@gamil.com으둜 λ³€κ²½ λœλ‹€.
update member set email = 'eee@gmail.com';

//아이디가 abc123인 ν–‰μ˜ μ΄λ©”μΌλ§Œ abc@naver.com으둜 λ³€κ²½
update member set email = 'abc@naver.com' where id ='abc123';

// 2개 μ΄μƒμ˜ 컬럼 λ³€κ²½ 아이디가 eee555인 μ‚¬λžŒμ˜ pw와 nickname λ³€κ²½
update member set pw = '1234', nickname = '이티' where id = 'eee555';

delete

ν…Œμ΄λΈ”μ˜ 행을 μ‚­μ œν•˜λŠ” ꡬ문 β†’ ν–‰μ˜ κ°œμˆ˜μ— λ³€ν™”κ°€ 생김

  • 쑰건문을 걸어주지 μ•ŠμœΌλ©΄ ν…Œμ΄λΈ”μ˜ λͺ¨λ“  데이터가 μ‚­μ œλ¨.
  • delete from ν…Œμ΄λΈ”λͺ… where 쑰건
// idκ°€ 200인 μ‚¬λžŒ μ‚­μ œ
delete from member where id = '200';

// μ „μ²΄μ˜ ν…Œμ΄λΈ” μ‚­μ œ
delete from member;

truncate

ν…Œμ΄λΈ”μ˜ 전체 행을 μ‚­μ œν•  λ•Œ μ‚¬μš©ν•˜λŠ” ꡬ문

  • 되돌릴 수 μ—†μŒ β†’ 영ꡬ적으둜 μ‚­μ œ
// rollback / commit
commit;
select * from member;
delete from member;
rollback;

2️⃣ TCL (Transcation Control Language)

ν•œλ²ˆμ— μˆ˜ν–‰λ˜μ–΄μ•Όν•˜λŠ” μž‘μ—…μ˜ λ‹¨μœ„

ex) ATM

  1. μΉ΄λ“œ μ‚½μž…
  2. 메뉴 선택(인좜)
  3. κΈˆμ•‘ 확인 / λΉ„λ°€λ²ˆν˜Έ 인증
  4. μ‚¬μš©μžκ°€ μž…λ ₯ν•œ κΈˆμ•‘μ΄ ν•΄λ‹Ή κ³„μ’Œμ—μ„œ 뽑을 수 μžˆλŠ” κΈˆμ•‘μΈμ§€ 확인
  5. μ‹€μ œ ν˜„κΈˆ λΏ…
  6. μΉ΄λ“œ 뽑고 끝

== ν˜„κΈˆμ„ μΈμΆœν•œλ‹€ μž‘μ—…

β†’ 6λ²ˆκΉŒμ§€ μž‘μ—…μ΄ μ •μƒμ μœΌλ‘œ μ™„λ£Œκ°€ 됐을 λ•Œ β†’ COMMIT (μ΅œμ’… μ €μž₯)

β†’ 6λ²ˆκΉŒμ§€μ˜ μž‘μ—… μ€‘μ—μ„œ ν•˜λ‚˜λΌλ„ 비정상 흐름이 λ°œμƒν•˜λ©΄ κ·Έλ•ŒλŠ” λͺ¨μ€ μž‘μ—…μ„ rollback(μ·¨μ†Œ)

  • commit : νŠΈλžœμž­μ…˜ μž‘μ—…μ΄ μ •μƒμ μœΌλ‘œ μ™„λ£Œλ˜λ©΄ λ³€κ²½ λ‚΄μš©μ„ 영ꡬ적으둜 μ €μž₯
  • savepoint : ν˜„μž¬ νŠΈλžœμž­μ…˜ μž‘μ—… μ‹œμ μ—λ‹€κ°€ 이름 λΆ€μ—¬ (ν•˜λ‚˜μ˜ νŠΈλžœμž­μ…˜ μ•ˆμ—μ„œ ꡬ역을 λ‚˜λˆ„λŠ” 것)
  • rollback : νŠΈλžœμž­μ…˜ μž‘μ—…μ„ λͺ¨λ‘ μ·¨μ†Œν•˜κ³  μ΅œκ·Όμ— commit ν–ˆλ˜ μ§€μ μœΌλ‘œ λŒμ•„κ°€λŠ” 것.

rollback to savepoint λͺ… : ν•΄λ‹Ή savepoint둜 λ˜λŒμ•„ κ°„λ‹€

// ν…Œμ΄λΈ” 생성
create table tbl_user (
    no number unique
    , id varchar2(100) primary key
    , pw varchar2(100) not null
);

// 데이터 μ‚½μž…
insert into tbl_user values(1,'user1','pw1');
insert into tbl_user values(2,'user2','pw2');
insert into tbl_user values(3,'user3','pw3');

select * from tbl_user;

commit;

insert into tbl_user values(4,'user4','pw4 ');

rollback; // κ°€μž₯ μ΅œμ‹ μ— commit된 μƒνƒœλ‘œ λŒμ•„κ° 즉 user3 κΉŒμ§€λ§Œ λ‚˜μ˜΄
insert into tbl_user values(4,'user4','pw4 ');
savepoint spl;
insert into tbl_user values(5,'user5','pw5');

rollback to spl; // save 포인트λ₯Ό ν•΄μ€€κ±°λŠ” user4 κΉŒμ§€μž„
select * from tbl_user; // user4κΉŒμ§€λ§Œ λ‚˜μ˜΄
rollback; // savepoint ν–ˆλ˜κ²ƒλ„ μ•ˆλ‚˜μ˜΄ 였직 commit ν–ˆμ„ λ•Œμ˜ λ°μ΄ν„°λ§Œ 좜λ ₯

3️⃣ DCL(Data Control Language)

데이터 μ œμ–΄μ–΄

λ°μ΄ν„°λ² μ΄μŠ€μ— κ΄€ν•œ λ³΄μ•ˆ, 무결성, 볡ꡬ λ“± DBMSλ₯Ό μ œμ–΄ν•˜κΈ° μœ„ν•œ μ–Έμ–΄

β†’ grant, revoke / TCL(commit, rollback)

grant : μ‚¬μš©μž λ˜λŠ” role(resource, connect, dba)에 κΆŒν•œ λΆ€μ—¬

β†’ system/κ΄€λ¦¬μž 계정 접속 β†’ μ‹œκ·œ μ‚¬μš©μž 생성 β†’ grant 접속 κΆŒν•œ λΆ€μ—¬ β†’ λ¦¬μ†ŒμŠ€ κΆŒν•œ λΆ€μ—¬

// kh κ³„μ •μ—μ„œ μ‹€ν–‰
drop table coffee;
create table coffee(
    name varchar2(50) primary key
    ,price number not null
    ,brand varchar2(100) not null
);

// kh κ³„μ •μ—μ„œ μ‹€ν–‰
insert into coffee values ('μΉ΄νŽ˜λΌλ–Ό',3500,'NESCAFE');
select * from coffee;
commit;

select * from kh.coffee;

// kh κ³„μ •μ—μ„œ μ‹€ν–‰
// system κ³„μ •μ—μ„œ test01 κ³„μ •ν•œν…Œ kh 계정이 가지고 μžˆλŠ” coffee ν…Œμ΄λΈ”μ— μ ‘κ·Ό κΆŒν•œ λΆ€μ—¬
grant select on kh.coffee to test01;
commit;

// kh κ³„μ •μ—μ„œ μ‹€ν–‰
// system κ³„μ •μ—μ„œ test01 κ³„μ •ν•œν…Œ kh κ³„μ •μ˜ coffee ν…Œμ΄λΈ”μ— λŒ€ν•œ insert κΆŒν•œ λΆ€μ—¬
grant insert on kh.coffee to test01;
commit;

// testκ³„μ •μ—μ„œ μ‹€ν–‰
insert into kh.coffee values('바닐라라떼',6000,'starbucks');
select * from kh.coffee;

// kh κ³„μ •μ—μ„œ μ‹€ν–‰
// revoke -> λΆ€μ—¬λœ κΆŒν•œμ„ ν•΄μ œ / νšŒμˆ˜ν•˜λŠ” λͺ…λ Ήμ–΄
revoke select, insert on kh.coffee from test01;

// system κ³„μ •μ—μ„œ μ‹€ν–‰ kh의 κΆŒν•œμ„ 확인 ν•˜λŠ” μž‘μ—…
select * from dba_role_privs where grantee ='KH';

4️⃣ view

ν•˜λ‚˜ μ΄μƒμ˜ ν…Œμ΄λΈ”μ—μ„œ μ›ν•˜λŠ” 데이터λ₯Ό μ„ νƒν•΄μ„œ μƒˆλ‘œμš΄ 가상 ν…Œμ΄λΈ”μ„ λ§Œλ“€μ–΄ μ£ΌλŠ” 것

  • λ·°λ₯Ό 톡해 λ§Œλ“€μ–΄μ§„ ν…Œμ΄λΈ”μ΄ 물리적으둜 μ‘΄μž¬ν•˜λŠ” 것은 μ•„λ‹ˆκ³ , λ‹€λ₯Έ ν…Œμ΄λΈ”μ˜ λ°μ΄ν„°λ§Œ μ‘°ν•©ν•˜μ—¬ λ³΄μ—¬μ£ΌλŠ” 것
  • νŠΉμ • 계정이 원본 ν…Œμ΄λΈ”μ— μ ‘κ·Όν•΄μ„œ λͺ¨λ“  ν…Œμ΄ν„°(λΆˆν•„μš”ν•œ 데이터)에 μ ‘κ·Όν•˜λŠ” κ±Έ 방지
  • λ·°λ₯Ό μƒμ„±ν•˜λŠ” κΆŒν•œ β†’ 뷰의 λ‚΄μš©μ„ μˆ˜μ •ν•˜λ©΄ β†’ μ‹€μ œ 원본 ν…Œμ΄λΈ”μ˜ 데이터도 μˆ˜μ •λ¨

πŸ’‘ create view 뷰이름 as select ꡬ문

  • μ‹€μ œ ν…Œμ΄λΈ”μ—μ„œ 데이터λ₯Ό λ°”κΎΈλ©΄ view ν…Œμ΄λΈ”μ—μ„œλ„ 데이터가 바뀐닀
// λ·° 생성 κΆŒν•œ λΆ€μ—¬
grant create view to kh;

// λ·° ν…Œμ΄λΈ” 생성
// employee -> emp_no, emp_name, email ,phone
create view emp_view as select emp_no, emp_name, email, phone from employee;
select * from emp_view;

// khκ³„μ •μ—μ„œ μ‹€ν–‰
// test01 κ³„μ •ν•œν…Œ μœ„μ—μ„œ λ§Œλ“  emp_view에 μ ‘κ·Ό x κ·Έλž˜μ„œ 접근을 μ£ΌλŠ”κ²ƒ
grant select on kh.emp_view to test01;
commit;

// testκ³„μ •μ—μ„œ μ‹€ν–‰
select * from kh.emp_view;

// testκ³„μ •μ—μ„œ μ‹€ν–‰
select * from kh.emp_view;

// khκ³„μ •μ—μ„œ μ‹€ν–‰
// 선동일 -> 이름을 κΉ€λ™μΌμœΌλ‘œ μˆ˜μ •
update employee set emp_name = '김동일' where emp_name ='선동일';
select * from employee;
commit;

// khκ³„μ •μ—μ„œ μ‹€ν–‰
// λ·° μ‚­μ œ
drop view emp_view;
commit;

5️⃣ sequence

순차적으둜 μ •μˆ˜ 값을 μžλ™μœΌλ‘œ μƒμ„±ν•˜λŠ” 객체 -> μžλ™ 번호 λ°œμƒκΈ°

  1. start with 숫자 -> λͺ‡λ²ˆλΆ€ν„° 번호λ₯Ό μ‹œμž‘ν• κ±΄μ§€
  2. increment by 숫자 -> λͺ‡ λ‹¨μœ„λ‘œ 숫자λ₯Ό μ¦κ°€μ‹œν‚¬κ±΄μ§€
  3. maxvalue 숫자 / nomaxvalue -> μ‹œν€€μŠ€μ˜ μ΅œλŒ€κ°’ 지정 / 지정x
  4. minvalue 숫자 / nominvalue -> μ‹œν€€μŠ€μ˜ μ΅œμ†Œκ°’ 지정 / 지정x
  5. cycle / nocycle -> λ§Œμ•½ μ΅œλŒ€κ°’μ— λ„λ‹¬ν•˜λ©΄ 처음으둜 λŒμ•„κ°€ λ‹€μ‹œ μˆœλ²ˆμ„ 맀기기 μ‹œμž‘ν• κ±΄μ§€
  6. cache / nocache -> λ©”λͺ¨λ¦¬μƒμ— 미리 μ‹œν€€μŠ€λ₯Ό 뽑아 μ˜¬λ €λ‘κ³  μ‚¬μš©ν•˜λŠ” 것/ λ©”λͺ¨λ¦¬μƒμ— μ˜¬λ €λ†“μ§€ x
create sequence seq_temp
    start with 1
    increment by 1
    maxvalue 10
    cycle
    nocache;

select * from user_sequences where sequence_name = 'SEQ_TEMP';

select seq_temp.currval from dual; // μ—λŸ¬ nextvalλ¨Όμ € μ‚¬μš© ν•΄μ•Όν•œλ‹€
select seq_temp.nextval from dual;
select seq_temp.currval from dual;
  • nextval : ν˜„μž¬ μ‹œν€€μŠ€μ˜ λ‹€μŒ 값을 λ°˜ν™˜ν•¨κ³Ό λ™μ‹œμ— μ‹œν€€μŠ€λ₯Ό 증가
  • currval : ν˜„μž¬κ°’μ„ λ°˜ν™˜ β†’ μ ‘μ†ν•˜κ³ λ‚˜μ„œ nextval이 λ‹¨ν•œλ²ˆλ„ 쓰이지 μ•Šμ•˜λ‹€λ©΄ μ‚¬μš© x (nextvalλ¨Όμ € μ‚¬μš© ν•΄μ•Όν•œλ‹€)

1) ν™œμš©


drop sequence seq_temp;
create sequence seq_temp
    start with 1
    increment by 1
    maxvalue 10
    cycle
    nocache;

select * from user_sequences where sequence_name = 'SEQ_TEMP';

select seq_temp.nextval from dual;
select seq_temp.currval from dual;

select * from coffee;

delete from coffee where name in ('1','2');
// insert into coffee values(1,4000,'Max');
// insert into coffee values(2,4000,'Max');
// μœ„μ˜ μ½”λ“œμ²˜λŸΌ ν•˜μ§€μ•Šκ³  seqλ₯Ό μ‚¬μš©
insert into coffee values(seq_temp.nextval,4000,'Max');
insert into coffee values(seq_temp.nextval,4000,'Max');

Categories:

Updated:

Leave a comment