I have a trigger function:
CREATE OR REPLACE FUNCTION Day_21_bankTriggerFunction()
RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
DECLARE
act VARCHAR(30);
BEGIN
SELECT account_number INTO act
DELETE FROM depositor
WHERE depositor.account_number = act;
RETURN act;
END;
$$;
\`
and then I have a trigger:
CREATE TRIGGER Day_21_bankTrigger AFTER DELETE on account FOR EACH ROW EXECUTE PROCEDURE Day_21_bankTriggerFunction()
The thought behind this is that is an account was deleted from the account table then this should trigger the function to run and then delete all records on the depositor table where that account is present.
I can create the trigger function and trigger without an issues but if I attempt to delete an account from the account table...I still see the account # in the depositor table when I shouldn't.
Any thoughts?
above is what I tried. Expected results would be to delete an account from the account table and then the trigger function should kick off and remove that account from the depositor table
Usually, relationships of any mode between tables are created by foreign keys. This is the best way and are standards for DBs. Using foreign keys you can control your data. SQL sample:
Third, specify the parent table and parent key columns referenced by the foreign key columns in the
REFERENCESclause.Finally, specify the delete and update actions in the
ON DELETEandON UPDATEclauses. The delete and update actions determine the behaviors when the primary key in the parent table is deleted and updated. Since the primary key is rarely updated, theON UPDATEaction is not often used in practice. We’ll focus on theON DELETEaction. PostgreSQL supports the following actions after updating or deleting:NULLif data exists on referencing table)DEFAULT VALUESof this field if data exists on referencing table)NO ACTION)I wrote for you a sample trigger function:
Inside my trigger function, I have written two types of SQL codes. (
SECTION-1,SECTION-2). You must choose one of them.