TIL 23.09.07

ν™©μ€ν•˜Β·2023λ…„ 9μ›” 7일
0

TIL

λͺ©λ‘ 보기
75/146

πŸ“ŒToday I Learned

였라클(plsql)

ex30_plsql - 읡λͺ… ν”„λ‘œμ‹œμ €

PL/SQL

  • Oracle's Procedural Language extension to SQL
  • 기쑴의 ANSI-SQL + 절차 지ν–₯ μ–Έμ–΄ κΈ°λŠ₯ μΆ”κ°€
  • ANSI-SQL + ν™•μž₯팩(λ³€μˆ˜, μ œμ–΄ 흐름(μ œμ–΄λ¬Έ), 객체(λ©”μ„œλ“œ) μ •μ˜)

ν”„λ‘œμ‹œμ €, Procedure

  • λ©”μ„œλ“œ, ν•¨μˆ˜ λ“±...
  • μˆœμ„œκ°€ μžˆλŠ” λͺ…λ Ήμ–΄μ˜ 집합
  • λͺ¨λ“  PL/SQL ꡬ문은 ν”„λ‘œμ‹œμ € λ‚΄μ—μ„œλ§Œ μž‘μ„±/λ™μž‘μ΄ κ°€λŠ₯ν•˜λ‹€.
  • ν”„λ‘œμ‹œμ € μ•„λ‹Œ μ˜μ—­ > ANSI-SQL

1. 읡λͺ… ν”„λ‘œμ‹œμ €

- 1회용 μ½”λ“œ μž‘μ„±μš©

2. μ‹€λͺ… ν”„λ‘œμ‹œμ €

- λ°μ΄ν„°λ² μ΄μŠ€ 객체
- μ €μž₯용
- 재호좜

PL/SQL ν”„λ‘œμ‹œμ € ꡬ쑰

  1. 4개의 λΈ”λŸ­(ν‚€μ›Œλ“œ)으둜 ꡬ성
    • DECLARE
    • BEGIN
    • EXCEPTION
    • END
  1. DECLARE
    • μ„ μ–ΈλΆ€
    • ν”„λ‘œμ‹œμ € λ‚΄μ—μ„œ μ‚¬μš©ν•  λ³€μˆ˜, 객체 등을 μ„ μ–Έν•˜λŠ” μ˜μ—­
    • μƒλž΅ κ°€λŠ₯
  2. BEGIN ~ END
    • μ‹€ν–‰λΆ€, κ΅¬ν˜„λΆ€
    • κ΅¬ν˜„λœ μ½”λ“œλ₯Ό κ°€μ§€λŠ” μ˜μ—­(λ©”μ„œλ“œμ˜ λ°”λ””λΆ€λΆ„)
    • μƒλž΅ λΆˆκ°€λŠ₯
    • κ΅¬ν˜„λœ μ½”λ“œ > ANSI-SQL + PL/SQL(μ—°μ‚°, μ œμ–΄ λ“±)
  3. EXCEPTION
    • μ˜ˆμ™Έμ²˜λ¦¬λΆ€
    • catch μ—­ν• , 3번 μ˜μ—­ try μ—­ν• 
    • μƒλž΅ κ°€λŠ₯

μžλ£Œν˜• + λ³€μˆ˜

PL/SQL μžλ£Œν˜•

  • ANSI/SQLκ³Ό 동일

λ³€μˆ˜ μ„ μ–Έν•˜κΈ°

  • λ³€μˆ˜ν˜• μžλ£Œν˜• [not null][default κ°’];

PL/SQL μ—°μ‚°μž

  • ANSI/SQLκ³Ό 동일

λŒ€μž… μ—°μ‚°μž

  • ANSI/SQL λŒ€μž… μ—°μ‚°μž
    ex) update table set column = 'κ°’';
  • PL/SQL λŒ€μž… μ—°μ‚°μž
    ex) λ³€μˆ˜ := 'κ°’';

λ³€μˆ˜ > μ–΄λ–€ μš©λ„λ‘œ μ‚¬μš©?
- select κ²°κ³Όλ₯Ό λ‹΄λŠ” μš©λ„(λ§€μš°μ€‘μš”)
- select into 절(PL/SQL)

PL/SQL ν”„λ‘œμ‹œμ € μ•ˆμ—μ„œλŠ” μˆœμˆ˜ν•œ SELECT문은 올 수 μ—†λ‹€.(μ ˆλŒ€)
PL/SQL ν”„λ‘œμ‹œμ € μ•ˆμ—μ„œλŠ” SELECT INTO문만 μ‚¬μš©ν•œλ‹€.

intoλŠ” ν•œλ²ˆλ§Œ.
into μ‚¬μš© μ‹œ
1. 컬럼의 κ°œμˆ˜μ™€ λ³€μˆ˜μ˜ 개수 일치
2. 컬럼의 μˆœμ„œμ™€ λ³€μˆ˜μ˜ μˆœμ„œ 일치
3. 컬럼과 λ³€μˆ˜μ˜ μžλ£Œν˜• 일치

νƒ€μž… μ°Έμ‘°

%type

  • μ‚¬μš©ν•˜λŠ” ν…Œμ΄λΈ”μ˜ νŠΉμ • μ»¬λŸΌκ°’μ˜ μŠ€ν‚€λ§ˆλ₯Ό μ•Œμ•„λ‚΄μ„œ λ³€μˆ˜μ— 적용
  • λ³΅μ‚¬λ˜λŠ” 정보
    • a. μžλ£Œν˜•
    • b. 길이
  • 컬럼 1개 μ°Έμ‘°

