Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question