Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda