Postgresql delete record involving two tables and store this record in the third record - sql

I'm trying to delete a record from the table 'student', where on a Cascade delete it will remove it from the 'entry' table. But before delete i need to store this record in the third table 'cancel'.
Here is what i worked out so far:
DELETE FROM "CMPS".student
WHERE sno = '1';
CREATE TRIGGER canceled BEFORE DELETE
ON entry
FOR EACH ROW
EXECUTE PROCEDURE trigger_backup_row
CREATE OR REPLACE FUNCTION trigger_backup_row(integer)
RETURNS trigger AS
$$
BEGIN
INSERT INTO cancel (eno, excode, sno) values (NEW.eno, NEW.excode, NEW.sno);
RETURN NEW;
END;
$$
language PLPGSQL
But comes back with an errors. Any help will be much appreciated.

I suppose you need:
CREATE OR REPLACE FUNCTION trigger_backup_row()
RETURNS trigger AS
$$
BEGIN
INSERT INTO cancel (eno, excode, sno) values (OLD.eno, OLD.excode, OLD.sno);
RETURN OLD;
END;
$$
language PLPGSQL
;
CREATE TRIGGER canceled BEFORE DELETE
ON entry
FOR EACH ROW
EXECUTE PROCEDURE trigger_backup_row()
;
trigger function do not use arguments
on delete yo udon't have any NEW row - just an OLD one

Related

String modification trigger is not working

I am trying to write a trigger that executes after the INSERT completes on only newly inserted rows (I do not need to execute on old rows). The trigger should modify inserted string using split_part and replace functions.
The queries are as follows:
DROP TRIGGER IF EXISTS date_extraction ON t32upb
drop function if exists date_extraction();
CREATE FUNCTION date_extraction() RETURNS trigger AS $$
begin
NEW.filename := replace(split_part(old.filename,'_', 2), '.tif', '');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER
date_extraction
AFTER INSERT on
sentinel_2.t32upb
FOR EACH STATEMENT EXECUTE PROCEDURE
date_extraction();
What is wrong with the query? it is not working

How To Write a trigger in postgres to update another column when data inserted into another cloum with same value in same table?

Please Find below Table structure:-
CREATE TABLE emp (
empname text NOT NULL,
salary integer,
salary1 integer
);
Whenever i will insert data into emp table using below query salary1 column should be filled automatically using trigger in postgres ?
insert into emp(empname,salary) values('Tarik','1200');
I have tried below code,But it is not working for me.
CREATE OR REPLACE FUNCTION employee_insert_trigger_fnc()
RETURNS trigger AS
$$
BEGIN
update emp set salary1=NEW.salary where NEW.salary=NEW.salary;
RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';
create TRIGGER employee_insert_trigger
AFTER Insert
ON emp
FOR EACH ROW
EXECUTE PROCEDURE employee_insert_trigger_fnc();
Please help..
Thanks in advance.
Issue resolved after my R&D:-
CREATE OR REPLACE FUNCTION employee_insert_trigger_fnc()
RETURNS trigger AS
$$
BEGIN
NEW.salary1 := NEW.salary;
RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';
create TRIGGER employee_insert_trigger
BEFORE INSERT OR UPDATE
ON ami_master.emp
FOR EACH ROW
EXECUTE PROCEDURE employee_insert_trigger_fnc();

How to send current schema and affected table with notification function/trigger?

When a row is inserted or updated in a specific table (in this example it's the table called 'fpl'). How can I include the affected table and schema in the notification?
SQL as follows:
CREATE TRIGGER fpl_event
AFTER INSERT OR UPDATE ON fpl
FOR EACH ROW
EXECUTE PROCEDURE fpl_notify();
CREATE OR REPLACE FUNCTION fpl_notify()
RETURNS trigger AS $$
BEGIN
NOTIFY dbNotification, 'something got insereted in fpl!';
RETURN NULL;
END;
$$ LANGUAGE PLPGSQL;
Update:
CREATE OR REPLACE FUNCTION fpl_notify() RETURNS trigger
AS
$$
BEGIN
EXECUTE format('notify dbNotification, ''%s''', TG_TABLE_SCHEMA);
RETURN NULL;
END;
$$ LANGUAGE PLPGSQL;
read trigger special variables

Trigger for update timestamp field

I'm trying to create trigger, that updates my lastmodified field on insert/update. Here's my trigger:
CREATE OR REPLACE TRIGGER timestamp_trigger BEFORE INSERT OR UPDATE ON orders
FOR EACH ROW EXECUTE PROCEDURE timestamp_update;
and my procedure:
CREATE OR REPLACE FUNCTION timestamp_update()
RETURNS TRIGGER
AS
$$
BEGIN
UPDATE orders
SET lastmodified = getdate()
WHERE orderid = new.orderid
RETURN new;
END;
$$ language plpgsql;
Unfortunately when I try to update some field, field isn't updated. Also when i create row, the lastmodified field isn't updated.
Do not use update in a trigger like this because it may lead to endless recursive loop.
If you want to modify inserted/updated row, just change the new record:
CREATE OR REPLACE FUNCTION timestamp_update()
RETURNS TRIGGER
AS
$$
BEGIN
NEW.lastmodified = now();
RETURN new;
END;
$$ language plpgsql;
The syntax create or replace trigger is incorrect.
CREATE TRIGGER timestamp_trigger
BEFORE INSERT OR UPDATE ON orders
FOR EACH ROW EXECUTE PROCEDURE timestamp_update();

PL/PgSQL creating a conditional trigger on insert

I'm working on my first ever Trigger. When I'm doing an INSERT on table I want to conditionaly remove rows from other table.
Here is a trigger:
CREATE OR REPLACE FUNCTION clear_seen_by()
RETURNS trigger AS
$BODY$
BEGIN
IF (OLD.popup = '1') THEN
DELETE FROM news_seen_by;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Invoked by:
CREATE TRIGGER clear_seen_by
AFTER INSERT
ON news
FOR EACH STATEMENT
EXECUTE PROCEDURE clear_seen_by();
As an error I see that NEW or OLD (if I motify the trigger) is not declared/unknown. Where is the problem?
In an INSERT statement you do not have an OLD record defined.
You should use NEW.popup instead, and also declare the trigger to be FOR EACH ROW.
CREATE OR REPLACE FUNCTION clear_seen_by() RETURNS trigger AS
$BODY$
BEGIN
IF (NEW.popup = '1') THEN
DELETE FROM news_seen_by;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE COST 100;
CREATE TRIGGER
clear_seen_by
AFTER INSERT ON
news
FOR EACH ROW EXECUTE PROCEDURE
clear_seen_by();
You declare a trigger FOR EACH STATEMENT. Maybe you need FOR EACH ROW?
FOR EACH STATEMENT triggers do not have NEW and OLD.