|
Ãâó:
target=_blank>http://www.gurubee.net/lecture/1145
1. Æ®¸®°Å¶õ?
INSERT, UPDATE, DELETE¹®ÀÌ TABLE¿¡ ´ëÇØ ÇàÇØÁú ¶§ ¹¬½ÃÀûÀ¸·Î ¼öÇàµÇ´Â PROCEDURE ÀÌ´Ù.
Æ®¸®°Å´Â TABLE°ú´Â º°µµ·Î DATABASE¿¡ ÀúÀå µÈ´Ù.
Æ®¸®°Å´Â VIEW¿¡ ´ëÇؼ°¡ ¾Æ´Ï¶ó TABLE¿¡ °üÇؼ¸¸ Á¤ÀÇ µÉ ¼ö ÀÖ´Ù.
Çà Æ®¸®°Å : Ä÷³ÀÇ °¢°¢ÀÇ ÇàÀÇ µ¥ÀÌÅÍ Çà º¯È°¡ »ý±æ¶§¸¶´Ù ½ÇÇàµÇ¸ç, ±× µ¥ÀÌÅÍ ÇàÀÇ ½ÇÁ¦°ªÀ» Á¦¾îÇÒ ¼ö ÀÖ´Ù.
¹®Àå Æ®¸®°Å : Æ®¸®°Å »ç°Ç¿¡ ÀÇÇØ ´Ü Çѹø ½ÇÇàµÇ¸ç, Ä÷³ÀÇ °¢ µ¥ÀÌÅÍ ÇàÀ» Á¦¾î ÇÒ ¼ö ¾ø´Ù.
2. Æ®¸®°Å ¹®¹ý
- BEFORE : INSERT, UPDATE, DELETE¹®ÀÌ ½ÇÇàµÇ±â Àü¿¡ Æ®¸®°Å°¡ ½ÇÇà µÈ´Ù.
- AFTER : INSERT, UPDATE, DELETE¹®ÀÌ ½ÇÇàµÈ ÈÄ Æ®¸®°Å°¡ ½ÇÇà µÈ´Ù.
- trigger_event : INSERT, UPDATE, DELETE Áß¿¡¼ ÇÑ °³ ÀÌ»ó ¿Ã ¼ö ÀÖ´Ù.
- FOR EACH ROW : ÀÌ ¿É¼ÇÀÌ ÀÖÀ¸¸é Çà Æ®¸®°Å°¡ µÈ´Ù.
----------------------------------------------
3. Æ®¸®°Å ¿¹Á¦ 1
SQL> CREATE OR REPLACE TRIGGER sum_trigger
BEFORE
INSERT OR UPDATE ON emp
FOR EACH ROW
DECLARE
-- º¯¼ö¸¦ ¼±¾ðÇÒ ¶§´Â DECLARE¹®À» »ç¿ëÇØ¾ß ÇÑ´Ù
avg_sal NUMBER;
BEGIN
SELECT ROUND(AVG(sal),3)
INTO avg_sal
FROM emp;
DBMS_OUTPUT.PUT_LINE('±Þ¿© Æò±Õ : ' || avg_sal);
END;
/
-- DBMS_OUTPUT.PUT_LINEÀ» Ãâ·Â
SQL> SET SERVEROUTPUT ON ;
-- INSERT¹®À» ½ÇÇàÇغ¸ÀÚ.
SQL> INSERT INTO EMP(EMPNO, ENAME, JOB, HIREDATE, SAL)
VALUES(1000, 'LION', 'SALES', SYSDATE, 5000);
-- INSERT¹®À» ½ÇÇàµÇ±â Àü±îÁöÀÇ ±Þ¿© Æò±ÕÀÌ Ãâ·ÂµÈ´Ù.
±Þ¿© Æò±Õ : 2073.214
1 °³ÀÇ ÇàÀÌ ¸¸µé¾îÁ³½À´Ï´Ù.
4. Æ®¸®°Å ¿¹Á¦ 2
SQL> CREATE OR REPLACE TRIGGER triger_test
BEFORE
UPDATE ON dept
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('º¯°æ Àü Ä÷³ °ª : ' || : old.dname);
DBMS_OUTPUT.PUT_LINE('º¯°æ ÈÄ Ä÷³ °ª : ' || : new.dname);
END;
/
-- DBMS_OUTPUT.PUT_LINEÀ» Ãâ·Â
SQL> SET SERVEROUTPUT ON ;
-- UPDATE¹®À» ½ÇÇà½ÃÅ°¸é..
SQL> UPDATE dept SET dname = 'Ãѹ«ºÎ' WHERE deptno = 30
-- Æ®¸®°Å°¡ ÀÚµ¿ ½ÇÇàµÇ¾î °á°ú°¡ Ãâ·ÂµÈ´Ù.
º¯°æ Àü Ä÷³ °ª : Àλç°ú
º¯°æ ÈÄ Ä÷³ °ª : Ãѹ«ºÎ
1 ÇàÀÌ °»½ÅµÇ¾ú½À´Ï´Ù.
|