postgres 触发器创建

发布于 2024-10-06 17:34:44 字数 128 浏览 0 评论 0原文

如果触发器不存在,如何仅创建触发器?

当我创建或替换时,我收到语法错误,因此我正在寻找一种方法来测试触发器是否存在。

我总是可以从 pg_trigger 选择 *,但我确信有更合适的方法。

谢谢

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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若水微香 2024-10-13 17:34:44

Postgres 可以有条件地删除触发器 - 请参阅文档。在创建触发器之前执行此操作,那么它将始终有效。

DROP TRIGGER IF EXISTS mytrigger ON mytable;

正如 Jack 在评论中指出的那样,此功能从 8.2 起才可用;不过它已经发布四年多了,所以它应该可以在您的版本中使用。

Postgres can conditionally drop a trigger - see the docs. 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; this has been out for more than four years though, so it should be available in your version.

放赐 2024-10-13 17:34:44
CREATE TRIGGER (NameOfTrigger) AFTER INSERT OR UPDATE ON (NameOfTable)   

DROP TRIGGER IF EXISTS (NameOfTrigger) ON (NameOfTable);
CREATE TRIGGER (NameOfTrigger) AFTER INSERT OR UPDATE ON (NameOfTable)   

DROP TRIGGER IF EXISTS (NameOfTrigger) ON (NameOfTable);
一曲爱恨情仇 2024-10-13 17:34:44

您可以在创建触发器时替换 postgres 中的现有触发器 - 请参阅 docs

CREATE OR REPLACE TRIGGER YOUR_TRIGGER_NAME ...

postgres 版本 12 及更高版本支持此功能。

you can replace the existing trigger in postgres while creating the trigger - see the docs

CREATE OR REPLACE TRIGGER YOUR_TRIGGER_NAME ...

This is supported in postgres version 12 and above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文