%rowtype

  • ν–‰ 전체 μ°Έμ‘°(μ—¬λŸ¬ 개의 μ»¬λŸΌμ„ ν•œλ²ˆμ— μ°Έμ‘°)
  • %type의 μ§‘ν•©ν˜•
  • λ ˆμ½”λ“œ 전체(μ—¬λŸ¬ 개 컬럼)λ₯Ό ν•˜λ‚˜μ˜ λ³€μˆ˜μ— μ €μž₯

μ œμ–΄λ¬Έ
1. 쑰건문
2. 반볡문
3. λΆ„κΈ°λ¬Έ

case문

  • ANSI-SQL의 caseλ¬Έκ³Ό 거의 μœ μ‚¬
  • μžλ°”μ˜ switchλ¬Έ, 닀쀑 ifλ¬Έ

반볡문

  1. loop
  • λ‹¨μˆœ 반볡
declare
    vnum number := 1;
begin
    loop
        dbms_output.put_line(vnum);
        vnum := vnum + 1;
        
        exit when vnum > 10; -- 쑰건뢀 break
        
    end loop;
end;

  1. for loop
  • 횟수 반볡(μžλ°” for)
  • loop 기반
begin
    for dan in 2..9 loop
        for num in 1..9 loop
            insert into tblgugudan (dan, num, result) values (dan, num, dan * num);
        end loop;
    end loop;
end;

  1. while loop
  • 쑰건 반볡(μžλ°” while)
  • loop 기반
declare
    vnum number := 1;
begin 
    while vnum <= 10 loop
        dbms_output.put_line(vnum);
        vnum := vnum + 1;
    end loop;
end;

select > κ²°κ³Όμ…‹ > PL/SQL λ³€μˆ˜ λŒ€μž…

  1. select into
    - κ²°κ³Όμ…‹μ˜ λ ˆμ½”λ“œκ°€ 1개일 λ•Œλ§Œ μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€.
  2. cursor
    - κ²°κ³Όμ…‹μ˜ λ ˆμ½”λ“œκ°€ N개일 λ•Œ μ‚¬μš©ν•œλ‹€.
    - 루프 μ‚¬μš©
declare
    λ³€μˆ˜ μ„ μ–Έ;
    μ»€μ„œ μ„ μ–Έ;
begin
    μ»€μ„œ μ—΄κΈ°;
        loop
            데이터 μ ‘κ·Ό(루프 1νšŒμ „ > λ ˆμ½”λ“œ 1개) <- μ»€μ„œ μ‚¬μš©
        end loop;
    μ»€μ„œ λ‹«κΈ°;
end;
--------------------------------------------------------
-- 걍 μ™Έμ›Œ.
declare
    cursor vcursor
    is
    select * from tblInsa;
    vrow tblInsa%rowtype;
begin
    open vcursor;
        loop
            fetch into;
            exit when;
        end loop;
    close vcursor;
end;


-- 정석
declare
    cursor vcursor
    is
    select * from tblInsa;
    vrow tblInsa%rowtype;
begin
    open vcursor;
        loop
            fetch vcursor into vrow;
            exit when vcursor%notfound;
            
            dbms_output.put_line(vrow.name);
            
        end loop;
    close vcursor;
end;


-- κ°„κ²°
declare
    cursor vcursor
    is
    select * from tblInsa;
    vrow tblInsa%rowtype;
begin
    for vrow in vcursor loop  -- loop + fetch into + vrow + exit when  (for λ£¨ν”„λ³€μˆ˜ in 집합 loop)
        dbms_output.put_line(vrow.name);
    end loop;
end;

μ˜ˆμ™Έμ²˜λ¦¬
: μ‹€ν–‰λΆ€(begin-end)μ—μ„œ λ°œμƒν•˜λŠ” μ˜ˆμ™Έλ₯Ό μ²˜λ¦¬ν•˜λŠ” λΈ”λŸ­ > exception λΈ”λŸ­

-- μ˜ˆμ™Έ λ°œμƒ > DB μ €μž₯
create table tblLog(
    seq number primary key,                 -- PK
    code varchar2(7) not null check (code in ('A001', 'B001', 'B002', 'C001')), --μ—λŸ¬ μƒνƒœ μ½”λ“œ
    message varchar2(1000) not null,        -- μ—λŸ¬ λ©”μ‹œμ§€
    regdate date default sysdate not null   -- μ—λŸ¬ λ°œμƒ μ‹œκ°
);

create sequence seqLog;

select * from tblLog;

declare
    vcnt number;
    vname tblInsa.name%type;
begin
    select count(*) into vcnt from tblCountry where name = 'νƒœκ΅­'; -- ORA-01476: divisor is equal to zero
    dbms_output.put_line(100 / vcnt);
    
    select name into vname from tblInsa where num = 1000; -- ORA-01403: no data found
    dbms_output.put_line(vname);

exception
    when ZERO_DIVIDE then
        dbms_output.put_line('0으둜 λ‚˜λˆ„κΈ°');
        insert into tblLog values (seqLog.nextVal, 'B001', 'κ°€μ Έμ˜¨ λ ˆμ½”λ“œκ°€ μ—†μŠ΅λ‹ˆλ‹€.', default);
        
    when NO_DATA_FOUND then
        dbms_output.put_line('데이터 μ—†μŒ');        
        insert into tblLog values (seqLog.nextVal, 'A001', '직원이 μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.', default);
        
    when others then
        dbms_output.put_line('λ‚˜λ¨Έμ§€ μ˜ˆμ™Έ ');
        insert into tblLog values (seqLog.nextVal, 'C001', '기타 μ˜ˆμ™Έκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.', default);
end;
profile
μ°¨κ·Όμ°¨κ·Ό ν•˜λ‚˜μ”©

0개의 λŒ“κΈ€