gpt4 book ai didi

postgresql - 插入记录时自动在 Postgres 中设置到期日期

转载 作者:行者123 更新时间:2023-11-29 12:48:59 24 4
gpt4 key购买 nike

我有一个类(class)表,其中加入日期为 now() 但需要设置 monthly_expiry_day --> 现在 + 1 个月。如何在插入类(class)时自动设置此列?

最佳答案

您需要在 BEFORE INSERT 触发器中分配此值:

CREATE OR REPLACE FUNCTION insert_course_expiry()
RETURNS trigger AS
$$
BEGIN
new.course_expiry = current_date + interval '1 year';
return new;
END;
$$
LANGUAGE plpgsql;

然后创建触发器:

create trigger set_expiry_trigger 
before insert on student_course
for each row
execute procedure insert_course_expiry();

关于postgresql - 插入记录时自动在 Postgres 中设置到期日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58380748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com