๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[Lecture] DataBase - Day 10~11 ๋ณธ๋ฌธ

Development Study

[Lecture] DataBase - Day 10~11

f1r3_r41n 2023. 12. 29. 18:05
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ–ฅ DataBase - Day 10

๐Ÿ‘‰๐Ÿป [Chapter 13] ๋ทฐ(View)

[01] ๋ทฐ์˜ ๊ฐœ๋…

  • ๋ทฐ
    • ๊ฐ€์ƒ ํ…Œ์ด๋ธ”(virtual table)
    • select ๋ฌธ์„ ์ €์žฅํ•œ ๊ฐ์ฒด
  • ๋ทฐ์˜ ์‚ฌ์šฉ ๋ชฉ์ 
    • ํŽธ์˜์„ฑ : select ๋ฌธ์˜ ๋ณต์žก๋„ ์™„ํ™”
    • ๋ณด์•ˆ์„ฑ : ํ…Œ์ด๋ธ”์˜ ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋งŒ ๋…ธ์ถœ

[02] ๋ทฐ ์ƒ์„ฑ

create [or replace][force | noforce] view ๋ทฐ์ด๋ฆ„ as sub-query
[with check option [constraint ์ œ์•ฝ์กฐ๊ฑด]]
[with read only]
  • ๋ทฐ์˜ ์ƒ์„ฑ
    • or replace : ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ทฐ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์‚ญ์ œ ํ›„ ๋‹ค์‹œ ์ƒ์„ฑ
    • force : ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์˜ ์กด์žฌ ์—ฌ๋ถ€์— ์ƒ๊ด€์—†์ด ๋ทฐ ์ƒ์„ฑ
    • noforce : ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์ด ์กด์žฌํ•  ๊ฒฝ์šฐ์—๋งŒ ๋ทฐ ์ƒ์„ฑ, ๊ธฐ๋ณธ ๊ฐ’
    • with check option : ์ฃผ์–ด์ง„ ์ œ์•ฝ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ž…๋ ฅ ๋ฐ ์ˆ˜์ • ํ—ˆ์šฉ
    • with read only : select ๋งŒ ๊ฐ€๋Šฅํ•œ ์ฝ๊ธฐ ์ „์šฉ ๋ทฐ ์ƒ์„ฑ
grant create view to [account_name];
  • ๊ณ„์ •์— ๋ทฐ ์ƒ์„ฑ ๊ถŒํ•œ ๋ถ€์—ฌ

[03] ๋ทฐ ์‚ญ์ œ

drop view ๋ทฐ;
  • ๋ทฐ ์‚ญ์ œ ์ฟผ๋ฆฌ

๐Ÿ‘‰๐Ÿป [Chapter 16] PL/SQL ๊ธฐ์ดˆ

[01] PL/SQL ๊ตฌ์กฐ

  • ๋ธ”๋ก(block)
    • PL/SQL ํ”„๋กœ๊ทธ๋žจ์˜ ๊ธฐ๋ณธ ๋‹จ์œ„
  • PL/SQL ์‚ฌ์šฉ ์‹œ ์ฃผ์˜์‚ฌํ•ญ
    • declare, begin, exception ํ‚ค์›Œ๋“œ์—๋Š” ; ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ
    • PL/SQL ๋ธ”๋ก์˜ ๊ฐ ๋ถ€๋ถ„์—์„œ ์‹คํ–‰ํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์žฅ ๋์—๋Š” ; ์‚ฌ์šฉ
    • PL/SQL ๋ฌธ ๋‚ด๋ถ€์—์„œ ํ•œ ์ค„ ์ฃผ์„๊ณผ ์—ฌ๋Ÿฌ ์ค„ ์ฃผ์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • PL/SQL ๋ฌธ ์ž‘์„ฑ์„ ๋งˆ์น˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋งˆ์ง€๋ง‰์— / ์‚ฌ์šฉ
  • PL/SQL ์ฃผ์„
    • - : ํ•œ ์ค„ ์ฃผ์„
    • /* ... */ : ์—ฌ๋Ÿฌ ์ค„ ์ฃผ์„
declare
    [์‹คํ–‰์— ํ•„์š”ํ•œ ์—ฌ๋Ÿฌ ์š”์†Œ ์„ ์–ธ];
