Zaktualizowanie dany po usunieciu rekordow?

0

Witam,

Mam 4-5 tabelek nawiazujacych do kuponow/znizek w sklepie online. Jak usuwam rekordy z glownej tabeli kupon, chce zaktualizowac w kazdej z 5 tabel, kolumne removed_date do daty `NOW(). Baza danych postgreSQL.

0

Poczytaj o triggerach.

2

Można i bez triggerów :)

begin

/*
create temp table kupony
(
id serial primary key,
nazwa text,
removed_date timestamp)
on commit drop;

insert into kupony(nazwa) values('kupon 1'), ('drugi kupon'), ('a to trzeci'), ('no i czwarty'), ('ktoś to czyta?'), ('jeszcze jeden'), ('kolejny');


create temp table t1
(
id serial,
kupon_id int references kupony(id),
insert_date timestamp not null default clock_timestamp(),
removed_date timestamp)
on commit drop;

insert into t1(kupon_id) select id from kupony order by random() limit (1+random()*(select count(*)-1 from kupony))::int;


create temp table t2
(
id serial,
kupon_id int references kupony(id),
insert_date timestamp not null default clock_timestamp(),
removed_date timestamp)
on commit drop;

insert into t2(kupon_id) select id from kupony order by random() limit (1+random()*(select count(*)-1 from kupony))::int;

create temp table t3
(
id serial,
kupon_id int references kupony(id),
insert_date timestamp not null default clock_timestamp(),
removed_date timestamp)
on commit drop;

insert into t3(kupon_id) select id from kupony order by random() limit (1+random()*(select count(*)-1 from kupony))::int;

*/


select 't1'::text, * from t1
union
select 't2'::text, * from t2
union
select 't3'::text, * from t3
order by 1,2
;

with x as
(
 update kupony set removed_date=clock_timestamp() where id=4 returning id
)
, u1 as
(
update t1 set removed_date = clock_timestamp() where kupon_id in (select id from x) returning id
)
, u2 as
(
update t2 set removed_date = clock_timestamp() where kupon_id in (select id from x) returning id
)
, u3 as
(
update t3 set removed_date = clock_timestamp() where kupon_id in (select id from x) returning id
)
select * from u3
;

select 't1'::text, * from t1
union
select 't2'::text, * from t2
union
select 't3'::text, * from t3
order by 1,2;

EDIT:
Alternatywna wersja tworząca tabele i wstawiająca rekordy...

/*
create temp table kupony
(
id serial primary key,
nazwa text,
removed_date timestamp
)
on commit drop;

insert into kupony(nazwa) values('kupon 1'), ('drugi kupon'), ('a to trzeci'), ('no i czwarty'), ('ktoś to czyta?'), ('jeszcze jeden'), ('kolejny');

create temp table t
(
id serial,
kupon_id int references kupony(id),
insert_date timestamp not null default clock_timestamp(),
removed_date timestamp
)
on commit drop;

create temp table t1(nazwa_tabeli text not null default 't1' check (nazwa_tabeli='t1')) inherits(t) on commit drop;
create temp table t2(nazwa_tabeli text not null default 't2' check (nazwa_tabeli='t2')) inherits(t) on commit drop;
create temp table t3(nazwa_tabeli text not null default 't3' check (nazwa_tabeli='t3')) inherits(t) on commit drop;

insert into t1(kupon_id) select id from kupony order by random() limit (1+random()*(select count(*)-1 from kupony))::int;
insert into t2(kupon_id) select id from kupony order by random() limit (1+random()*(select count(*)-1 from kupony))::int;
insert into t3(kupon_id) select id from kupony order by random() limit (1+random()*(select count(*)-1 from kupony))::int;

*/

1 użytkowników online, w tym zalogowanych: 0, gości: 1