francs=> create or replace rule rul_test_rule_delete as on delete to test_rule do instead nothing; CREATE RULE
francs=> /d test_rule Table "francs.test_rule" Column | Type | Modifiers --------+-----------------------+----------- id | integer | notnull name | character varying(32) | Indexes: "test_rule_pkey" PRIMARY KEY, btree (id) Rules: rul_test_rule_delete AS ON DELETE TO test_rule DO INSTEAD NOTHING rul_test_rule_update AS ON UPDATE TO test_rule DO INSTEAD NOTHING
francs=> select * from test_trigger; id | name ----+------ 1 | b 2 | b 3 | b 4 | b 5 | b 6 | b 7 | b 8 | b 9 | b 10 | b (10 rows)
2.2 创建触发器函数
1 2 3 4 5 6 7 8
CREATEORREPLACEFUNCTION func_test_trigger() RETURNStrigger LANGUAGE plpgsql AS $function$ BEGIN RAISEEXCEPTION'Attention: can not update or delete table test_trigger,Please contact francs !'; END; $function$;
2.3 创建 UPDATE 和 DELETE 触发器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
create trigger trigger_test_trigger_update BEFORE UPDATE ON test_trigger FOR EACH ROW EXECUTE PROCEDUREfunc_test_trigger(); create trigger trigger_test_trigger_delete BEFORE DELETE ON test_trigger FOR EACH ROW EXECUTE PROCEDUREfunc_test_trigger();
francs=> /d test_trigger Table "francs.test_trigger" Column | Type | Modifiers --------+-----------------------+----------- id | integer | not null name | character varying(32) | Indexes: "test_trigger_pkey" PRIMARY KEY, btree (id) Triggers: trigger_test_trigger_delete BEFORE DELETE ON test_trigger FOR EACH ROW EXECUTE PROCEDUREfunc_test_trigger() trigger_test_trigger_updateBEFOREUPDATEONtest_triggerFOREACHROWEXECUTEPROCEDUREfunc_test_trigger()
备注:在表 test_trigger 上创建 update 和 delete 两个触发器。
2.4 update 测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14
francs=> select * from test_trigger where id=1; id | name ----+------ 1 | b (1 row)
francs=> update test_trigger setname='bbb' where id=1; ERROR: Attention: can not update or delete table test_trigger,Please contact francs !
francs=> select * from test_trigger where id=1; id | name ----+------ 1 | b (1 row)
2.5 delete 测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14
francs=> select * from test_trigger where id=1; id | name ----+------ 1 | b (1 row) francs=> delete from test_trigger where id=1; ERROR: Attention: can not update or delete table test_trigger,Please contact francs !
francs=> select * from test_trigger where id=1; id | name ----+------ 1 | b (1 row)
Name CREATE RULE – define a new rewrite rule Synopsis CREATE [ OR REPLACE ] RULE name AS ON event TO table [ WHERE condition ] DO [ ALSO | INSTEAD ] { NOTHING | command | ( command ; command … ) }
附二 CREATE TRIGGER
Name CREATE TRIGGER – define a new trigger Synopsis CREATE [ CONSTRAINT ] TRIGGER name { BEFORE | AFTER | INSTEAD OF } { event [ OR … ] } ON table [ FROM referenced_table_name ] { NOT DEFERRABLE | [ DEFERRABLE ] { INITIALLY IMMEDIATE | INITIALLY DEFERRED } } [ FOR [ EACH ] { ROW | STATEMENT } ] [ WHEN ( condition ) ] EXECUTE PROCEDURE function_name ( arguments ) where event can be one of: INSERT UPDATE [ OF column_name [, … ] ] DELETE TRUNCATE