DELETE事件触发器函数

最近更新时间: 2024-10-17 17:10:00

限制TDSQL PG记录不能被删除。

postgres=# CREATE OR REPLACE FUNCTION t_trigger_delete_trigger_func() RETURNS trigger AS 
postgres-# $$
postgres$# BEGIN
postgres$#     #不准许t_trigger.nc值为 TDSQL PG
postgres$#     IF OLD.nc = 'TDSQL PG' THEN    
postgres$#         RETURN NULL;
postgres$#         --RAISE EXCEPTION 'TDSQL PG不能被删除';
postgres$#     END IF;    
postgres$#     RETURN OLD;
postgres$# END;
postgres$# $$ 
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# CREATE TRIGGER t_trigger_delete_trigger BEFORE DELETE ON t_trigger FOR EACH ROW EXECUTE PROCEDURE t_trigger_delete_trigger_func();
CREATE TRIGGER
postgres=# INSERT INTO t_trigger VALUES(2,'TDSQL PG');
INSERT 0 1
postgres=# SELECT * t_trigg
postgres=# SELECT * FROM t_trigger ;
 id |           nc            
----+-------------------------
  1 | TDSQLPG_0.426093454472721
  2 | TDSQL PG
(2 rows)
postgres=# DELETE FROM t_trigger WHERE id=2;
DELETE 0
postgres=# SELECT * FROM t_trigger ;
 id |           nc            
----+-------------------------
  1 | TDSQLPG_0.426093454472721
  2 | TDSQL PG
(2 rows)