์ƒˆ์†Œ์‹

IT & Security/DB

[์˜ค๋ผํด] ์˜ˆ์ œ ๋ฌธ์ œ

  • -
๋ฐ˜์‘ํ˜•
-- 7-1๋ฒˆ ๋ฌธ์ œ์ž„ ์œ„๋Š” 7๋ฒˆ์ด๊ณ , ์ด๋ฅผ ํ•ฉํ•ด์„œ ๋งŒ๋“  ๊ฒƒ!
create or replace procedure p_insert02(v_empno testemp.empno%type,
                                       v_ename testemp.ename%type,
                                       v_sal testemp.sal%type,
                                       v_deptno testemp.deptno%type)
is 
    v_sumsal testemp.sumsal%type;
begin
    insert into testemp
    values(v_empno,v_ename,v_sal,v_deptno,null,null);
    update testemp
    set sumsal=(select sum(sal)
                from testemp
                where deptno=v_deptno)
    where deptno=v_deptno;
    select sum(sal)
    into v_sumsal
    from testemp
    where deptno=v_deptno;
    if v_deptno=10 then
        v_sumsal:=0;
    end if;
    if v_sumsal >= 10000 then
        dbms_output.put_line('10000 over!');
    end if;
    commit;
exception
    when others then
        dbms_output.put_line(sqlcode||sqlerrm);
        rollback;
end;
/
select * from testemp;
exec p_insert02(9999,'PARK',100,30);


drop table testemp;
create table testemp as 
select empno, ename, sal, deptno from emp;

alter table testemp add grade number(1);
alter table testemp add sumsal number(10);
select *from testemp;


--๋ฌธ์ œ 8๋ฒˆ
create or replace procedure p_delete01(v_empno testemp.empno%type)
is 
    v_deptno testemp.deptno%type;
    v_sumsal testemp.sumsal%type;
    invalid_sumsal exception; --1. ์„ ์–ธ๋ถ€
begin
    select deptno
    into v_deptno
    from testemp
    where empno=v_empno;
    delete from testemp 
    where empno=v_empno;
    
    select sum(sal)
    into v_sumsal --์กฐ๊ฑด์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•จ
    from testemp
    where deptno=v_deptno;
    if v_deptno=10 then
        v_sumsal:=0;
    end if;
    if v_sumsal >= 10000 then
        raise invalid_sumsal; -- 2. ์‹คํ–‰๋ถ€
    end if;
    update testemp
    set sumsal=v_sumsal
    where deptno=v_deptno;
    commit;
exception
    when invalid_sumsal then --3.exception
        dbms_output.put_line('10000 over!');
        rollback;
     when others then
        dbms_output.put_line(sqlcode||sqlerrm);
        rollback;
end;
/

select * from testemp;
exec p_delete01(9998);
exec p_insert02(9998,'KIM',2000,20);

create or replace procedure p_sum02
is
    invalid_sumsal exception; --1.1 ์‚ฌ์šฉ์ž์ •์˜ exception
    v_deptno testemp.deptno%type;
    v_sumsal testemp.sumsal%type;
    cursor testemp_cur is  --2.1 ์„ ์–ธ
        select deptno,sum(sal)
        from testemp
        group by deptno;
begin
    open testemp_cur; --2.2 ์ปค์„œ ์˜คํ”ˆ
    loop 
        fetch testemp_cur into v_deptno,v_sumsal; --2.3 ์ปค์„œ fetch(์ฝ๊ธฐ)
        exit when testemp_cur%notfound;
        if v_sumsal <=10 then
            raise invalid_sumsal; --1.2
        end if;
        update testemp
        set sumsal=v_sumsal
        where deptno = v_deptno;
    end loop;
    close testemp_cur; --2.4 ์ปค์„œ ๋‹ซ๊ธฐ 
    commit;
