postgresql - Postgres 将列值更新为 time.Now()
问题描述
当列更新到paid_at
表time.Now()
时orders
,我需要将列更新到表上是否可以在 postgres 上创建触发器?他们都在同一张桌子上status
paid
orders
解决方案
是否可以在 postgres 上创建触发器
是的,这是可能的。
如手册中所述,您首先需要一个触发功能:
create function update_paid_at()
returns trigger
as
$$
begin
new.paid_at := now();
return new;
end;
$$
language plpglsql;
然后你需要一个触发器定义:
create trigger update_orders_trigger
before update on orders
for each row
when (new.status = 'paid' and new.status <> old.status)
execute procedure update_paid_at();
触发器仅在状态更改为'paid'
然后设置paid_at
列的值时触发。
推荐阅读
- excel - 如何用Excel写一个月的工作日?
- android - 我无法从数组列表中删除项目。如何从android中的数组列表中删除项目?
- git - 从 git 历史记录中永久删除文件
- python - 我可以关闭由不同进程建立的连接吗
- python-3.x - 如何从列表列表中随机选择
- google-apps-script - 如何重构较长的 Google Sheet 公式或转换为自定义函数?
- python - 用户输入最后一条数据后的 UnboundLocalError
- xpath - Xpath 用于在任何其他元素或元素结尾之前获取此元素之后的所有元素?
- java - java.lang.IllegalStateException 如果在蓝牙扫描期间滚动
- ios - 延迟出列可重用单元格