PostgreSQL trigger for inserting an entry in table2 AFTER a row is inserted in table1 (using the values from newly inserted row in table1)

26 views Asked by At

I have displayed my database schema below.

CREATE TABLE table_group
(
    group_id serial NOT NULL,
    group_creator_id integer NOT NULL,
    group_name character varying(20) NOT NULL,
    active_status boolean NOT NULL,
    PRIMARY KEY (group_id),
)

CREATE TABLE table_group_members
(
    group_member_id serial NOT NULL,
    group_id integer REFERENCES table_group (group_id) NOT NULL,
    member_user_id integer REFERENCES table_group (group_creator_id) NOT NULL,
    PRIMARY KEY (group_member_id)
)

I want to INSERT a new row in the table table_group_members as soon a new row is inserted in table table_group.

I want to pass the group_id and group_creator_id of the newly inserted row in the table table_group as parameters to INSERT query for the table table_group_members

How should I do it using the PostgreSQL TRIGGER?

0

There are 0 answers