exception 
   when invalid_sumsal then --1.3
        dbms_output.put_line(v_deptno||' ' ||TO_CHAR(v_sumsal));
        rollback;
    when too_many_rows then -- predefined-exception
        dbms_output.put_line('Try again!!');
        rollback;
    when others then
        dbms_output.put_line(sqlcode||sqlerrm);
        rollback;
end;
/

select * from testemp;
exec p_sum02;
exec p_insert02(9997,'LEE',5,40);


drop table testemp;
create table testemp
as select empno, ename, sal, deptno
    from emp;
create table testdept
as select deptno,dname
    from dept;

--๊ณผ์ œ 1๋ฒˆ
desc user_constraints
select constraint_name,constraint_type,
table_name,search_condition,r_constraint_name
from user_constraints
where table_name in ('TESTDEPT','TESTEMP');

alter table testdept
add constraint pk_testdept_deptno primary key(deptno);

--๊ณผ์ œ 2๋ฒˆ
alter table testemp
add constraint pk_testemp_empno primary key(empno);
--๊ณผ์ œ 3๋ฒˆ
alter table testemp
add constraint fk_testemp_deptno 
foreign key(deptno) references testdept(deptno);

-- 4๋ฒˆ 
alter table testemp
add enumber char(10)  --ํ‚ค ์ƒ์„ฑ ๋™์‹œ์— ์ปฌ๋Ÿผ ์ƒ์„ฑ
constraint uk_testemp_enumber unique;

--5๋ฒˆ(์˜ค๋ผํด์—์„  not null์„ ์ œ์•ฝ์กฐ๊ฑด์œผ๋กœ ๋ณธ๋‹ค)
--not null๋„ ck (์ฒดํฌ)์ œ์•ฝ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜ ์ด๋‹ค.
alter table testemp
modify ename varchar2(10) constraint nn_ename not null;
--constraint ck_testemp_ename not null๊ณผ ๊ฐ™์ด ์ œ์•ฝ์กฐ๊ฑด์„ ์ค˜๋„ ๋จ.

--6๋ฒˆ (์˜ค๋ผํด์—์„œ default ๋Š” ์ œ์•ฝ์กฐ๊ฑด์ด ์•„๋‹ˆ๋‹ค.)
alter table testdept
add dcount number(3) default 0;


--7๋ฒˆ 
alter table testemp
modify enumber char(14);

--8๋ฒˆ
alter table testemp
add egender char(1) default 'm' not null
constraint ck_testemp_egender check(egender in ('m','f'));


select constraint_name,constraint_type,
table_name,search_condition,r_constraint_name
from user_constraints
where table_name in ('TESTDEPT','TESTEMP');

desc user_tab_columns
select table_name,column_name,data_default
from user_tab_columns
where table_name in ('TESTDEPT','TESTEMP');

-- 9๋ฒˆ
alter table testemp
drop constraint SYS_C007227;
alter table testemp
add constraint ck_testemp_egender2
check(egender is not null);

--10๋ฒˆ
drop view v_testdept;
create view v_testdept
as select * 
    from testdept
    where dcount=0;
select * from v_testdept;
--grant create view to scott; ๋ฅผ sys_user์—์„œ ์‹คํ–‰์‹œ์ผœ์ฃผ์–ด ๊ถŒํ•œ์„ ์ค˜์•ผ ์œ„์— create๊ฐ€ ์‹คํ–‰๋จ
select * from testemp;

--11๋ฒˆ
--๋ถ€์„œ์˜ ์ธ์›์ด 10๋ช… ์ดˆ๊ณผ์ด๋ฉด, ์‚ฌ์šฉ์ž ์ •์˜ exception incalid_dcout์„ ์ผ์œผํ‚ค๊ณ 
--'10 over!'์—๋Ÿฌ๋ฉ”์„ธ์ง€์™€ ๋ถ€์„œ๋ฒˆํ˜ธ,์ธ์›์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๋ชจ๋“  ์ž‘์—…์„ ์ทจ์†Œํ•˜๋ผ.
create or replace procedure p_dcount01
is
    cursor dept_cur is
        select deptno,count(*)
        from testemp
        group by deptno;
    v_deptno testemp.deptno%type;
    v_dcount testdept.dcount%type;
    invalid_dcount exception;