begin
    [์ž‘์—…์„ ์œ„ํ•ด ์‹ค์ œ ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด];
exception
    [PL/SQL ์ˆ˜ํ–‰ ๋„์ค‘ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ];
end;
  • declare : ์„ ํƒ
    • ์‹คํ–‰์— ์‚ฌ์šฉ๋  ๋ณ€์ˆ˜, ์ƒ์ˆ˜, ์ปค์„œ ๋“ฑ ์„ ์–ธ
  • begin : ํ•„์ˆ˜
    • ์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ, select, DML, ํ•จ์ˆ˜ ๋“ฑ ์ •์˜
  • exception : ์„ ํƒ
    • PL/SQL ์‹คํ–‰ ๋„์ค‘ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜(์˜ˆ์™ธ ์ƒํ™ฉ) ์ฒ˜๋ฆฌ
set serveroutput on;
  • PL/SQL ๊ฒฐ๊ณผ๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ์‚ฌ์ „์ž‘์—… ํ•„์š”
    • ๊ธฐ๋ณธ์ ์œผ๋กœ PL/SQL ์‹คํ–‰๊ฒฐ๊ณผ๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜์ง€ ์•Š์Œ
    • ์œ„์˜ ์ฟผ๋ฆฌ๋ฅผ ์ ์šฉํ•˜๋ฉด ํ™”๋ฉด ์ถœ๋ ฅ ๊ธฐ๋Šฅ์ด ํ™œ์„ฑํ™”๋จ

[02] ๋ณ€์ˆ˜์™€ ์ƒ์ˆ˜

๋ณ€์ˆ˜๋ช… ์ž๋ฃŒํ˜• := ๊ฐ’ ๋˜๋Š” ํ‘œํ˜„์‹
v_empno number(4) := 7788;
  • ๋ณ€์ˆ˜ ์„ ์–ธ
  • ๋ณ€์ˆ˜๋ช… ์ •ํ•˜๊ธฐ
    • ๊ฐ™์€ ๋ธ”๋ก ์•ˆ์—์„œ ์‹๋ณ„์ž๋Š” ๊ณ ์œ ํ•ด์•ผ ํ•จ
    • ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ณ„ํ•˜์ง€ ์•Š์Œ
    • ํ…Œ์ด๋ธ”๋ช… ๊ทœ์น™์„ ๋”ฐ๋ฆ„
  • ๋ณ€์ˆ˜์˜ ์ž๋ฃŒํ˜•
    • ์Šค์นผ๋ผํ˜•(scalar type)
      • ์ˆซ์ž, ๋ฌธ์ž, ๋‚ ์งœ ๋“ฑ๊ณผ ๊ฐ™์ด ์˜ค๋ผํด ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•
    • ์ฐธ์กฐํ˜•(reference type)
      • ํ…Œ์ด๋ธ” ์—ด์˜ ์ž๋ฃŒํ˜•์ด๋‚˜ ํ•˜๋‚˜์˜ ํ–‰ ๊ตฌ์กฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์ž๋ฃŒํ˜•
      • ์—ด ์ฐธ์กฐ : %type
        • ๋ณ€์ˆ˜๋ช… ํ…Œ์ด๋ธ”๋ช….์—ด์ด๋ฆ„%type
      • ํ–‰ ์ฐธ์กฐ : %rowtype
        • ๋ณ€์ˆ˜๋ช… ํ…Œ์ด๋ธ”๋ช…%rowtype
    • ๋ณตํ•ฉํ˜•(composite type)
      • ์ปฌ๋ ‰์…˜, ๋ ˆ์ฝ”๋“œ
    • LOBํ˜•(Large Object type)
      • ๋Œ€์šฉ๋Ÿ‰์˜ ํ…์ŠคํŠธ, ์ด๋ฏธ์ง€, ๋™์˜์ƒ ๋“ฑ
๋ณ€์ˆ˜๋ช… constant ์ž๋ฃŒํ˜• := ๊ฐ’ ๋˜๋Š” ํ‘œํ˜„์‹
v_tax constant number(1) := 3;
  • ์ƒ์ˆ˜ ์„ ์–ธ
๋ณ€์ˆ˜๋ช… ์ž๋ฃŒํ˜• default ๊ฐ’ ๋˜๋Š” ํ‘œํ˜„์‹
v_deptno number(2) default 10;
  • ๋ณ€์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’ ์ง€์ •
