[Day15] Oracle15 [10/18]
λ¬Έμ 1)tbl_score ν
μ΄λΈμ μλ‘μ΄ νμμ μ±μ μ 보λ₯Ό μ μ₯νλ νλ‘μμ : up_insScore
p : num, name, kor, eng, mat μ
λ ₯νμ§ μμΌλ©΄ 0 μ²λ¦¬
μ΄μ , νκ· , λ±κΈ κΉμ§λ μ²λ¦¬κ° λλλ‘ ..
CREATE OR REPLACE PROCEDURE up_insScore
(
pnum IN tbl_score.num%type
, pname IN tbl_score.name%type
, pkor IN tbl_score.kor%type
, peng IN tbl_score.eng%type
, pmat IN tbl_score.mat%type
)
IS
vtot tbl_score.tot%type;
vavg tbl_score.avg%type;
vgrade tbl_score.grade%type;
-- vrank insa.rank%type;
BEGIN
vtot := pkor + peng + pmat;
vavg := vtot / 3;
vgrade := CASE
WHEN vavg >= 90 THEN 'A'
WHEN vavg >= 80 THEN 'B'
WHEN vavg >= 70 THEN 'C'
WHEN vavg >= 60 THEN 'D'
ELSE 'F'
END;
INSERT INTO tbl_score ( num, name, kor, eng, mat, tot, avg, grade )
VALUES ( pnum, pname, pkor, peng, pmat, vtot, vavg, vgrade );
up_rankScore; -- λ λ€λ₯Έ μ μ₯ νλ‘μμ νΈμΆ/μ€ν
COMMIT;
-- EXCEPTION
END;
EXEC UP_INSSCORE( 1102, 'λ°μ μ
', 99,88,67);
SELECT *
FROM tbl_score;
λ¬Έμ 5) λ±μλ₯Ό μ²λ¦¬νλ νλ‘μμ : up_rankScore
CREATE OR REPLACE PROCEDURE up_rankScore
IS
vrank tbl_score.rank%type;
BEGIN
UPDATE tbl_score t
SET rank = ( SELECT COUNT(*)+1 FROM tbl_score WHERE tot > t.tot ) ;
-- WHERE
-- EXCEPTION
END;
EXEC UP_RANKSCORE;
-- κ° νμμ νμ΄~~
λ¬Έμ 2) tbl_score ν
μ΄λΈμ μλ‘μ΄ νμμ μ±μ μ 보λ₯Ό μμ νλ νλ‘μμ : up_updScore p : num, kor, eng, mat μ λ ₯νμ§ μμΌλ©΄ μμ μ μ μ μλ‘ μ²λ¦¬ μ΄μ , νκ· , λ±κΈ κΉμ§λ μ²λ¦¬κ° λλλ‘ .. λ¬Έμ 3) tbl_score ν μ΄λΈμ μμ νλ νλ‘μμ : up_delScore p : num λ¬Έμ 4) tbl_score ν μ΄λΈμ λͺ¨λ νμ μ 보λ₯Ό μ‘°ννλ νλ‘μμ : up_selScore |
-------------------------------------------------------------------------------------
-- PLSQL λ¬Έλ² : IF/WHILE/FOR λ¬Έ := INTO
-- μ) μ μ₯ νλ‘μμ
-- νμκ°μ
μ€ "ID μ€λ³΅ 체ν¬"
-- ID [ keni ] [ID μ€λ³΅μ²΄ν¬ λ²νΌ]
μ΄λ―Έ μ¬μ© μ€μΈ μμ΄λ μ
λλ€.
μ¬μ© κ°λ₯ν μμ΄λ μ
λλ€.
CREATE OR REPLACE PROCEDURE up_idCheck
(
pempno IN emp.empno%type
,pempnoCheck OUT NUMBER -- μ¬μ©κ°λ₯ 0, μ¬μ©λΆκ°λ₯ 1
)
IS
BEGIN
SELECT COUNT(*) INTO pempnoCheck
FROM emp
WHERE empno = pempno;
-- EXCEPTION
END;
-- νλ‘μμ ν
μ€νΈ μ©λμ μ΅λͺ
νλ‘μμ .
DECLARE
vempno emp.empno%type;
vempnoCheck NUMBER(1);
BEGIN
vempno := 7369;
up_idCheck( vempno, vempnoCheck );
-- DBMS_OUTPUT.PUT_LINE( vempnoCheck ); --0 , 1
IF vempnoCheck = 0 THEN
DBMS_OUTPUT.PUT_LINE( 'μ¬μ© κ°λ₯ν μμ΄λ μ
λλ€.' );
ELSE
DBMS_OUTPUT.PUT_LINE( 'μ΄λ―Έ μ¬μ© μ€μΈ μμ΄λ μ
λλ€.' );
END IF;
END;
-- λ¬Έμ ) ID/PWD μ
λ ₯ν΄μ λ‘κ·ΈμΈ μ²λ¦¬
-- empno/ename
-- λ‘κ·ΈμΈ μ±κ³΅/ IDμ‘΄μ¬X / IDμ‘΄μ¬O PWD X
0 1 -1
-- μ μ₯ νλ‘μμ μμ± + ν
μ€νΈ up_logon
CREATE OR REPLACE PROCEDURE up_logon
(
pempno emp.empno%type
, pename emp.ename%type
, plogonCheck OUT NUMBER -- ν¬κΈ° X μ±κ³΅ 0 ID X -1 1
)
IS
vename emp.ename%type;
BEGIN
SELECT COUNT(*) INTO plogonCheck -- 1, 0
FROM emp
WHERE empno = pempno; -- AND ename = pename
IF plogonCheck = 1 THEN -- ID μ‘΄μ¬νλ€.
SELECT ename INTO vename
FROM emp
WHERE empno = pempno;
IF vename = pename THEN
plogonCheck := 0;
ELSE
plogonCheck := 1;
END IF;
ELSE
plogonCheck := -1;
END IF;
-- EXCEPTION
END;
DECLARE
vlogonCheck NUMBER;
BEGIN
--UP_LOGON( 7369, 'SMITH', vlogonCheck);
UP_LOGON( 7369, 'KENIK', vlogonCheck);
--UP_LOGON( 9999, 'SMITH', vlogonCheck);
IF vlogonCheck=0 THEN
DBMS_OUTPUT.PUT_LINE('λ‘κ·ΈμΈ μ±κ³΅');
ELSIF vlogonCheck=1 THEN
DBMS_OUTPUT.PUT_LINE('IDλ μ‘΄μ¬νμ§λ§ pwdκ° μλͺ»λμλ€.');
ELSE
DBMS_OUTPUT.PUT_LINE('IDλ μ‘΄μ¬ X.');
END IF;
END;
-- μ μ₯ ν¨μ( Stored Function )
-- μ£Όλ―Όλ²νΈ -> λ¨μ/μ¬μ λ°ννλ uf_gender μ μ₯ ..
-- λ¬Έμ ) μ£Όλ―Όλ²νΈ -> μλ
μμΌ(yyyy.mm.dd) λ°ννλ uf_birth()
-- λ¬Έμ ) μ£Όλ―Όλ²νΈ -> λ§λμ΄ λ°ννλ uf_age()
SELECT num, name, ssn
, uf_birth(ssn)
, uf_age( ssn ) --
FROM insa;
CREATE OR REPLACE FUNCTION uf_age
(
prrn VARCHAR2
)
RETURN NUMBER
IS
vischeck number(1);
vt_year number(4);
vb_year number(4);
vage number(3);
BEGIN
vischeck := SIGN( TRUNC( SYSDATE ) - TO_DATE( SUBSTR( prrn, 3,4), 'MMDD') );
vt_year := TO_CHAR( SYSDATE , 'YYYY');
vb_year := CASE
WHEN SUBSTR( prrn, 8, 1 ) IN (1,2,5,6) THEN '1900' + SUBSTR( prrn, 1,2)
WHEN SUBSTR( prrn, 8, 1 ) IN (3,4,7,8) THEN '2000' + SUBSTR( prrn, 1,2)
ELSE '1800' + SUBSTR( prrn, 1,2)
END;
vage := CASE VISCHECK
WHEN -1 THEN -- μμΌ μμ§λκ²
vt_year - vb_year-1
ELSE -- 0, 1
vt_year - vb_year
END ;
RETURN vage;
--EXCEPTION
END;
CREATE OR REPLACE FUNCTION uf_birth
(
pssn insa.ssn%type
)
RETURN VARCHAR2
IS
-- 1900,1800,2000
vcentry NUMBER(2);
vgender NUMBER(1);
vbirth VARCHAR2(20);
vssn6 VARCHAR2(6);
BEGIN
vssn6 := SUBSTR( pssn, 0, 6 );
vgender := SUBSTR( pssn, -7, 1);
vcentry := CASE
WHEN vgender IN (1,2,5,6) THEN 19
WHEN vgender IN ( 3,4,7,8) THEN 20
ELSE 18
END;
-- 19911223
vbirth := TO_CHAR( TO_DATE( CONCAT( vcentry, vssn6) ), 'YYYY.MM.DD' );
RETURN vbirth;
--EXCEPTION
END;
--μ) IN, OUT, IN OUT μ
μΆλ ₯ νλΌλ―Έν° X
DECLARE
vphone VARCHAR2(9) := '8765-8653';
BEGIN
up_phone( vphone );
DBMS_OUTPUT.PUT_LINE( vphone ); -- '8765'
END;
CREATE OR REPLACE PROCEDURE up_phone
(
pphone IN OUT VARCHAR2 -- μ
μΆλ ₯μ© νλΌλ―Έν°
)
IS
BEGIN
pphone := SUBSTR( pphone, 0, 4);
--EXCEPTION
END;
-- [νΈλ¦¬κ±°] / μμΈμ²λ¦¬ / ν¨ν€μ§ --
PL/SQL 6κ°μ§ μ’
λ₯
1) μ΅λͺ
νλ‘μμ
2) μ μ₯ νλ‘μμ
3) μ μ₯ ν¨μ
4) νΈλ¦¬κ±°
5) ν¨ν€μ§
6) X
[ νΈλ¦¬κ±° ( trigger ) ]
1. νΈλ¦¬κ±° ? λ°©μμ -> μλ -> μ΄μ~
2. μ΄λ€ μμ
μ (befor) λλ ν( after) νΈλ¦¬κ±°μ μ μν λ‘μ§μ μ€ννλ PL/SQLμ ν μ’
λ₯
3. λμ ν
μ΄λΈμ 미리 νΈλ¦¬κ±°λ₯Ό μ§μ νλ©΄
μ΄λ€ μ΄λ²€νΈ(DML)κ° λ°μν λ μλμΌλ‘ μ§μ λ νΈλ¦¬κ±°κ° μλνλλ‘ ν κ°μ²΄λ₯Ό νΈλ¦¬κ±°λΌ νλ€.
4. μ)
[νΈλμμ
μ²λ¦¬ ]
1) μ
κ³ ν
μ΄λΈ(λμν
μ΄λΈ) + DML(insert) PK μν μ κ³ λ μ§ μ κ³ μλ 1 P001 22.10.18 10 μλμΌλ‘ 2) μ¬κ³ ν μ΄λΈ μ¬κ³ μλ μμ (update) 1 P001 60 |
5. νΈλ¦¬κ±° μμ½μ΄
1) before μμ
μ μ νΈλ¦¬κ±°κ° μλ μ²λ¦¬. 2) after ν 3) for each row : νλ§λ€ μ²λ¦¬ λλ νΈλ¦¬κ±°( ν νΈλ¦¬κ±° ) 4) referencing : μν₯λ°λ νμ κ° μ°Έμ‘° 5) :old μ°Έμ‘° μ 컬λΌμ κ° :new μ°Έμ‘° ν 컬λΌμ κ° |
6. νΈλ¦¬κ±° μ μΈ νμ
CREATE OR REPLACE TRIGGER νΈλ¦¬κ±°λͺ
[ before λλ after ]
νΈλ¦¬κ±°μ΄λ²€νΈ ON λμν
μ΄λΈ
[for each row [ when trigger 쑰건] ]
DECLARE
-- λ³μ μ μΈ
BEGIN
-- μ€ν λΈλ
EXCEPTION
-- μμΈ μ²λ¦¬λΈλ
END;
7. νΈλ¦¬κ±° νμΈ
SELECT *
FROM user_triggers;
8. νΈλ¦¬κ±° μμ±, μμ , νμ±ν/λΉνμ±ν.
9. νΈλ¦¬κ±° μμ )
A ν
μ΄λΈ : insert, update, delete μ΄λ²€νΈκ° λ°μμ νλ©΄
B ν
μ΄λΈ : λ‘κ·Έ κΈ°λ‘μ μλμΌλ‘ λ¨κΈ°λ νΈλ¦¬κ±° μμ±..
CREATE TABLE tbl_trigger1(
id number primary key
, name varchar2(20)
);
CREATE TABLE tbl_trigger2(
memo varchar2(100) -- λ‘κ·Έ κΈ°λ‘
, ilja date default sysdate
);
AFTER νΈλ¦¬κ±° μ μΈ)
-> tbl_trigger1μ insertκ° μΌμ΄λ ν begin μ€ν
CREATE OR REPLACE TRIGGER ut_ex01
AFTER
INSERT ON tbl_trigger1
--FOR EACH ROW ννΈλ¦¬κ±°
--DECLARE
BEGIN
INSERT INTO tbl_trigger2 ( memo ) VALUES ( 'TBL_TRIGGER1 INSERT...' );
-- COMMIT/ROLLBACK X
--EXCEPTION
END;
INSERT INTO tbl_trigger1 VALUES ( 1 , 'νκΈΈλ' );
INSERT INTO tbl_trigger1 VALUES ( 2 , 'κΉκΈΈλ' );
INSERT INTO tbl_trigger1 VALUES ( 3 , 'μ΅κΈ°μ' );
INSERT INTO tbl_trigger1 VALUES ( 4 , 'λ°κΈ°μ' );
-- νΈλ¦¬κ±° + UPDATE
CREATE OR REPLACE TRIGGER ut_ex02
AFTER
UPDATE ON tbl_trigger1
--FOR EACH ROW ννΈλ¦¬κ±°
--DECLARE
BEGIN
INSERT INTO tbl_trigger2 ( memo ) VALUES ( 'TBL_TRIGGER1 UPDATE...' );
-- COMMIT/ROLLBACK X
--EXCEPTION
END;
UPDATE tbl_trigger1
SET name = 'κ΄λ¦¬μ'
WHERE id = 1;
ν©μ³μ νΈλ¦¬κ±° μμ± κ°λ₯! (IF / ELSEIF)
CREATE OR REPLACE TRIGGER ut_ex01
AFTER
INSERT OR UPDATE OR DELETE ON tbl_trigger1
--FOR EACH ROW ννΈλ¦¬κ±°
--DECLARE
BEGIN
-- COMMIT/ROLLBACK X
IF INSERTING THEN
INSERT INTO tbl_trigger2 ( memo ) VALUES ( 'TBL_TRIGGER1 INSERT...' );
ELSIF UPDATING THEN
INSERT INTO tbl_trigger2 ( memo ) VALUES ( 'TBL_TRIGGER1 UPDATE...' );
ELSIF DELETING THEN
INSERT INTO tbl_trigger2 ( memo ) VALUES ( 'TBL_TRIGGER1 DELETE...' );
END IF;
--EXCEPTION
END;
--μμ 2) λ§μ½μ
-- tbl_trigger1 ν
μ΄λΈμ I/U/D μμ
μ 근무μκ°μλ§ ν μ μλλ‘ νκ² λ€.
-- ν /μΌ X 근무μκ°μ μ£Όλ§(ν /μΌ)μ μΈ νκ³ νμΌ( 9:00~18:00 )
-- BEFORE νΈλ¦¬κ±°λ‘ μ μΈ--
CREATE OR REPLACE TRIGGER ut_ex03
BEFORE -- AFTER
INSERT OR UPDATE OR DELETE ON tbl_trigger1
-- FOR EACH ROW ννΈλ¦¬κ±°
-- DECLARE
BEGIN
IF TO_CHAR(SYSDATE, 'DY') IN ('ν ','μΌ')
OR NOT (TO_CHAR(SYSDATE, 'HH24') BETWEEN 9 AND 12 ) THEN
-- κ°μ λ‘ μλ¬ λ°μ -> I,U,D DML λ¬Έλ μ·¨μ.
-- JAVA throw λ¬Έ new Exception();
-- RAISE_APPLICATION_ERROR(μλ¬μ½λ, μλ¬λ©μμ§);
RAISE_APPLICATION_ERROR(-20000, 'μ§κΈμ 근무μκ°μ΄ μλκΈ° λλ¬Έμ μμ
μ μλ©λλ€.');
END IF;
-- EXCEPTION
END;
μ€λ₯ 보κ³
--ORA-20000: μ§κΈμ 근무μκ°μ΄ μλκΈ° λλ¬Έμ μμ
μ μλ©λλ€.
--ORA-06512: at "SCOTT.UT_EX03", line 7
--ORA-04088: error during execution of trigger 'SCOTT.UT_EX03'
-- μμ 3)
create table tbl_trg1
(
hak varchar2(10) primary key
, name varchar2(20)
, kor number(3)
, eng number(3)
, mat number(3)
);
create table tbl_trg2
(
hak varchar2(10) primary key
, tot number(3)
, avg number(5,2)
, constraint fk_test2_hak foreign key(hak) references tbl_trg1(hak)
);
-- μλμΌλ‘ tbl_trg2 ν
μ΄λΈ 1, tot, avg μ²λ¦¬... μ μ₯. (νΈλ¦¬κ±°)
-- ORA-04082: NEW or OLD references not allowed in table level triggers
-- ν
μ΄λΈ λ 벨 νΈλ¦¬κ±°μμλ :NEW, :OLD μ¬μ©ν μ μλ€.
-- ν λ 벨 νΈλ¦¬κ±° FOR EACH ROWλ
-- UPDATE 10λ²λΆμ μ¬μ 3λͺ
-> νΈλ¦¬κ±° 1λ²(ν
μ΄λΈλ 벨) / 3λ²(λ‘μ°λ 벨)
CREATE OR REPLACE TRIGGER ut_trg1dml
AFTER
INSERT ON tbl_trg1
FOR EACH ROW -- ν λ 벨 νΈλ¦¬κ±°
DECLARE
vtot NUMBER(3) ;
vavg NUMBER(5,2) ;
BEGIN
-- 1, 'hong', 90,78, 99
vtot := :NEW.kor + :NEW.eng + :NEW.mat;
vavg := vtot / 3;
INSERT INTO tbl_trg2 ( hak, tot, avg ) VALUES ( :NEW.hak , vtot , vavg );
-- EXCEPTION
END;
INSERT INTO tbl_trg1 ( hak, name, kor, eng, mat ) VALUES ( 1, 'hong', 90,78, 99 );
2) 1 hong 90 7 8 99 μμ λκΈ° μ μ μλ κ° :OLD
1 hong 87 67 100 μλ‘ μμ λ κ° :NEW
UPDATE TBL_TRG1
SET kor = 87, eng = 67, mat = 100
WHERE hak = 1;
μλ tot/avg μμ λμ΄μΌν¨
CREATE OR REPLACE TRIGGER ut_trg1dml
AFTER
UPDATE OR INSERT ON tbl_trg1
FOR EACH ROW -- ν λ 벨 νΈλ¦¬κ±°
DECLARE
vtot NUMBER(3) ;
vavg NUMBER(5,2) ;
BEGIN
-- 1 hong 87 67 100
vtot := :NEW.kor + :NEW.eng + :NEW.mat;
vavg := vtot / 3;
IF INSERTING THEN
-- 1, 'hong', 90,78, 99
INSERT INTO tbl_trg2 ( hak, tot, avg ) VALUES ( :NEW.hak , vtot , vavg );
ELSIF UPDATING THEN
UPDATE tbl_trg2
SET tot = vtot , avg = vavg
WHERE hak = :OLD.hak; -- :NEW.hak
END IF;
-- EXCEPTION
END;
λ¬Έμ 3) tbl_trg1 ν
μ΄λΈμ 1λ² νμμ μ 보λ₯Ό μμ νλ©΄
μλ tbl_trg2 ν
μ΄λΈμ 1λ² μ 보(λ μ½λ) μμ ....νλ νΈλ¦¬κ±° μμ±, ν
μ€νΈνμΈμ..
DELETE FROM tbl_trg1
WHERE hak = 1;
-- ORA-02292: integrity constraint (SCOTT.FK_TEST2_HAK) violated - child record found
-- tbl_trg2 ν
μ΄λΈμμ μ°Έμ‘°
CREATE OR REPLACE TRIGGER ut_trg102
BEFORE
DELETE ON tbl_trg1
FOR EACH ROW -- ν λ 벨 νΈλ¦¬κ±°
BEGIN
DELETE FROM tbl_trg2
WHERE hak = :OLD.hak ; -- :NEW.hak X
-- EXCEPTION
END;
--DELETE FROM dept
--WHERE deptno = 10;
--ORA-02292: integrity constraint (SCOTT.FK_DEPTNO) violated - child record found
-- ν¨ν€μ§(package )--
1. κ΄κ³λλ νμ
, νλ‘κ·Έλ¨ κ°μ²΄, μλΈνλ‘κ·Έλ¨(procedure, function)μ λ
Όλ¦¬μ μΌλ‘ λ¬Άμ΄ λμ κ².
2. ν¨ν€μ§λ specification( λͺ
μΈ, μ¬μ )κ³Ό body( λͺΈμ²΄ ) λΆλΆμΌλ‘ ꡬμ±
--dbms_output . put_line()
--ν¨ν€μ§λͺ
.ν¨μλͺ
()
-- specificationλΆλΆ --
CREATE OR REPLACE PACKAGE employee_pkg
as
procedure print_ename(p_empno number);
procedure print_sal(p_empno number);
end employee_pkg;
-- Package EMPLOYEE_PKGμ΄(κ°) μ»΄νμΌλμμ΅λλ€.
-- body λΆλΆ
CREATE OR REPLACE PACKAGE BODY employee_pkg
as
procedure print_ename
(
p_empno number
)
is
l_ename emp.ename%type;
begin
select ename
into l_ename
from emp
where empno = p_empno;
dbms_output.put_line(l_ename);
exception
when NO_DATA_FOUND then
dbms_output.put_line('Invalid employee number');
end print_ename;
procedure print_sal(p_empno number) is
l_sal emp.sal%type;
begin
select sal
into l_sal
from emp
where empno = p_empno;
dbms_output.put_line(l_sal);
exception
when NO_DATA_FOUND then
dbms_output.put_line('Invalid employee number');
end print_sal;
end employee_pkg;
exec employee_pkg.print_ename(1234);
exec employee_pkg.print_ename(7369);
exec employee_pkg.print_sal(7369);
-- μ€μ ) id μ€λ³΅μ²΄ν¬, λ‘κ·ΈμΈ μ μ₯νλ‘μμ .....
1) specification λΆλΆ
CREATE OR REPLACE PACKAGE logon_pkg
AS
PROCEDURE up_idCheck
(
pempno IN emp.empno%TYPE -- ID
, pempnoCheck OUT NUMBER -- 0(μ¬μ©κ°λ₯) 1(μ¬μ©λΆκ°λ₯)
);
PROCEDURE up_logon
(
pempno IN emp.empno%TYPE -- ID
, pename IN emp.ename%TYPE -- PWD
, plogonCheck OUT NUMBER -- 0(λ‘κ·ΈμΈμ±κ³΅) 1(IDμ‘΄μ¬νμ§μμΌλ©΄) -1(ID o, PWD X)
);
FUNCTION uf_age
(
prrn VARCHAR2
)
RETURN NUMBER ;
END logon_pkg;
2) body λΆλΆ
CREATE OR REPLACE PACKAGE BODY logon_pkg
AS
PROCEDURE up_idCheck
(
pempno IN emp.empno%TYPE -- ID
, pempnoCheck OUT NUMBER -- 0(μ¬μ©κ°λ₯) 1(μ¬μ©λΆκ°λ₯)
)
IS
BEGIN
SELECT COUNT(*) INTO pempnoCheck
FROM emp
WHERE empno = pempno;
--EXCEPTION
END up_idCheck;
PROCEDURE up_logon
(
pempno IN emp.empno%TYPE -- ID
, pename IN emp.ename%TYPE -- PWD
, plogonCheck OUT NUMBER -- 0(λ‘κ·ΈμΈμ±κ³΅) 1(IDμ‘΄μ¬νμ§μμΌλ©΄) -1(ID o, PWD X)
)
IS
vename emp.ename%TYPE;
BEGIN
SELECT COUNT(*) INTO plogonCheck -- 1 , 0
FROM emp
WHERE empno = pempno;
IF plogonCheck = 1 THEN -- ID μ‘΄μ¬νλ€λ©΄
SELECT ename INTO vename
FROM emp
WHERE empno = pempno;
IF vename = pename THEN -- PWD μΌμΉνλ€λ©΄
plogonCheck := 0; -- λ‘κ·ΈμΈ μ±κ³΅
ELSE
plogonCheck := -1; -- λ‘κ·ΈμΈ μ€ν¨
END IF;
ELSE
plogonCheck := 1; -- ID μ‘΄μ¬νμ§ μλκ²½μ°
END IF;
--EXCEPTION
END up_logon;
FUNCTION uf_age
(
prrn VARCHAR2
)
RETURN NUMBER
IS
vischeck number(1);
vt_year number(4);
vb_year number(4);
vage number(3);
BEGIN
vischeck := SIGN( TRUNC( SYSDATE ) - TO_DATE( SUBSTR( prrn, 3,4), 'MMDD') );
vt_year := TO_CHAR( SYSDATE , 'YYYY');
vb_year := CASE
WHEN SUBSTR( prrn, 8, 1 ) IN (1,2,5,6) THEN '1900' + SUBSTR( prrn, 1,2)
WHEN SUBSTR( prrn, 8, 1 ) IN (3,4,7,8) THEN '2000' + SUBSTR( prrn, 1,2)
ELSE '1800' + SUBSTR( prrn, 1,2)
END;
vage := CASE VISCHECK
WHEN -1 THEN -- μμΌ μμ§λκ²
vt_year - vb_year-1
ELSE -- 0, 1
vt_year - vb_year
END ;
RETURN vage;
--EXCEPTION
END uf_age;
END logon_pkg;
SELECT num, name , ssn, logon_pkg.uf_age(ssn) age
FROM insa;
-- PL/SQL μμ± μ μΆ (νμ¬,μ€λ¬΄) : VIEW, SP, SF, T, P--
-- 컀μ( CURSOR ) + νλΌλ―Έν°λ₯Ό μ΄μ©νλ λ°©λ² --
EXEC UP_SelDeptEmp( 20 ); -- 6λͺ
+ 컀μ μ¬μ©(λͺ
,μ)
-- 1) λ°©λ²
CREATE OR REPLACE PROCEDURE UP_SelDeptEmp
(
pdeptno IN dept.deptno%type
)
IS
vdeptno emp.deptno%type;
vename emp.ename%type;
vsal emp.sal%type;
CURSOR c_emp IS (
SELECT deptno, ename, sal
FROM emp
WHERE deptno = pdeptno
);
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO vdeptno, vename, vsal;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( vdeptno || ', ' || vename || ', ' || vsal);
END LOOP;
CLOSE c_emp;
--EXCEPTION
END;
-- 2) λ°©λ²
CREATE OR REPLACE PROCEDURE UP_SelDeptEmp
(
pdeptno IN dept.deptno%type
)
IS
vdeptno emp.deptno%type;
vename emp.ename%type;
vsal emp.sal%type;
CURSOR c_emp( cpdeptno dept.deptno%type ) IS (
SELECT deptno, ename, sal
FROM emp
WHERE deptno = cpdeptno
);
BEGIN
OPEN c_emp ( pdeptno );
LOOP
FETCH c_emp INTO vdeptno, vename, vsal;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( vdeptno || ', ' || vename || ', ' || vsal);
END LOOP;
CLOSE c_emp;
--EXCEPTION
END;
-- 3) UP_μΆλ ₯νλ νλ‘μμ λ€μ μ§κΈκΉμ§ DBMS_OUPUT.PUT_LINE() μ¬λ¬ λ μ½λλ₯Ό νλ©΄μ μΆλ ₯
-- μλ° νλ‘κ·Έλ¨ μ°λ <- 컀μ(λ°μ΄ν° μ μ₯ κ°μ²΄) μ체λ₯Ό μΆλ ₯μ© λ§€κ°λ³μ λ°ν.
-- [ SYS_REFCURSOR, μ€λΌν΄9i μ΄ν~ ] μ΄κ±Έλ‘ κΈ°μ΅ ***
-- REF CURSORS ( μ€λΌν΄ 9i μ΄μ )
CREATE OR REPLACE PROCEDURE up_selInsa
(
pcursor IN SYS_REFCURSOR
)
IS
vname insa.name%type;
vcity insa.city%type;
vbasicpay insa.basicpay%type;
BEGIN
LOOP
FETCH pcursor INTO vname, vcity, vbasicpay;
EXIT WHEN pcursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( vname || ', ' || vcity || ', ' || vbasicpay);
END LOOP;
END;
γ΄ μ¬κΈ΄ OPENμ΄ μμ
[μ μ₯ νλ‘μμ ]
CREATE OR REPLACE PROCEDURE up_test_selInsa
IS
vcursor SYS_REFCURSOR; -- 컀μ λ³μ μ μΈ
BEGIN
OPEN vcursor FOR SELECT name, city, basicpay FROM insa;
UP_SELINSA( vcursor ); -- LOOP FETCH
CLOSE vcursor;
-- EXCEPTION
END;
EXEC up_test_selInsa;
-- + μ€λΌν΄ μ μ₯ νλ‘μμ μ€ν ν 컀μ μΆλ ₯μ© λ§€κ°λ³μλ‘ λ겨주면
-- μλ°μμ 컀μλ₯Ό λ°μμ μ¬μ©νλ μ½λ©. ( κΈ°μ΅ , μκΈ° )
CREATE OR REPLACE PROCEDURE up_ex05
(
pcursor OUT SYS_REFCURSOR
)
IS
BEGIN
OPEN pcursor FOR SELECT name, city, basicpay FROM insa;
-- LOOP~ FETCH X
-- CLOSE X
END;
-- PL/SQL λΈλ( EXCEPTION λΈλ ) - μμΈ μ²λ¦¬ λ°©λ²
1) 미리 μ μλ μλ¬ μ²λ¦¬λ°©λ² ( 7κ°μ§ )
SELECT ename, sal
FrOM emp
WHERE sal = 1250;
--
EXEC up_emplist( sal );
EXEC up_emplist( 10000 ); -- 0 ORA-01403: no data found
EXEC up_emplist( 5000 ); -- 1
EXEC up_emplist( 1250 ); -- 2 ORA-01422: exact fetch returns more than requested number of rows
--
CREATE OR REPLACE PROCEDURE up_emplist
(
psal emp.sal%type
)
IS
vename emp.ename%type;
BEGIN
SELECT ename INTO vename
FROM emp
WHERE sal = psal;
DBMS_OUTPUT.PUT_LINE( psal || ' ==> ' || vename );
EXCEPTION -- μμΈμ²λ¦¬ μ
WHEN no_data_found THEN
RAISE_APPLICATION_ERROR(-20002, '> QUERY DATA NOT FOUND.'); -- JAVA μ°λ + μμΈμ²λ¦¬.
WHEN TOO_MANY_ROWS THEN
RAISE_APPLICATION_ERROR(-20003, '> QUERY TOO MANY ROWS FOUND.');
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20004, '> QUERY OTHERS EXCEPTION FOUND.');
END;
-- Procedure UP_EMPLISTμ΄(κ°) μ»΄νμΌλμμ΅λλ€.
--2) 미리 μ μ λμ§ μλ μμΈ μ²λ¦¬ λ°©λ²
DELETE FROM dept WHERE deptno >= 50;
COMMIT;
SELECT * FROM dept;
--
INSERT INTO dept VALUES ( 40, 'QC', 'SEOUL');
ORA-00001: unique constraint (SCOTT.PK_DEPT) violated
-- 미리 μ μλμ§ μλ μ -02291
-- ORA-02291: integrity constraint (SCOTT.FK_DEPTNO) violated - parent key not found
INSERT INTO emp ( empno, ename, deptno ) VALUES ( 9999, 'kenik', 90 );
CREATE OR REPLACE PROCEDURE up_insEmp
(
pempno emp.empno%type
, pename emp.ename%type
, pdeptno emp.deptno%type
)
IS
v_invalid_deptno EXCEPTION;
PRAGMA EXCEPTION_INIT( v_invalid_deptno,-02291);
BEGIN
INSERT INTO emp ( empno, ename, deptno ) VALUES ( pempno, pename, pdeptno );
COMMIT;
EXCEPTION
WHEN v_invalid_deptno THEN
RAISE_APPLICATION_ERROR(-20999, '> QUERY DEPTNO FK NOT FOUND.');
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20004, '> QUERY OTHERS EXCEPTION FOUND.');
END;
EXEC UP_INSEMP( 9999, 'KENIK', 90 );
-- ORA-20999: > QUERY DEPTNO FK NOT FOUND.
3) μ¬μ©μκ° μ μν μλ¬ μ²λ¦¬λ°©λ²
μλ°μμ throw μ λΉμ·
--μλ° - class STUDNENT{ -- int kor; 0~100 κ΅μ΄μ μ λ²μλ²μ΄λ«λ€ μμΈ μ²λ¦¬ --} --class ScoreOutOFBoundException extends Exception{ --} -- throw new ScoreOutOFBoundException(); |
SELECT COUNT(*) INTO vempcount
FROM emp
WHERE sal BETWEEN (psal-100) AND (psal+100) ;
λ§μ½ 400~600 μ¬μ X COUNT(*) INTO vempcount == 0 κ°μ λ‘ μ¬μ©μ μ μν μμΈ λ°μμν€κ³
EXCEPTION
WHEN μμΈ THEN
μ²λ¦¬~
-- ORA-20011: > QUERY EMP COUNT = 0 .... <
EXEC UP_EXCEPTION02( 500 );
EXEC UP_EXCEPTION02( 1200 );
CREATE OR REPLACE PROCEDURE up_exception02
(
psal IN emp.sal%type
)
IS
vempcount NUMBER;
ve_no_emp_returned EXCEPTION; -- μμΈ λ³μ μ μΈ
BEGIN
SELECT COUNT(*) INTO vempcount
FROM emp
WHERE sal BETWEEN (psal-100) AND (psal+100) ;
IF vempcount = 0 THEN
-- -- κ°μ μμΈ λ°μ throw new MyScoreException();
RAISE ve_no_emp_returned; -- μμΈ κ°μ λ°μ
ELSE
DBMS_OUTPUT.PUT_LINE('>μ²λ¦¬ κ²°κ³Ό : ' || vempcount );
END IF;
EXCEPTION
WHEN ve_no_emp_returned THEN
RAISE_APPLICATION_ERROR(-20011, '> QUERY EMP COUNT = 0 .... <');
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20004, '> QUERY OTHERS EXCEPTION FOUND <');
END;
-- [ λμ SQL ] --
JAVA λμ λ°°μ΄
int [] m ; -- λ°°μ΄ν¬κΈ° X n = new int[n]; |
PL/SQL
1. λμ SQL ? [μ»΄νμΌ μ]μ SQLλ¬Έμ₯μ΄ νμ μ΄ λμ§ μμ κ²½μ° -> μ€νλ λ SQL λ¬Έμ νμ .
-> νλ‘μμ κ° μ»΄νμΌλ λ μΏΌλ¦¬κ° μμ±λλ κ²μ΄ μλλΌ μ€νλ λ μΏΌλ¦¬κ° μμ±λ¨.
-- κ²μ --
SELECT *
FROM board
WHERE μ λͺ© LIKE '%μ»΄ν¨ν°%' OR λ΄μ© LIKE '%μ»΄ν¨ν°%';;
SELECT *
FROM board
WHERE μμ±μ LIKE '%μ»΄ν¨ν°%';
SELECT *
FROM board
WHERE λ΄μ© LIKE '%μ»΄ν¨ν°%';
SELECT *
FROM board
WHERE μ λͺ© LIKE '%μ»΄ν¨ν°%';
-- λ€λμ κ²μ 쑰건 (μ¬λ¬ 쑰건μ 체ν¬ν΄μΌν λ κΌ μ¨μΌν¨!)
2. PL/SQL λμ 쿼리λ₯Ό μ¬μ©νλ 2κ°μ§ λ°©λ²
1) μμ λμ 쿼리 ( NDS : Native Dynamic SQL ) 2) dbms_sql ν¨ν€μ§ μ¬μ© X |
3. λμ 쿼리λ₯Ό μ€ννλ λ°©λ²
1) νμ
EXECUTE IMMEDIATE λμ 쿼리문
[ INTO λ³μλͺ
,λ³μλͺ
... ]
[ USING MODE(IN, OUT, IN OUT ) νλΌλ―Έν°..... ];
4. λμ 쿼리 μμ
1) μμ 1 - μ΅λͺ
νλ‘μμ
DECLARE
vsql VARCHAR2(1000); -- λμ 쿼리 μ μ₯ν λ³μ
vdeptno emp.deptno%type;
vempno emp.empno%type;
vename emp.ename%type;
vjob emp.job%type;
BEGIN
-- γ±) λμ 쿼리 μμ±
vsql := 'SELECT deptno, empno, ename, job ';
vsql := vsql || ' FROM emp ';
vsql := vsql || ' WHERE empno = 7369';
-- γ΄) λμ 쿼리 μ€ν.
EXECUTE IMMEDIATE vsql
INTO vdeptno, vempno, vename, vjob;
DBMS_OUTPUT.PUT_LINE( vdeptno || ', ' || vempno || ', ' || vename || ', ' || vjob ) ;
--EXCEPTION
END;
1) μμ 2 - μ μ₯ νλ‘μμ
CREATE OR REPLACE PROCEDURE UP_NDSEmp
(
pempno emp.empno%type
)
IS
vsql VARCHAR2(1000);
vdeptno emp.deptno%type;
vempno emp.empno%type;
vename emp.ename%type;
vjob emp.job%type;
BEGIN
vsql := 'SELECT deptno, empno, ename, job ';
vsql := vsql || ' FROM emp ';
vsql := vsql || ' WHErE empno = :pempno';
EXECUTE IMMEDIATE vsql
INTO vdeptno, vempno, vename, vjob
USING pempno;
DBMS_OUTPUT.PUT_LINE( vdeptno || ', ' || vempno || ', ' || vename || ', ' || vjob ) ;
--EXCEPTION
END;
EXEC UP_NDSEmp( 7369 );
3) μμ 3 - μ μ₯ νλ‘μμ
dept ν
μ΄λΈμ μλ‘μ΄ λΆμ μ 보λ₯Ό μ μ₯νλ νλ‘μμ μμ± + λμ 쿼리 μ¬μ©.
deptno 10/20/30/40
seq_dept μνμ€ μμκ°50 10μ¦κ° X , μνμ€ μ¬μ© μνκ³
CREATE OR REPLACE PROCEDURE UP_INSDept
(
pdname dept.dname%type
, ploc dept.loc%type
)
IS
vsql VARCHAR2(1000);
vdeptno dept.deptno%type;
BEGIN
SELECT MAX(deptno) + 10 INTO vdeptno
FROM dept;
vsql := 'INSERT INTO dept ';
vsql := vsql || ' ( deptno, dname, loc ) ';
vsql := vsql || ' VALUES ( :vdeptno, :pdname, :ploc ) ';
EXECUTE IMMEDIATE vsql
-- INTO
USING vdeptno, pdname, ploc;
-- COMMIT;
-- EXCEPTION
END;
EXEC UP_INSDEPT( 'QC', 'SEOUL');
--μ) μ¬μ©μ μνλ ννμ κ²μν μ¬λ¬κ° μμ±.. ( ν
μ΄λΈ μμ± DDL λ¬Έ ) λμ 쿼리
DECLARE
vsql VARCHAR2(1000);
vtablename VARCHAR2(20);
BEGIN
vtablename := 'tbl_nds';
vsql := 'CREATE TABLE ' || vtablename;
--vsql := 'CREATE TABLE :vtablename '; USING μ¬μ©ν κ²½μ°!
vsql := vsql || ' ( ';
vsql := vsql || ' id number primary key ';
vsql := vsql || ' , name varchar2(20) ';
vsql := vsql || ' ) ';
DBMS_OUTPUT.PUT_LINE( vsql );
EXECUTE IMMEDIATE vsql;
--USING vtablename;
END;
-- λ§μ§λ§ λμ 쿼리 μμ ( μκΈ° ) --
μ ) OPEN FOR λ¬Έ ?
λμ SQLμ μ€ν κ²°κ³Όκ° μ¬λ¬ κ°μ νμ λ°ννλ SELECTλ¬Έ + 컀μ
CREATE OR REPLACE PROCEDURE up_nds02
(
pdeptno emp.deptno%type
)
IS
vsql varchar2(1000);
vrow emp%ROWTYPE;
vcur SYS_REFCURSOR; -- 9i REF CURSOR
BEGIN
vsql := 'SELECT * ';
vsql := vsql || ' FROM emp ';
vsql := vsql || ' WHERE deptno = :pdeptno ';
-- EXECUTE IMMEDIATE λμ 쿼리문 X
-- INTO
-- USING pdeptno;
OPEN vcur FOR vsql USING pdeptno;
LOOP
FETCH vcur INTO vrow;
EXIT WHEN vcur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( vrow.empno || ', ' || vrow.ename );
END LOOP;
CLOSE vcur;
--EXCEPTION
END;
EXEC UP_NDS02( 30 );
EXEC UP_NDS02( 10 );
νΈλ¦¬κ±° μ€μ΅
-- μν ν
μ΄λΈ μμ±
CREATE TABLE μν (
μνμ½λ VARCHAR2(6) NOT NULL PRIMARY KEY
,μνλͺ
VARCHAR2(30) NOT NULL
,μ μ‘°μ¬ VARCHAR2(30) NOT NULL
,μλΉμκ°κ²© NUMBER
,μ¬κ³ μλ NUMBER DEFAULT 0
);
-- μ κ³ ν μ΄λΈ μμ±
CREATE TABLE μ
κ³ (
μ
κ³ λ²νΈ NUMBER PRIMARY KEY
,μνμ½λ VARCHAR2(6) NOT NULL CONSTRAINT FK_ibgo_no
REFERENCES μν(μνμ½λ)
,μ
κ³ μΌμ DATE
,μ
κ³ μλ NUMBER
,μ
κ³ λ¨κ° NUMBER
);
-- ν맀 ν
μ΄λΈ μμ±
CREATE TABLE ν맀 (
ν맀λ²νΈ NUMBER PRIMARY KEY
,μνμ½λ VARCHAR2(6) NOT NULL CONSTRAINT FK_pan_no
REFERENCES μν(μνμ½λ)
,ν맀μΌμ DATE
,ν맀μλ NUMBER
,ν맀λ¨κ° NUMBER
);
-- μν ν μ΄λΈμ μλ£ μΆκ°
INSERT INTO μν(μνμ½λ, μνλͺ
, μ μ‘°μ¬, μλΉμκ°κ²©) VALUES
('AAAAAA', 'λμΉ΄', 'μΌμ±', 100000);
INSERT INTO μν(μνμ½λ, μνλͺ
, μ μ‘°μ¬, μλΉμκ°κ²©) VALUES
('BBBBBB', 'μ»΄ν¨ν°', 'μλ', 1500000);
INSERT INTO μν(μνμ½λ, μνλͺ
, μ μ‘°μ¬, μλΉμκ°κ²©) VALUES
('CCCCCC', 'λͺ¨λν°', 'μΌμ±', 600000);
INSERT INTO μν(μνμ½λ, μνλͺ
, μ μ‘°μ¬, μλΉμκ°κ²©) VALUES
('DDDDDD', 'νΈλν°', 'λ€μ°', 500000);
INSERT INTO μν(μνμ½λ, μνλͺ
, μ μ‘°μ¬, μλΉμκ°κ²©) VALUES
('EEEEEE', 'νλ¦°ν°', 'μΌμ±', 200000);
COMMIT;
------------------------------------------------------------
-- 3. νΈλ¦¬κ±° μμ± (λ¬Έμ )
-- 1) μ
κ³ ν
μ΄λΈμ INSERT νΈλ¦¬κ±°λ₯Ό μμ± νλ€.
-- [μ
κ³ ] ν
μ΄λΈμ μλ£κ° μΆκ° λλ κ²½μ° [μν] ν
μ΄λΈμ [μ¬κ³ μλ]μ΄ λ³κ²½ λλλ‘ νΈλ¦¬κ±°λ₯Ό μμ±νλ€.
-- μ
κ³ ν
μ΄λΈμ λ°μ΄ν° μ
λ ₯
INSERT INTO μ
κ³ (μ
κ³ λ²νΈ, μνμ½λ, μ
κ³ μΌμ, μ
κ³ μλ, μ
κ³ λ¨κ°)
VALUES (1, 'AAAAAA', '2004-10-10', 5, 50000);
INSERT INTO μ
κ³ (μ
κ³ λ²νΈ, μνμ½λ, μ
κ³ μΌμ, μ
κ³ μλ, μ
κ³ λ¨κ°)
VALUES (2, 'BBBBBB', '2004-10-10', 15, 700000);
INSERT INTO μ
κ³ (μ
κ³ λ²νΈ, μνμ½λ, μ
κ³ μΌμ, μ
κ³ μλ, μ
κ³ λ¨κ°)
VALUES (3, 'AAAAAA', '2004-10-11', 15, 52000);
INSERT INTO μ
κ³ (μ
κ³ λ²νΈ, μνμ½λ, μ
κ³ μΌμ, μ
κ³ μλ, μ
κ³ λ¨κ°)
VALUES (4, 'CCCCCC', '2004-10-14', 15, 250000);
INSERT INTO μ
κ³ (μ
κ³ λ²νΈ, μνμ½λ, μ
κ³ μΌμ, μ
κ³ μλ, μ
κ³ λ¨κ°)
VALUES (5, 'BBBBBB', '2004-10-16', 25, 700000);
COMMIT;
CREATE OR REPLACE TRIGGER ins
AFTER
INSERT ON μ
κ³
FOR EACH ROW
BEGIN
UPDATE μν
SET μ¬κ³ μλ = μ¬κ³ μλ + :NEW.μ
κ³ μλ
WHERE μνμ½λ = :NEW.μνμ½λ;
END;
-- 2) μ
κ³ ν
μ΄λΈμ UPDATE νΈλ¦¬κ±°λ₯Ό μμ± νλ€. (λ¬Έμ )
-- [μ
κ³ ] ν
μ΄λΈμ μλ£κ° λ³κ²½ λλ κ²½μ° [μν] ν
μ΄λΈμ [μ¬κ³ μλ]μ΄ λ³κ²½ λλλ‘ νΈλ¦¬κ±°λ₯Ό μμ±νλ€.
CREATE OR REPLACE TRIGGER ups
AFTER
UPDATE ON μ
κ³
FOR EACH ROW
BEGIN
UPDATE μν
SET μ¬κ³ μλ = μ¬κ³ μλ - :OLD.μ
κ³ μλ + :NEW.μ
κ³ μλ
WHERE μνμ½λ = :NEW.μνμ½λ;
END;
-- UPDATE ν
μ€νΈ
UPDATE μ
κ³ SET μ
κ³ μλ = 30 WHERE μ
κ³ λ²νΈ = 5;
COMMIT;
-- 3) μ
κ³ ν
μ΄λΈμ DELETE νΈλ¦¬κ±°λ₯Ό μμ± νλ€.
-- [μ
κ³ ] ν
μ΄λΈμ μλ£κ° μμ λλ κ²½μ° [μν] ν
μ΄λΈμ [μ¬κ³ μλ]μ΄ λ³κ²½ λλλ‘ νΈλ¦¬κ±°λ₯Ό μμ±νλ€.
CREATE OR REPLACE TRIGGER des
AFTER
DELETE ON μ
κ³
FOR EACH ROW
BEGIN
UPDATE μν
SET μ¬κ³ μλ = μ¬κ³ μλ - :OLD.μ
κ³ μλ
WHERE μνμ½λ = :OLD.μνμ½λ;
END;
-- DELETE ν
μ€νΈ
DELETE FROM μ
κ³ WHERE μ
κ³ λ²νΈ = 5;
COMMIT;
-- μ
κ³ ν
μ΄λΈμ μ¬κ³ μλ μμ λ° μμ λ μν ν
μ΄λΈμ μ¬κ³ μλμ΄ μ κ±°λ μμΌλ©΄ ν μ μμΌλ―λ‘
-- UPDATE λ° DELETE νΈλ¦¬κ±°λ₯Ό BEFORE νΈλ¦¬κ±°λ‘ μμ νμ¬ μν ν
μ΄λΈμ μ¬κ³ μλμ λ°λΌ μμ λλ μμ λ₯Ό ν μ μλλ‘ μμ νλ€.
-- 4) ν맀 ν
μ΄λΈμ INSERT νΈλ¦¬κ±°λ₯Ό μμ±νλ€.(BEFORE νΈλ¦¬κ±°λ‘ μμ±)
-- [ν맀] ν
μ΄λΈμ μλ£κ° μΆκ° λλ κ²½μ° [μν] ν
μ΄λΈμ [μ¬κ³ μλ]μ΄ λ³κ²½ λλλ‘ νΈλ¦¬κ±°λ₯Ό μμ±νλ€.
CREATE OR REPLACE TRIGGER in_ν맀
BEFORE
INSERT ON ν맀
FOR EACH ROW
DECLARE
vμ¬κ³ μλ NUMBER;
BEGIN
SELECT μ¬κ³ μλ INTO vμ¬κ³ μλ
FROM μν
WHERE μνμ½λ = :NEW.μνμ½λ;
IF :NEW.ν맀μλ > vμ¬κ³ μλ
THEN RAISE_APPLICATION_ERROR(-20000, 'μ¬κ³ λΆμ‘±! ν맀 λΆκ°');
ELSE
UPDATE μν
SET μ¬κ³ μλ = μ¬κ³ μλ - :NEW.ν맀μλ
WHERE μνμ½λ = :NEW.μνμ½λ;
END IF;
END;
-- ν맀 ν
μ΄λΈμ λ°μ΄ν° μ
λ ₯
INSERT INTO ν맀 (ν맀λ²νΈ, μνμ½λ, ν맀μΌμ, ν맀μλ, ν맀λ¨κ°) VALUES
(1, 'AAAAAA', '2004-11-10', 5, 1000000);
COMMIT;
INSERT INTO ν맀 (ν맀λ²νΈ, μνμ½λ, ν맀μΌμ, ν맀μλ, ν맀λ¨κ°) VALUES
(1, 'AAAAAA', '2004-11-10', 50, 1000000);
COMMIT;
-- 5) ν맀 ν
μ΄λΈμ UPDATE νΈλ¦¬κ±°λ₯Ό μμ±νλ€.(BEFORE νΈλ¦¬κ±°λ‘ μμ±)
-- [ν맀] ν
μ΄λΈμ μλ£κ° λ³κ²½ λλ κ²½μ° [μν] ν
μ΄λΈμ [μ¬κ³ μλ]μ΄ λ³κ²½ λλλ‘ νΈλ¦¬κ±°λ₯Ό μμ±νλ€.
CREATE OR REPLACE TRIGGER up_ν맀
BEFORE
UPDATE ON ν맀
FOR EACH ROW
DECLARE
vμ¬κ³ μλ NUMBER;
BEGIN
SELECT μ¬κ³ μλ INTO vμ¬κ³ μλ
FROM μν
WHERE μνμ½λ = :NEW.μνμ½λ;
IF :NEW.ν맀μλ > vμ¬κ³ μλ + :OLD.ν맀μλ
THEN RAISE_APPLICATION_ERROR(-20001, 'ν맀μμ λΆκ°');
ELSE
UPDATE μν
SET μ¬κ³ μλ = μ¬κ³ μλ + :OLD.ν맀μλ - :NEW.ν맀μλ
WHERE μνμ½λ = :NEW.μνμ½λ;
END IF;
END;
-- UPDATE ν
μ€νΈ
UPDATE ν맀 SET ν맀μλ = 200 WHERE ν맀λ²νΈ = 1;
UPDATE ν맀 SET ν맀μλ = 10 WHERE ν맀λ²νΈ = 1;
COMMIT;
-- 6) ν맀 ν
μ΄λΈμ DELETE νΈλ¦¬κ±°λ₯Ό μμ± νλ€.
-- [ν맀] ν
μ΄λΈμ μλ£κ° μμ λλ κ²½μ° [μν] ν
μ΄λΈμ [μ¬κ³ μλ]μ΄ λ³κ²½ λλλ‘ νΈλ¦¬κ±°λ₯Ό μμ±νλ€.
CREATE OR REPLACE TRIGGER de_ν맀
AFTER
DELETE ON ν맀
FOR EACH ROW
BEGIN
UPDATE μν
SET μ¬κ³ μλ = μ¬κ³ μλ + :OLD.ν맀μλ
WHERE μνμ½λ = :OLD.μνμ½λ;
END;
-- DELETE ν
μ€νΈ
DELETE ν맀 WHERE ν맀λ²νΈ = 1;
COMMIT;
μ΄κΈ°ν
------------------------------------------------------------
-- μν ν
μ΄λΈ μμ±
DROP TABLE ν맀;
DROP TABLE μ
κ³ ;
DROP TABLE μν;
CREATE TABLE μν (
μνμ½λ VARCHAR2(6) NOT NULL PRIMARY KEY
,μνλͺ
VARCHAR2(30) NOT NULL
,μ μ‘°μ¬ VARCHAR2(30) NOT NULL
,μλΉμκ°κ²© NUMBER
,μ¬κ³ μλ NUMBER DEFAULT 0
);
-- μ
κ³ ν
μ΄λΈ μμ±
CREATE TABLE μ
κ³ (
μ
κ³ λ²νΈ NUMBER PRIMARY KEY
,μνμ½λ VARCHAR2(6) NOT NULL CONSTRAINT FK_ibgo_no
REFERENCES μν(μνμ½λ)
,μ
κ³ μΌμ DATE
,μ
κ³ μλ NUMBER
,μ
κ³ λ¨κ° NUMBER
);
-- ν맀 ν
μ΄λΈ μμ±
CREATE TABLE ν맀 (
ν맀λ²νΈ NUMBER PRIMARY KEY
,μνμ½λ VARCHAR2(6) NOT NULL CONSTRAINT FK_pan_no
REFERENCES μν(μνμ½λ)
,ν맀μΌμ DATE
,ν맀μλ NUMBER
,ν맀λ¨κ° NUMBER
);
-- μν ν
μ΄λΈμ μλ£ μΆκ°
INSERT INTO μν(μνμ½λ, μνλͺ
, μ μ‘°μ¬, μλΉμκ°κ²©) VALUES
('AAAAAA', 'λμΉ΄', 'μΌμ±', 100000);
INSERT INTO μν(μνμ½λ, μνλͺ
, μ μ‘°μ¬, μλΉμκ°κ²©) VALUES
('BBBBBB', 'μ»΄ν¨ν°', 'μλ', 1500000);
INSERT INTO μν(μνμ½λ, μνλͺ
, μ μ‘°μ¬, μλΉμκ°κ²©) VALUES
('CCCCCC', 'λͺ¨λν°', 'μΌμ±', 600000);
INSERT INTO μν(μνμ½λ, μνλͺ
, μ μ‘°μ¬, μλΉμκ°κ²©) VALUES
('DDDDDD', 'νΈλν°', 'λ€μ°', 500000);
INSERT INTO μν(μνμ½λ, μνλͺ
, μ μ‘°μ¬, μλΉμκ°κ²©) VALUES
('EEEEEE', 'νλ¦°ν°', 'μΌμ±', 200000);
COMMIT;
SELECT * FROM μν;
μ΅κ·ΌλκΈ