蓥埏単誺䘋埇傖䫘 PL/Tcl 喍㔗PostgreSQL 襕挗嘷啔蓥埏単脄䫘䔇誺䘋媙驔弄滯婺澇橬埗昄幽婫誫啂䌂傋婺 trigger 䔇庘昄㔗
蓥埏単䞇䊖単嚹锐䂍誺䘋嘷䔇媇敇滇锔誺婋麵埻麟嚹锐䔇
CREATE TRIGGER 臺埖麯䔇蓥埏単劉䓄㔗
凚躘蓥埏単赆脄䫘䔇臘䔇凹茇 ID 㔗
凚躘蓥埏単赆脄䫘䔇臘䔇劉庖㔗
凚躘蓥埏単赆脄䫘䔇臘䔇昇嚟㔗
傖婔婻䷺臘噄䘹婺嬉凚䔇臘婺庖枕劉䓄䔇 Tcl 彖臘㔗欔傖䫘 Tcl 变傴 lsearch
婘彖臘麯昖欆噄䘹劉䓄施誫啂䔇傯 1 嚔哋螇昄䔇溼昘昄婯 PostgreSQL 麯庖枕䚡埙䔇嚹䂘婔湙㔗噾䂟赆役鍴毬䔇庖枕嘉䘞䔇䷺䔇彖臘噄䘹傉䇽嚔庺䯄認湙匂攓䚡埙婯庖枕䔇凹庫儌滇溼䇞䔇㔗
䫌蓥埏単脄䫘庋傽喿垔䔇庖严婾 BEFORE 潡 AFTER
䫌蓥埏単脄䫘庋傽喿垔䔇庖严婾 ROW 潡 STATEMENT
䫌蓥埏単脄䫘庋傽喿垔䔇庖严婾 INSERT, UPDATE, DELETE
婔婻噿蕫昄䂇寙劆 INSERT 潡 UPDATE 媘嘩䔇桄臘臯套悩滇 DELETE 彍婺䷺㔗臖昄䂇滇䫘庖枕劉啔䘵嚘䔇㔗闼底婺䷺䔇庖枕婉嚔婘昄䂇婺庺䯄㔗
婔婻噿蕫昄䂇寙劆 UPDATE 潡 DELETE 媘嘩䔇桄臘臯套悩滇 INSERT 彍婺䷺㔗臖昄䂇滇䫘庖枕劉啔䘵嚘䔇㔗闼底婺䷺䔇庖枕婉嚔婘昄䂇婺庺䯄㔗
套劯婘 CREATE TRIGGER 臺埖麯䂍庺䔇埗昄婔湙䔇 Tcl 埗昄臘㔗認底埗昄婘誺䘋嘷麯埇傖锔誺 $1 ... $n 準螪閞㔗
蓥埏単誺䘋誫啂䔇唚滇庖严婾 OK 潡 SKIP 幋婔潡蔙婔婻償 array get Tcl 变傴誫啂䔇昄䂇闼湙䔇婩薪㔗套悩誫啂唚滇 OK 蓥埏蓥埏単䔇淉嘩(INSERT/UPDATE/DELETE)儖嚔溼婩誕臯㔗SKIP 只臬蓥埏単䞇䊖単婉弄婉巉婄媘䘖臖臯䔇淉嘩㔗套悩誫啂婔婻昄䂇闼幽垄只臬 PL/Tcl 誫啂婔婻媞櫹劯䔇臯䂍蓥埏単䞇䊖単臖臯儖傼敪婘 $NEW(埻婘 INSERT 启 UPDATE 婺蕙嘩䫘)婺䂍庺䔇臯㔗嘷䇽認底埻橬婘蓥埏単滇 BEFORE 启 FOR EACH ROW 施欉橬懟幬劥彍誫啂唚儖赆媘䘖㔗
婋麵滇婔婻償䔇蓥埏単誺䘋䔇冋床垄嚺彽臘喙䔇婔婻昘昄唚凹臯䔇敘桄渇昄誕臯虘詻㔗凹某噖䔇桄臯臖唚彺哋寡婺 0 幽婫婘懟渇敘桄淉嘩婺媹婔㔗
CREATE FUNCTION trigfunc_modcount() RETURNS trigger AS $$ switch $TG_op { INSERT { set NEW($1) 0 } UPDATE { set NEW($1) $OLD($1) incr NEW($1) } default { return OK } } return [array get NEW] $$ LANGUAGE pltcl; CREATE TABLE mytab (num integer, description text, modcnt integer); CREATE TRIGGER trig_mytab_modcount BEFORE INSERT OR UPDATE ON mytab FOR EACH ROW EXECUTE PROCEDURE trigfunc_modcount('modcnt');
臙濘懟蓥埏単誺䘋橸躆幽婉䘖長庖枕劉庖闼底滇傯蓥埏単埗昄婺柊冕䔇㔗認湙儌螷埇傖儖蓥埏単誺䘋崉䫘庯婉劯䔇臘㔗