clear markdown feedback
clear markdown feedback
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 3798603 fiddles created (41876 in the last week).

create table test ( id serial primary key, name varchar(128) not null );
 hidden batch(es)


create table test_archive ( id serial primary key, name varchar(128) not null );
 hidden batch(es)


CREATE OR REPLACE FUNCTION archive_record() RETURNS TRIGGER AS $$ BEGIN RAISE NOTICE 'Running trigger'; EXECUTE format('INSERT INTO %I.%I SELECT $1.*', TG_TABLE_SCHEMA, (TG_TABLE_NAME || '_archive')) USING OLD; RETURN NULL; END; $$ LANGUAGE PLPGSQL;
 hidden batch(es)


CREATE TRIGGER delete_test AFTER DELETE ON test FOR EACH ROW EXECUTE PROCEDURE archive_record();
 hidden batch(es)


insert into test values (1,'test1'),(2,'test2')
2 rows affected
 hidden batch(es)


DELETE FROM test WHERE id = 1
1 rows affected
 hidden batch(es)


SELECT * FROM test_archive
id name
1 test1
 hidden batch(es)


SELECT * FROM test
id name
2 test2
 hidden batch(es)