begin
    open dept_cur;
    loop
        fetch dept_cur into v_deptno,v_dcount;
        exit when dept_cur%notfound;
        if v_dcount >10 then --ํ…Œ์ŠคํŠธ ํ•˜๋ ค๋ฉด 5๋กœ ํ•ด๋ณด๋ฉด ๋จ
            raise invalid_dcount;
        end if;
        update testdept
        set dcount = v_dcount
        where deptno=v_deptno;
    end loop;
    close dept_cur;
    commit;
exception
    when invalid_dcount then
        dbms_output.put_line
        ('10 over! '||v_deptno||' ' ||v_dcount);
        rollback;
    when others then
        dbms_output.put_line(sqlcode||sqlerrm);
        rollback;
end;
/
exec p_dcount01;
select * from testdept;
update testdept
set dcount =0;
commit;--ํ…Œ์ŠคํŠธ ์ „ ํŠธ๋žœ์ ์…˜ ์ฒ˜๋ฆฌ๋ฅผ ์™„๋ฃŒํ•ด์ฃผ์–ด์•ผํ•จ.

--12 ๋ฒˆ
create or replace procedure p_insert10(v_empno testemp.empno%type,
v_ename testemp.ename%type,v_sal testemp.sal%type,
v_deptno testemp.deptno%type,v_enumber testemp.enumber%type,
v_egender testemp.egender%type)
is 
begin
    insert into testemp
    values(v_empno,v_ename,v_sal,v_deptno,v_enumber,v_egender);
    update testdept
    set dcount = dcount+1
    where deptno= v_deptno;
    commit;
exception
    when others then
        dbms_output.put_line(sqlcode||sqlerrm);
        rollback;
end;
/
select * from testemp;
select * from testdept;
exec p_insert10(9999,'HAEUN',1000,10,'9999-9999','f');


--15๋ฒˆ 
create or replace trigger t_insert10 
before insert on testemp for each row
begin
    update testdept
    set dcount = dcount+1
    where deptno = :new.deptno; --์™ธ๋ถ€ ๋ณ€์ˆ˜๋Š” ๊ผญ ์•ž์— :(์ฝœ๋ก )์„ ๋ถ™์—ฌ์•ผํ•œ๋‹ค.

exception
    when others then
        dbms_output.put_line(sqlcode||sqlerrm);
        rollback;
end;
/

select * from testemp;
select * from testdept;
insert into testemp
values(9998,'KIM',900,10,'9999-9998','f');
commit;


--13๋ฒˆ
create or replace procedure p_delete10(v_empno testemp.empno%type)
is 
    v_deptno testemp.deptno%type;
begin
    select deptno 
    into v_deptno
    from testemp
    where empno = v_empno;
    
    delete from testemp
    where empno = v_empno;
    update testdept
    set dcount = dcount-1
    where deptno = v_deptno;
    commit;
exception
    when others then
        dbms_output.put_line(sqlcode||sqlerrm);
        rollback;
end;
/

select * from testemp;
select * from testdept;
exec p_delete10(9999);

--16๋ฒˆ
create or replace trigger t_delete10
before delete on testemp for each row
begin
    update testdept
    set dcount= dcount-1
    where deptno = :old.deptno; --์™ธ๋ถ€๋ณ€์ˆ˜ ์ฝœ๋ก ์ด ๋“ค์–ด๊ฐ
exception
    when others then
        dbms_output.put_line(sqlcode||sqlerrm);
        rollback;
end;
/
select * from testdept;
select * from testemp;
delete from testemp
where empno=9998;
commit;

desc user_source
select line,text
from user_source
where name='T_DELETE10';
desc user_objects
select object_name,object_type
from user_objects
where object_type='TRIGGER';
desc user_triggers
select * from user_triggers;
๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.