请高人写个简单的postgresql 触发器
发布网友
发布时间:2022-04-08 22:54
我来回答
共4个回答
懂视网
时间:2022-04-09 03:16
trigger_name ON table_name
RENAME TO new_name;
?
ALTER TRIGGER last_name_changes ON employees
RENAME TO log_last_name_changes;
?
ALTER TABLE table_name
DISABLE TRIGGER trigger_name
| ALL
You specify the trigger name after the?DISABLE TRIGGER?clause to disable a particular trigger. To disable all triggers associated with a table, you use?ALL?instead of a particular trigger name.
DROP
TRIGGER
[IF
EXISTS]
trigger_name
ON
table_name;
Postgresql流水帐(第七天):Trigger
标签:
热心网友
时间:2022-04-09 00:24
先创建个触发器函数
CREATE OR REPLACE FUNCTION trfor100w()
RETURNS trigger AS
$BODY$
DECLARE
rowcounts integer;
BEGIN
IF TG_OP='INSERT' THEN
select count(*) into rowcounts from tablename;
--tablename替换为你需要测试是否有100W条数据的表名
IF rowcounts >= 1000000 THEN
--这里执行一个删除最老一条的数据的语句
--我不知道你数据表的结构,这个不好写,
--你自己写下,直接写入sql语句就可,别忘了
--最后要加分号,比如
-- delete * from xxx;
END IF;
return NEW;
END IF;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
之后在正式创建触发器
CREATE TRIGGER tg_trfor100w
BEFORE INSERT
ON tablename
FOR EACH ROW
EXECUTE PROCEDURE trfor100w();
这里的tablename也需要替换为你需要测试是否有100W条数据的表名
热心网友
时间:2022-04-09 01:42
postgresql 存储过程/触发器 语法好像和 Oracle 的差不多
热心网友
时间:2022-04-09 03:16
1 建立一个返回为trigger的过程
CREATE OR REPLACE FUNCTION after_alphas_id() RETURNS trigger AS $BODY$
BEGIN
IF( TG_OP='DELETE' ) THEN
UPDATE titles SET alpha_at=null WHERE id=OLD.title_id;
ELSE
UPDATE titles SET alpha_at=NOW() WHERE id=NEW.title_id;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE 'plpgsql';
2 创建触发器
CREATE TRIGGER after_alphas_id
AFTER INSERT OR DELETE
ON alphas
FOR EACH ROW
EXECUTE PROCEDURE after_alphas_id();
只是一个例子,你拿去改改?