CREATE TRIGGER name { BEFORE | AFTER } { event [ OR ...]}
ON table FOR EACH { ROW | STATEMENT }
EXECUTE PROCEDURE func_name ( )
event : INPUT , UPDATE , DELETE µîÀÌ ¿Ã ¼ö ÀÖ´Ù.
func_name : »ç¿ëÀÚ Á¤ÀÇ ÇÔ¼öÀÌ´Ù. plpgsql À» ±ÇÀåÇÏ°í ½Í´Ù.
¶ÇÇÑ ÀÌ ÇÔ¼ö¿¡´Â ÀÎÀÚ°¡ µé¾î¿Ã ¼ö ¾ø´Ù.
¶ÇÇÑ ÀÌ ÇÔ¼öÀÇ RETURN TYPE ·Î´Â OPAQUE À̾î¾ß ÇÑ´Ù.
Àμö¸¦ À§ÇØ TRIGGER ÀÇ ÀÚü º¯¼ö¸¦ »ç¿ëÇÏ¿©¾ß ÇÑ´Ù.
(new,old,...)
TRIGGER ¸¦ Ã̽º·´°Ô Ç¥ÇöÇÏÀÚ¸é ¹æ¾Æ¼è¸¦ ´ç±â¸é ÃѾËÀÌ ³ª°¡´Â °Í¿¡ ºñÀ¯ÇÒ ¼ö ÀÖ´Ù.
TRIGGER Àº TUPLE(ROW or RECORD) ¿¡ ´ëÇÑ INSERT, UPDATE, DELETE ¸¦ »ç°Ç(event) ·Î
º¸°í ÀÌ »ç°Ç¿¡ Á¤ÀÇµÈ ¾î¶² ÇൿÀ¸·Î ¹ÝÀÀÇÏ´Â °ÍÀÌ´Ù.¡
CREATE TRIGGER trg_test
BEFORE DELETE OR UPDATE ON supplier FOR EACH ROW
EXECUTE PROCEDURE check_sup();
supplier Å×ÀÌºí¿¡ DELETE, UPDATE °¡ ¹ß»ýÇϸé ÀÌ µ¿ÀÛÀÌ ÇàÇØÁö±â Àü¿¡
check_sup() ¸¦ ½ÇÇàÇ϶ó. ÀÌ TRIGGER ÀÇ À̸§Àº trg_test ÀÌ´Ù.
CREATE FUNCTION func_name() RETURNS type
AS '
[DECLARE declarations]
BEGIN
statements
END;
'LANGUAGE 'plpgsql';
CREATE FUNCTION pgsql_test() RETURNS datetime
AS '
DECLARE curtime datetime;
BEGIN
curtime:= ''now'';
return curtime;
END;
' LANGUAGE 'plpgsql';
´ÙÀ½Àº ½ÇÇà ¿¹Á¦ÀÌ´Ù. ¿ì¼± ´ÙÀ½ ¿¹Á¦¸¦ ÀûÀýÈ÷ º¹»ç¸¦ Çؼ ÆÄÀÏ·Î ¸¸µç´Ù.(¿¹ : trg.sql) ´ÙÀ½ ¿¹Á¦¸¦ ½ÇÇà¿¡ ¾Õ¼ ¸ÕÀú ÇؾßÇÒ ÀÏÀÌ Àִµ¥ ±×°ÍÀº Procedural Languages ¸¦ µî·ÏÇÏ´Â °ÍÀÌ´Ù. ´ÙÀ½¿¹Á¦¿¡´Â PL/pgSQL À» ÀÌ¿ëÇÑ ÇÔ¼ö¸¦ »ç¿ëÇϹǷΠÀÌ°ÍÀ» µî·ÏÇÏ´Â °ÍÀº ÇʼöÀÌ´Ù.
¹æ¹ýÀº µÎ°¡Áö°¡ ÀÖ´Ù.
1. template1 µ¥ÀÌŸ º£À̽º¿¡ µî·ÏÇÏ´Â °ÍÀÌ´Ù. ÀÌ µ¥ÀÌŸº£À̽º¿¡ µî·ÏÀÌ µÈÈÄ template1 µ¥ÀÌŸº£À̽º¿¡¼ create database ¸í·ÉÀ¸·Î µ¥ÀÌŸº£À̽º¸¦ »ý¼ºÇϸé ÀÚµ¿ÀûÀ¸·Î »ý¼ºµÈ µ¥ÀÌŸº£À̽º¿¡ PL/pgSQLÀÌ µî·ÏÀÌ µÇ¹Ç·Î Æí¸®ÇÏ´Ù.
µî·Ï¿¡ ¾Õ¼ ´ÙÀ½À» È®ÀÎÇ϶ó.
postgreSQLÀÇ PATH : ¿©±â¼ÀÇ PATH ´Â /usr/local/pgsql ÀÌ´Ù. ¶ÇÇÑ pgsql µð·ºÅ丮 ¹ØÀÇ lib µð·ºÅ丮¿¡¼ plpgsql.so ¸¦ È®ÀÎÇ϶ó. ¾Æ¸¶µµ ÀÌ ÈÀÏÀº ´Ù Á¸ÀçÇÒ °ÍÀÌ´Ù.
µî·Ï °úÁ¤
[postgres@nogadax postgres]# psql template1
template1=>
template1=> CREATE FUNCTION plpgsql_call_handler() RETURNS OPAQUE AS
template1-> '/usr/local/pgsql/lib/plpgsql.so' LANGUAGE 'c';
template1=>
template1=> CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'
template1-> HANDLER plpgsql_call_handler
template1-> LANCOMPILER 'PL/pgSQL';
template1=> CREATE DATABASE nogadax;
template1=> \q
[postgres@nogadax postgres]#
2. ´ÙÀ½ ¹æ¹ýÀº »ý¼ºÇÑ µ¥ÀÌŸº£À̽º¸¶´Ù ÇϳªÇϳª ´Ù µî·ÏÀ» ÇÏ´Â °ÍÀÌ´Ù.
µî·Ï °úÁ¤
[postgres@nogadax postgres]# psql nogadax
nogadax=>
nogadax=> CREATE FUNCTION plpgsql_call_handler() RETURNS OPAQUE AS
nogadax-> '/usr/local/pgsql/lib/plpgsql.so' LANGUAGE 'c';
nogadax=>
nogadax=> CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'
nogadax-> HANDLER plpgsql_call_handler
nogadax-> LANCOMPILER 'PL/pgSQL';
nogadax=>
nogadax=> \q
[postgres@nogadax postgres]#
ÀÌÁ¦´Â À§ÀÇ µÎ°¡Áö ¹æ¹ýÁß Çϳª¸¦ ¼±ÅÃÇÏ¿© µî·ÏÀ» ÇÏ¸é µÈ´Ù.
´ÙÀ½Àº ¾Æ·¡ ¿¹Á¦¸¦ ½ÇÇàÇÏ´Â ¹æ¹ýÀÌ´Ù.
[podtgres@nogadax postgres]$ psql nogadax
nogadax=> \i /home/postgres/trg.sql
.....
.....
-------------------------------------------------------Cut here!!
--coded BY NoGaDa-X 2000/02/19
--DROP all Object for safe_test
DROP FUNCTION ins_row();
DROP TRIGGER trg_test ON test1;
DROP TABLE test1;
DROP TABLE test2;
--Create Table
CREATE TABLE test1(
tab1_id int4,
tab1_name text
);
CREATE TABLE test2(
tab2_id int4,
tab2_memo text DEFAULT 'None'
);
--Create Function
CREATE FUNCTION ins_row() RETURNS OPAQUE
AS '
BEGIN
INSERT INTO test2(tab2_id) VALUES(new.tab1_id);
RETURN new;
END;
' LANGUAGE 'plpgsql';
--Create Trigger
CREATE TRIGGER trg_test
AFTER INSERT ON test1 FOR EACH ROW
EXECUTE PROCEDURE ins_row();
--INSERT Transaction
BEGIN;
INSERT INTO test1 values(1,'nogadax');
INSERT INTO test1 values(2,'winob');
INSERT INTO test1 values(3,'diver708');
INSERT INTO test1 values(4,'jini');
INSERT INTO test1 values(5,'opensys');
INSERT INTO test1 values(6,'Linuz');
END;
--SELECT TRACTION
BEGIN;
SELECT * FROM test1;
SELECT * FROM test2;
END;
----------------------------------------------------------End !!
CREATE TRIGGER trigger_name { BEFORE | AFTER } { event [OR,...] }
ON table FOR EACH { ROW | STATEMENT }
EXECUTE PROCEDURE funcname ();
trigger_name : TRIGGER ÀÇ À̸§
table : Table ÀÇ À̸§
event : INSERT , DELETE , UPDATE µé Áß¿¡¼ Çϳª ȤÀº µÎ¼¼°³¸¦
TRIGGER ¸¦ ±âµ¿½ÃÅ°±â À§ÇÑ event ·Î º½
( ¿¹ : INSERT OR UPDATE )
func_name : user °¡ Á¦°øÇÑ ÇÔ¼ö. ÀÌ ÇÔ¼ö´Â Æ®¸®°Å°¡ »ý¼ºµÇ±â Àü¿¡ ¸¸µé¾îÁ®¾ß ÇÑ´Ù.
¶ÇÇÑ, ÀÌ ÇÔ¼öÀÇ return ÇüÀº opaqueÀ̸ç Àμö°¡ ¾ø¾î¾ß ÇÑ´Ù.
(ÀÌ ºÎºÐÀº PostgreSQL Programmer's Guide ¿¡ ³ª¿Í ÀÖ´Â ºÎºÐÀε¥ function
¿¡¼ ¿Ö Àμö°¡ ¾²ÀÌ¸é ¾ÈµÇ´ÂÁö ±×¸®°í opaque ÇüÀÇ ¸®ÅÏ °ª¸¸ µÇ´ÂÁö ¸¦
Á¤È®È÷ ¼³¸íÇÑ ºÎºÐÀÌ ¾ø´Ù.)
"CREATE TRIGGER"·Î ÀÎÇØ TRIGGER °¡ ¼º°øÀûÀ¸·Î »ý¼ºµÇ¸é CREATE ¶ó´Â
¸Þ½ÃÁö°¡ Ãâ·ÂµÈ´Ù.
CREATE TRIGGERÀº ÇöÀçÀÇ µ¥ÀÌŸº£À̽º¿¡ »õ·Î¿î TRIGGERÀ» µî·ÏÇÒ °ÍÀÌ´Ù.Trigger
Àº Å×À̺í(¸±·¹À̼Ç)°ú ¿¬°èµÇ¾î¼ ¹Ì¸® ±ÔÁ¤µÈ ÇÔ¼ö func_nameÀ» ½ÇÇàÇÑ´Ù.
Æ®¸®°Å´Â Æ®¸®°ÅÀÇ »ý¼º½Ã BEFOREÅ°¸¦ »ç¿ëÇÏ¿© Tuple(row,record)¿¡ ¾î¶² event°¡
¹ß»ýÇϱâ Àü¿¡ ±âµ¿µÇ¾î Áú¼ö ÀÖµµ·Ï ±ÔÁ¤µÇ¾îÁú¼ö ÀÖÀ¸¸ç ¹Ý´ë·Î AFTERÅ°¸¦ »ç¿ëÇÏ
¿© event°¡ ¿Ï·á ÈÄ¿¡ ±âµ¿µÇ°Ô ÇÒ¼öµµ ÀÖ´Ù.
(´ÙÀ½Àº ºÎºÐÀûÀ¸·Î Postgres data changes visibility rule ÀÌ ÂüÁ¶µÇ¾ú´Ù.)
Æ®¸®°Å°¡ BEFORE¿¡ ÀÇÇØ eventÀü¿¡ ±âµ¿µÇ¾î Áø´Ù¸é, Æ®¸®°Å´Â ÇöÀçÀÇ Tuple¿¡ ´ëÇÑ
event¸¦ °Ç³Ê¶Ù°Ô ÇÑ´Ù. ƯÈ÷ INSERT³ª UPDATEÀÇ event¿¡ ´ëÇؼ´Â »ðÀԵǾîÁú Æ©ÇÃ
ÀÇ º¯È¸¦ ÀÎÁöÇÒ ¼ö ¾ø´Ù. Áï, BEFORE¼º°ÝÀÇ Æ®¸®°Å´Â º¯°æµÇ¾îÁú Æ©Çõ鿡 ´ëÇؼ
"invisible"ÇÑ »óÅÂÀÌ´Ù. ´ÜÁö, 󸮵ǾîÁú event ¸¸ ÀνÄÇÒ ¼ö ÀÖ´Ù.
¶ÇÇÑ, Æ®¸®°Å°¡ AFTER Å°¿¡ ÀÇÇØ eventÈÄ¿¡ ±âµ¿µÇ¾îÁö¸é, ÃÖ±ÙÀÇ »ðÀÔ,UPDATE,»èÁ¦
µîÀÌ Æ®¸®°Å¿¡ "visible" ÀÌ´Ù. Áï, º¯°æµÈ ºÎºÐÀ» Æ®¸®°Å°¡ ÀÎÁöÇÒ ¼ö ÀÖ´Ù.
event´Â ´ÙÁßÀÇ event ¸¦ OR ¿¡ ÀÇÇØ ±ÔÁ¤ÇÒ ¼ö ÀÖ´Ù. ¶ÇÇÑ µ¿ÀÏÇÑ ¸±·¹À̼ǿ¡ µ¿ÀÏ
ÇÑ event ¸¦ ÁöÁ¤ÇÏ´Â Çϳª ÀÌ»óÀÇ Æ®¸®°Å¸¦ Á¤ÀÇÇÒ ¼ö ÀÖÀ¸³ª, ÀÌ´Â Æ®¸®°ÅÀÇ ±âµ¿
¼ø¼¸¦ ¿¹ÃøÇÒ ¼ö ¾ø°Ô µÈ´Ù.
Æ®¸®°Å°¡ SQL Äõ¸®¸¦ ½ÇÇàÇÒ¶§ ´Ù¸¥ Æ®¸®°ÅµéÀ» ±âµ¿½Ãų ¼ö ÀÖÀ¸¸ç À̸¦ CASCADE
Æ®¸®°Å¶ó ÇÑ´Ù. ÀÌ·¯ÇÑ Ä³½ºÄ³À̵å Æ®¸®°ÅÀÇ ·¹º§¿¡´Â Á¦ÇÑÀÌ ¾øÀ¸¹Ç·Î ÇѹøÀÇ Æ®¸®
°Å·Î ¿©·¯°³ÀÇ ´ÙÁßÀÇ Æ®¸®°Å¸¦ ±âµ¿½Ãų ¼ö ÀÖ´Ù.
µ¿ÀÏÇÑ ¸±·¹À̼ǿ¡ ´ëÇÑ INSERT Æ®¸®°Å°¡ ÀÖ´Ù¸é ÀÌ Æ®¸®°Å´Â ´Ù½Ã µ¿ÀÏÇÑ ¸±·¹À̼Ç
¿¡ ´ëÇÑ Æ®¸®°Å°¡ ±âµ¿µÇ¾îÁú ¼ö ÀÖ´Ù. ÇÏÁö¸¸ ¾ÆÁ÷ PostgreSQLÀº ÀÌ·± Æ®¸®°Å¿¡ ´ë
ÇÑ Æ©ÇÃÀÇ µ¿±âÈ°¡ Áö¿øµÇÁö ¾ÊÀ¸¹Ç·Î ÁÖÀǸ¦ ÇÏ¿©¾ß ÇÒ °ÍÀÌ´Ù.
CREATE TRIGGER Àº PostgreSQLÀÇ È®ÀåµÈ ±â´ÉÀÌ´Ù.
´ÜÁö ¸±·¹À̼Ç(Table) ÀÇ ¼ÒÀ¯ÀÚ¸¸ÀÌ ±× ¸±·¹À̼ǿ¡ Æ®¸®°Å¸¦ »ý¼ºÇÒ ¼ö ÀÖ´Ù.
¹öÁ¯ 6.4¿¡¼ STATEMENT ´Â ±¸ÇöµÇÁö ¾Ê¾Ò´Ù.
CASCADING TRIGGER SAMPLE
----------------------------------------------------Cut here !!
--coded by NoGaDa-X
--cascading tigger
DROP TRIGGER tab1_trg ON test1;
DROP TRIGGER tab2_trg ON test2;
DROP TRIGGER tab3_trg ON test3;
DROP FUNCTION tab1_func();
DROP FUNCTION tab2_func();
DROP FUNCTION tab3_func();
DROP TABLE test1;
DROP TABLE test2;
DROP TABLE test3;
DROP TABLE test4;
--Create Table
CREATE TABLE test1(
tab1_id INT4
);
CREATE TABLE test2(
tab2_id INT4
);
CREATE TABLE test3(
tab3_id INT4
);
CREATE TABLE test4(
tab4_id INT4
);
--Create Function
CREATE FUNCTION tab1_func() RETURNS opaque
AS '
BEGIN
INSERT INTO test2 values( new.tab1_id);
RETURN new;
END;
' LANGUAGE 'plpgsql';
CREATE FUNCTION tab2_func() RETURNS opaque
AS '
BEGIN
INSERT INTO test3 values( new.tab2_id);
RETURN new;
END;
' LANGUAGE 'plpgsql';
CREATE FUNCTION tab3_func() RETURNS opaque
AS '
BEGIN
INSERT INTO test4 values( new.tab3_id);
RETURN new;
END;
' LANGUAGE 'plpgsql';
--Create Trigger
CREATE TRIGGER tab1_trg AFTER
INSERT OR UPDATE ON test1 FOR EACH ROW
EXECUTE PROCEDURE tab1_func();
CREATE TRIGGER tab2_trg AFTER
INSERT OR UPDATE ON test2 FOR EACH ROW
EXECUTE PROCEDURE tab2_func();
CREATE TRIGGER tab3_trg AFTER
INSERT OR UPDATE ON test3 FOR EACH ROW
EXECUTE PROCEDURE tab3_func();
--transaction
BEGIN;
INSERT INTO test1 VALUES (1);
SELECT * from test1;
INSERT INTO test1 VALUES (2);
SELECT * from test2;
INSERT INTO test1 VALUES (3);
SELECT * from test3;
INSERT INTO test1 VALUES (4);
SELECT * from test4;
END;
-----------------------------------------------End !!