Auto add data to postgresql database

I have a postgresql database with 3 Tables. One of these is to store the id from the other two tables as foreign keys. So i am using this table just to make a connection between the other two tables. The problem is that i don't know how to add id's data to this table from the other.

Upvotes: 0

Views: 415

Answers (1)

varnit
varnit

Reputation: 1897

you can use postgresql triggers for automatic insertion of data here is a little example of how to do that

   CREATE OR REPLACE FUNCTION insert_id() RETURNS TRIGGER AS $example_table$
   BEGIN
      INSERT INTO table_name(ID) VALUES (new.ID);
      RETURN NEW;
   END;
$example_table$ LANGUAGE plpgsql;

now you can call the same procedure by using the triggers functionality in postgres

CREATE TRIGGER example_trigger AFTER INSERT ON table_name
FOR EACH ROW EXECUTE PROCEDURE insert_id();

Upvotes: 1

Related Questions