简体   繁体   English

postgres触发器创建

[英]postgres trigger creation

How do I only create a trigger if it does not exist? 如果它不存在,我该如何只创建一个触发器?

When I do create or replace, I get a syntax error so I am looking for a way to test for the existence of a trigger. 当我创建或替换时,我得到语法错误,所以我正在寻找一种方法来测试是否存在触发器。

I can always select * from pg_trigger, but I am sure there is a more suitable way. 我总是可以从pg_trigger中选择*,但我确信有更合适的方法。

Thanks 谢谢

Postgres can conditionally drop a trigger - see the docs . Postgres可以有条件地删除触发器 - 请参阅文档 Do this before creating the trigger, then it will always work. 创建触发器之前执行此操作,然后它将始终有效。

DROP TRIGGER IF EXISTS mytrigger ON mytable;

As Jack points out in the comments, this feature has only been available since 8.2; 正如杰克在评论中指出的那样,这个功能自8.2以来才可用; this has been out for more than four years though, so it should be available in your version. 这已经超过四年了,所以它应该在你的版本中可用。

CREATE TRIGGER (NameOfTrigger) AFTER INSERT OR UPDATE ON (NameOfTable)   

DROP TRIGGER IF EXISTS (NameOfTrigger) ON (NameOfTable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM