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;
๋ฐ์ํ
'IT & Security > DB' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Oracle] ํ๋ก์์ | ์ฌ์ฉ์ ์ ์ํจ์ | ํจ์ ๋ง๋ค๊ธฐ (0) | 2021.05.12 |
---|---|
[DB] ์ค๋ผํด ์๋ธ์ฟผ๋ฆฌ ์์ (0) | 2021.04.13 |
[Oracle DB] ์ง๊ณํจ์ | Join (0) | 2021.03.26 |
[ Oracle DB ] ํจ์ | ๋ด์ฅํจ์ | ์ง๊ณํจ์ | ๋จ์ผํ ํจ์ | ๋ณํํจ์ | ์ฌ์ฉ์ ์ ์ ํจ์ (0) | 2021.03.10 |
Contents
๋น์ ์ด ์ข์ํ ๋งํ ์ฝํ ์ธ
์์คํ ๊ณต๊ฐ ๊ฐ์ฌํฉ๋๋ค