I added this. However, I've no idea why it's not working. I updated member_no
on customers
table, not updating it on customer_tracking
table. I've tried it by updating premember_no
, email
as well. But it's the same.
drop function if exists update_customer_tracking_on_customer_update() cascade;
drop trigger if exists update_customer_tracking_on_customer_update on customers cascade;
create function update_customer_tracking_on_customer_update() returns trigger
language plpgsql
as
$$
begin
if new.email != old.email then
update customer_tracking set email = new.email where customer_tracking.customer_id = new.customer_id;
end if;
if new.member_no != old.member_no then
update customer_tracking set member_no = new.member_no where customer_tracking.customer_id = new.customer_id;
end if;
if new.premember_no != old.premember_no then
update customer_tracking
set premember_no = new.premember_no
where customer_tracking.customer_id = new.customer_id;
end if;
return new;
end;
$$;
-- auto-generated definition
create trigger update_cust_tracking_on_cust_update_trigger
after update
on customers
for each row
execute procedure update_customer_tracking_on_customer_update();
However, if I have a separate function for each column, it all works fine. However, I don't want to have so many triggers. Is it possible to have everything in one function?
The purpose here seems how to keep the customer_tracking
table in sync with the customer
table. Often times, and always from a maintenance perspective, the best approach is no additional code at all . That is the case here: drop the customer_tracking
table, drop the trigger function and create a customer_tracking
view.
drop table customer_tracking;
drop function update_customer_tracking_on_customer_update() cascade;
create or replace view customer_tracking as
select customer_id
, email
, member_number
, premember_no
from customers;
See comparative example here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.