๋ณ€์ˆ˜๋ช… ์ž๋ฃŒํ˜• not null := ๋˜๋Š” default ๊ฐ’ ๋˜๋Š” ํ‘œํ˜„์‹
v_deptno number(2) not null := 10;
v_deptno number(2) not null default 10;
  • ๋ณ€์ˆ˜์— null ๊ฐ’ ์ €์žฅ ์ œํ•œ

[03] ์กฐ๊ฑด ์ œ์–ด๋ฌธ

1.
if <condition> then
    {command}
end if;

2.
if <condition> then
    {command}
else
    {command}
end if;

3.
if <condition> then
    {command}
elsif <condition>
    {command}
...
else
    {command}
end if;
  • if ์กฐ๊ฑด๋ฌธ
case ๋น„๊ต๊ธฐ์ค€
    when ๊ฐ’1 then
        {command};
    ...
    when ๊ฐ’n then
        {command};
end case;
  • ๋‹จ์ˆœ case ๋ฌธ
case
    when ๊ฐ’1 then
        {command};
    ...
    when ๊ฐ’n then
        {command};
end case;
  • ๊ฒ€์ƒ‰ case ๋ฌธ

[04] ๋ฐ˜๋ณต ์ œ์–ด๋ฌธ

loop
    ๋ฐ˜๋ณต์ˆ˜ํ–‰์ž‘์—…;
end loop;
  • ๊ธฐ๋ณธ loop
    • exit ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜๋ณต ์ข…๋ฃŒ
while ์กฐ๊ฑด์‹ loop
    ๋ฐ˜๋ณต์ˆ˜ํ–‰์ž‘์—…;
end loop;
  • while loop
for i in ์‹œ์ž‘๊ฐ’..์ข…๋ฃŒ๊ฐ’ loop
    ๋ฐ˜๋ณต์ˆ˜ํ–‰์ž‘์—…;
end loop;
  • for loop
for i in ์‹œ์ž‘๊ฐ’..์ข…๋ฃŒ๊ฐ’ loop
    continue ...;
    ๋ฐ˜๋ณต์ˆ˜ํ–‰์ž‘์—…;
end loop;
  • continue ๋ฌธ, continue-when ๋ฌธ ํฌํ•จ ๋ฐ˜๋ณต๋ฌธ
    • ์˜ค๋ผํด 11g๋ถ€ํ„ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅ



๐Ÿ–ฅ DataBase - Day 11

๐Ÿ‘‰๐Ÿป [Chapter 19] ์ €์žฅ ์„œ๋ธŒํ”„๋กœ๊ทธ๋žจ

[01] Stored Procedure : insert ๋ฌธ

create [or replace] procedure ํ”„๋กœ์‹œ์ €์ด๋ฆ„
is|as
(
๋ณ€์ˆ˜์ด๋ฆ„ ๋ฐ์ดํ„ฐํƒ€์ž…, -- ํ”„๋กœ์‹œ์ € ๋‚ด์—์„œ ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜ ์„ ์–ธ
๋ณ€์ˆ˜์ด๋ฆ„ ๋ฐ์ดํ„ฐํƒ€์ž…,
...
)
begin
๊ธฐ๋Šฅ ๊ตฌํ˜„;
end;
  • ํ”„๋กœ์‹œ์ € ์ƒ์„ฑ ์ฟผ๋ฆฌ
exec ์ €์žฅํ”„๋กœ์‹œ์ €[(๊ฐ’1, ๊ฐ’2, ..., ๊ฐ’n)];
  • ํ”„๋กœ์‹œ์ €์— ์ •๋ณด ์ถ”๊ฐ€ ์ฟผ๋ฆฌ
create table dept_b
as select * from scott.dept;

select * from dept_b;

create or replace procedure insertdept(
    myDeptno in number,
    myDname in varchar2,
    myLoc in varchar2
)
as
begin
    insert into dept_b(deptno, dname, loc)
    values(myDeptno, myDname, myLoc);
end;

exec insertdept(50, '๊ฐœ๋ฐœ๋ถ€', 'Seoul');

select * from dept_b;
  • insert ๋ฌธ

[02] Stored Procedure : update ๋ฌธ

