Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
PostgreSQL Fill in a field automatically based on the value entered in another table

My question is the following:

I have a PostgreSQL database with several tables, what I want to do is the following, I will give you three tables as an example.

The first table is called teachers that has attributes name, id and type where type can only be "IN" or "EX"

I have a second table called internal teachers with the ID of all teachers with the attribute type = "IN" and another table called external teachers with the ID attribute of all teachers where type = "EX"

The thing is that at the moment I have to manually add the ID to the internal teachers and external teachers table, I would like to know if there is any way to that when entering the teacher's ID and its type to the teachers table, the database automatically places its ID in one table or another depending on its type.

Without more to say a greeting and thanks!

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

If you want automatically insertion in internal and external tables, you can use trigger as below:

Trigger Function

create or replace function trig_fun() 
returns trigger AS
$$
begin

if(new.type='IN') then
insert into internal values(new.id);
end if;

if(new.type='EX') then
insert into external values(new.id);
end if;

return new;

end;
$$
language plpgsql

and attach it after insert event of teacher table

create trigger trig_on_insert 
after insert on 
teachers
for each row
execute procedure trig_fun()

DEMO

over 4 years ago · Santiago Trujillo Report

0

You could implement your ‘internal’ and ‘external’ tables as separate views of the teachers table.

create table teachers as
select * from (values ( 1, 'EX')) as z(id, type);
          
create view external as (select * from teachers where type = 'EX');

create view internal as (select * from teachers where type = 'IN');

The following query:

select * from external;

gives:

(1, 'EX')

Whereas selecting from 'internal' gives an empty result (because there are no internal entries).

http://sqlfiddle.com/#!17/25a5a/1

over 4 years ago · Santiago Trujillo Report

0

There is no Postgres functionality that automatically creates row in a child table on insert on the parent table. Foreign key constraints enforce integrity when the data changes and come with various options, but not the one you describe.

One option is to write trigger. You could also be explicit in the query, using Postgres’ returning syntax:

with 
    t as (
        insert into teachers(name, type)
        values (?, ?)
        returning *
    ),
    x as (
        insert into teachers_internal(id)
        select id from t where type = 'IN'
    )
insert into teachers_external(id)
select id from t where type = 'EX'

I would recommend creating foreign keys too, so the proper action is taken when rows are dropped from the parent table (you probably want to delete the children row too).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!