简体   繁体   中英

Trigger in postgres

Good day,

I will appreciate any help that you may provide for the problem I am working on.

I have two tables below:

create table account (
account_number varchar(1000), 
branch_name varchar(1000), 
balance numeric(9,2)
);

insert into account (account_number, branch_name, balance) values ('acc1', 'Branch1', 100000);
insert into account (account_number, branch_name, balance) values ('acc2', 'Branch2', 150000);

create table depositor (cust_ID integer, account_number varchar(1000));
insert into depositor (cust_ID, account_number) values (1, 'acc1');
insert into depositor (cust_ID, account_number) values (2, 'acc2');
insert into depositor (cust_ID, account_number) values (3, 'acc3');

The task is to create a SQL trigger to carry out the following action: If an account is deleted, then write a trigger to delete the dependent tuple(s) from the depositor table for every owner of the deleted account .

Here is my progress - I assume there is a need to create function and appropriate trigger:

CREATE FUNCTION my_function() RETURNS TRIGGER AS $_$
BEGIN
    DELETE FROM depositor WHERE depositor.account_number = OLD.account_number;
    RETURN NEW;
END;
$_$ LANGUAGE 'plpgsql';


CREATE TRIGGER my_trigger
BEFORE DELETE ON account 
FOR EACH ROW 
EXECUTE PROCEDURE my_function();

But I get the following error message: "stack depth limit exceeded"

Thanks in advance for your help.

That is not the fault of the trigger you are showing. There must be another trigger on depositor (or on account ) that causes recursion.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM