I have two tables (A and B), and a foreign key in B, referencing A. I'm trying to insert rows in B, but with the validation of just inserting the rows that have an existing key in A.
With this table structure, I want to achieve the following:
create table a (id int primary key);
create table b (a_id int not null);
alter table b add foreign key (a_id) references a(id);
insert into a values (1), (2), (3);
insert into b values (2), (3), (4); -- This should just insert 2, 3 and exclude 4, since it doesn't exist in table A.
select * from b; -- The query should return just the foreign key 2 and 3.
Something like that. Is there any way to achieve this in PostgreSQL? Thanks.