I want to propagate the value of a primary key column from a parent table to a specific child table when inserting a new row.
For explanatory purposes I've created following tables:
Create TABLE Material(
MatID serial PRIMARY KEY,
materialname TEXT
);
Create TABLE RealMaterial(
MatID INT REFERENCES Material(MatID),
attributereal TEXT,
PRIMARY KEY(MatID)
);
Create TABLE VirtualMaterial(
MatID INT REFERENCES Material(MatID),
attributevirt TEXT,
PRIMARY KEY(MatID)
);
When I am inserting a new material I automatically want to add either a RealMaterial
or a VirtualMaterial
(referencing the new ID). I should emphasiZe that I want to use this shared-primary-key pattern and not just single-table-inheritance.
Should I use a trigger for my purposes?