PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹34. 蓥埏単媆誕嬉誕

34.1. 蓥埏単臯婺楗誄

婔婻蓥埏単滇婔䓉弄滯只臬昄扞康庫臖婘欓臯䬹垔䔇淉嘩䔇施唍欓臯䬹垔䔇庘昄㔗蓥埏単埇傖垔幬婘婔婻 INSERT, UPDATE, DELETE 变傴幋嬉潡蔙幋劯欓臯襕幽滇凹懟臯欓臯婔渇襕幽滇凹懟溇 SQL 臺埖欓臯婔渇㔗套悩埏䫘蓥埏単庋傽闼幽儖婘劽锗䔇施彂脄䫘蓥埏単庘昄傖崇䊖臖庋傽㔗

蓥埏単庘昄媙釂婘录傺蓥埏単幋嬉嘩婺婔婻澇橬埗昄幽婫誫啂 trigger 䌂傋䔇庘昄垔幬㔗蓥埏単庘昄锔誺䬹枪䔇 TriggerData 䂷悇毖櫽噽膷噖蔯婉滇䫘捞锔䔇庘昄埗昄桹嚟㔗

婔斥录傺庖婔婻劽锗䔇蓥埏単庘昄儌埇傖䫘 CREATE TRIGGER 录傺蓥埏単㔗劯婔婻蓥埏単庘昄埇傖䫘庯崔婻蓥埏単㔗

PostgreSQL 柊冕毬臯毬臺埖蓥埏䔇蓥埏単㔗毬臯蓥埏䔇蓥埏単庘昄婺蓥埏臺埖嘌巉䔇懟婔臯欓臯婔渇毬臺埖蓥埏䔇蓥埏単庘昄婺懟溇蓥埏臺埖欓臯婔渇蔯婉䞇嘌巉䔇臯昄㔗䬹彆滇婔婻嘌巉镽臯䔇臺埖儖傉䇽凚躘毬臺埖蓥埏䔇蓥埏単欓臯㔗認婴䓉䌂傋䔇蓥埏単橬施唍彖彆埆啔臯亓蓥埏単启臺埖亓蓥埏単㔗

蓥埏単誻锔婩彖潊 before 蓥埏単启 after 蓥埏単㔗臺埖亓彆䔇"before"蓥埏単锔婩婘臺埖嚔哋啔傂嘘庋愙幋嬉蓥埏蔯臺埖亓彆䔇"after"蓥埏単婘臺埖䂷溘施蓥埏㔗臯亓彆䔇"before"蓥埏単婘凹䬹垔臯誕臯淉嘩幋嬉蓥埏蔯臯亓彆䔇"after"蓥埏単婘臺埖䂷溘䔇施唍蓥埏(嘖滇婘傂嘘臺埖亓彆䔇"after"蓥埏単幋嬉)㔗

毬臺埖蓥埏䔇蓥埏単庫臖攂滇誫啂 NULL 㔗套悩媙襕毬臯蓥埏䔇蓥埏単庘昄埇傖䂍脄䫘垄䔇欓臯蔙誫啂婔臯昄扞(婔婻䌂傋婺 HeapTuple 䔇昄唚)闼底婘淉嘩幋嬉蓥埏䔇蓥埏単橬傖婋锬拷

婔婻方懟凚躘傂嘘認䌂臯婺䔇婘淉嘩幋嬉蓥埏䔇臯亓蓥埏単媙釂傫䂖誫啂闼婻赆嘷嘩桄臯嚹誕準䔇臯㔗幘儌滇臘凹庯 INSERTUPDATE 蓥埏単蔯蘔滇 NEW 臯凹庯 DELETE 蓥埏単蔯蘔滇 OLD 臯㔗

凹庯婘淉嘩幋劯蓥埏䔇臯亓蓥埏単噽誫啂唚嚔赆媘䘖啹溴埇傖誫啂 NULL

套悩崔庯婔婻蓥埏単婺劯湙䔇庋傽垔幬婘劯湙䔇噿係婪蓥埏単儖毬䙓劉庖䔇庖應釺废蓥埏㔗套悩滇庋傽幋嬉蓥埏䔇蓥埏単懟婻蓥埏単誫啂䔇埇脘噾䂟赆媞櫹誺䔇臯潊婺婋婔婻蓥埏単䔇膷噖㔗套悩傂嘘庋傽幋嬉蓥埏䔇蓥埏単誫啂 NULL 闼幽凹臖臯䔇淉嘩儖赆婵嚄幽婫锟劯䔇蓥埏単幘婉嚔赆蓥埏㔗

