I've got two tables that relate to one another in a 1:M relationship: parent
and child
.
I've constrained the parent_id
column in the child
table to match only parent
's table id values, by using the Foreign Key constraint. Now I want a third table - called another_table
- to have two FK columns that references the id
column in the parent
and child
tables.
How can I restrict the child_id
FK column in the third table, only to values from the child
table that match parent_id
value?
EDIT
E.G: As I mentioned above I've got three tables - parent
, child
and another_table
. Their content looks like this:
SELECT * FROM public.parent
id name
1 A
2 B
3 C
SELECT * FROM public.child
id name parent_id
1 A1 1
2 A2 1
3 A3 1
4 B1 2
5 C1 3
6 C2 3
SELECT * FROM public.another_table
I want to find a way within the DB structure (at the mean time), to prevent from inserting or updating a value in the third table's child_id
column, that do not comply with the child
's table data.