create or replace procedure updatedept(
    myDeptno in number,
    myDname in varchar2
)
as
begin
    update dept_b
    set dname = myDname
    where deptno = myDeptno;
end;

exec updatedept(50, '๊ฐœ๋ฐœ๋ถ€-2');
  • update ๋ฌธ
create or replace procedure insertorupdatedept(
    myDeptno in number,
    myDname in varchar2,
    myLoc in varchar2
)
as
    myCount number;
begin
    select count(*) into myCount
    from dept_b
    where deptno = myDeptno;

    if myCount!=0 then
        update dept_b set dname = myDname
        where deptno = myDeptno;
    else
        insert into dept_b(deptno, dname, loc)
        values(myDeptno, myDname, myLoc);
    end if;
end;

exec insertorupdatedept(50, 'ํ™๋ณด๋ถ€', 'Jeju');
exec insertorupdatedept(50, '๊ฐœ๋ฐœ๋ถ€', 'Jeju');
  • ์กฐ๊ฑด์— ์˜ํ•ด insert ํ˜น์€ update

[03] Stored Procedure : delete ๋ฌธ

create or replace procedure deletedept(
    myDeptno in number
)
as
begin
    delete from dept_b
    where deptno = myDeptno;
end;

exec deletedept(50);
  • delete ๋ฌธ
set serveroutput on;

create or replace procedure deletedept2(
    myDeptno in number
)
as 
    myCount number;
begin
    select count(*) into myCount
    from dept_b where deptno = myDeptno;

    if myCount!=0 then
        dbms_output.put_line('๋ถ€์„œ๋ฒˆํ˜ธ๊ฐ€ '||myDeptno||'์ธ ๋ถ€์„œ ์ •๋ณด๊ฐ€ ์‚ญ์ œ๋จ.');
        delete from dept_b
        where deptno = myDeptno;
    else
        dbms_output.put_line('์‚ญ์ œํ•˜๋ ค๋Š” ๋ถ€์„œ ์ •๋ณด๊ฐ€ ์—†์Œ');
    end if;
end;

exec deletedept2(70);
exec deletedept2(50);
  • delete ๋ฌธ

[04] Stored Procedure

create table emp_t
as select * from scott.emp;

set serveroutput on;

create or replace procedure showcomm(
    myEmpno in number
)
as
    vComm number;
begin
    select comm into vComm
    from emp_t
    where empno = myEmpno;

    if vComm!=0 then
        dbms_output.put_line('์ง์›๋ฒˆํ˜ธ๊ฐ€ '||myEmpno||'์ธ ์ง์›์˜ comm์€ '||vComm);
    else
        dbms_output.put_line('์ง์›๋ฒˆํ˜ธ๊ฐ€ '||myEmpno||'์ธ ์ง์›์˜ comm์€ ์—†์Œ');
    end if;
end;

exec showcomm(7369);
exec showcomm(7499);
  • ํŠน์ • ์ง์›์˜ comm ์ถœ๋ ฅ
create or replace procedure avgsal(
    myAvgsal out number
)
as
begin
    select avg(sal) into myAvgsal from emp_t;
end;

set serveroutput on;
declare
    vAvgsal number;
begin
    avgsal(vAvgsal);
    dbms_output.put_line('ํ‰๊ท ๊ธ‰์—ฌ '||vAvgsal);
end;
  • ํ‰๊ท  ๊ธ‰์—ฌ ๋ฐ˜ํ™˜
create or replace procedure calcAvgsal
as
    vAvgsal number;
begin
    select avg(sal) into vAvgsal
    from emp_t;
    dbms_output.put_line('ํ‰๊ท ๊ธ‰์—ฌ : '||vAvgsal);
end;

exec calcAvgsal;
  • ์ง์›๋“ค์˜ ํ‰๊ท  ๊ธ‰์—ฌ ์ถœ๋ ฅ
728x90
๋ฐ˜์‘ํ˜•

'Development Study' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Lecture] DataBase - Day 9  (1) 2023.12.29
[Lecture] DataBase - Day 6~8  (1) 2023.12.29
[Lecture] DataBase - Day 5  (0) 2023.12.29
[Lecture] DataBase - Day 4  (0) 2023.12.29
[Lecture] DataBase - Day 3  (1) 2023.12.29
Comments