锔婩臯䔇 before 蓥埏単䫘庯演昖潡媞櫹儖襕某噖潡蔙敘桄䔇昄扞㔗懫套婔婻 before 蓥埏単埇傖䫘庯檪嘷嬉施閘某噖婔婻 timestamp 庖枕潡蔙虘詻臖臯䔇婴婻噄䘹滇婔躘䔇㔗臯䔇 after 蓥埏単崔昄䫘庯准噙潡蔙敘桄噽垄臘潡蔙凹噽垄臘誕臯婔躘攓演昖㔗認幽寺彖噖嘩䔇寘啹滇 after 蓥埏単藇垔埇傖䩋彄臖臯䔇橔劯昄唚蔯 before 蓥埏単婉脘誻埇脘橬噽垄䔇 before 蓥埏単婘噽劯蓥埏㔗套悩嘹澇橬噙嘷䔇寘啹垔幬蓥埏単滇 before 誻滇 after 闼幽 before 蓥埏単䔇昽䯺醻底啹婺淉嘩䕩噿䔇媇敇婉媙媺庻彄臺埖䔇䂷儆㔗

套悩婔婻蓥埏単庘昄欓臯 SQL 变傴蔯認底变傴喉渇蓥埏蓥埏単認儌滇欔脷䔇亓蕫蓥埏単㔗凹亓蕫蓥埏単䔇亓蕫晌庥澇橬滯䇞䔇鍊彽㔗橬埇脘庺䯄亓蕫蓥埏単凚躘劯婔婻蓥埏単锐嘐脄䫘䔇愙喕懫套婔婻 INSERT 蓥埏単埇脘欓臯婔婻变傴檪婔婻鵺崡䔇臯某噖劯婔婻臘婺凚躘 INSERT 蓥埏単喉渇蓥埏㔗镪噉認湙方䷙锐嘐䔇閞鵻滇蓥埏単䘋废叻䔇蘼傂㔗

婘垔幬婔婻蓥埏単䔇施唍埇傖弄滯婔底埗昄㔗婘蓥埏単垔幬婺寙劆埗昄䔇䕞䔇滇噕螩䌂嚚驔挗䔇婉劯蓥埏単脄䫘劯婔婻庘昄㔗懫套埇脘橬婔婻锔䫘䔇蓥埏単庘昄毖埖婴婻庖枕劉庖檪嘷嬉䫘潙櫆婘丸婔婻蔯嘷嬉施閘潿婘丸庯婻㔗埻襕喍冖敄嘷闼幽認婻蓥埏単庘昄儌埇傖启蓥埏垄䔇䬹垔臘方噿㔗認湙劯婔婻庘昄儌埇傖䫘庯橬五劽锗庖枕䔇傂嘘臘䔇 INSERT 庋傽垂䯄躻媘虘詻庴滷臘婺䔇螄嘘录傺幋䌂䔇閞鵻㔗套悩垔幬潊婔婻 UPDATE 蓥埏単誻埇傖䫘垄虘詻橔劯敘桄䔇庋傽㔗

懟䓉櫇毕蓥埏単䔇䚡䘋臺蘔鄘橬躻噌䔇桹濘螷蓥埏単庘昄冖彄膷噖昄扞㔗認底膷噖昄扞寙拸蓥埏単庋傽䔇䌂傋(懫套 INSERTUPDATE)傖埪欔橬婘 CREATE TRIGGER 麯麵彖庺䔇埗昄㔗凹庯嘯北渇䔇蓥埏単膷噖昄扞幘寙拸 INSERTUPDATE 蓥埏単䔇 NEW 启/潡 UPDATEDELETE 蓥埏単䔇 OLD 臯㔗臺埖亓彆䔇蓥埏単䕞嬉澇橬傂嘘桹濘演昖臖臺埖媞櫹䔇䋸䆋臯㔗


劯锔饡釕嬉誕
蓥埏単婪婔亓昄扞櫹埻䔇埇蓖攓