W
W
WithProg2019-03-29 17:12:39
PostgreSQL
WithProg, 2019-03-29 17:12:39

How to create a conditional foreign key in postgres?

You need to make a conditional foreign key in postgresql that refers to multiple tables.
Let's say I have tables t1, t2, t3 and t4. I want to make it so that, depending on t4.name , the my_foreignKey
field refers to tables t1, t2 and t3.
And if this is not possible in postgres, are there ways to implement such a structure?
Thanks for any support!

CREATE TABLE t1(
id integer prymary key,
type VARCHAR(20) unique not null
);
CREATE TABLE t2(
id integer prymary key,
type VARCHAR(20) 
);
CREATE TABLE t3(
id integer prymary key,
name VARCHAR(20) 
);
CREATE TABLE t4(
id integer prymary key,
name VARCHAR(20) not null,
my_forignkey integer
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vapaamies, 2019-03-31
@vapaamies

Are you sure "conditional foreign key" is a relational model? What normal form does it correspond to?
In practice, it is common to store the values ​​t1, t2, and t3 in the same table, with a composite key if required. Partitioning is used to physically separate table data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question