Beginner LevelPostgreSQL
What is PostgreSQL Table Inheritance and what are its trade-offs?
❓ Interview Prompt: Explain Table Inheritance (`CREATE TABLE child () INHERITS (parent)`), querying parent tables, and foreign key / index limitations.
PostgreSQL supports Table Inheritance ('INHERITS'), allowing child tables to inherit all columns from a parent table:
Trade-offs & Limitations:
•
Querying the parent table automatically includes rows from all child tables (unless 'ONLY parent' is specified).
•
Crucial Limitation: Foreign keys and unique constraints on parent tables do NOT propagate to child tables! (Use Declarative Partitioning instead for modern setups).
Code Implementation Example
sql
CREATE TABLE cities (
name TEXT,
population NUMERIC
);
CREATE TABLE capitals (
state CHAR(2)
) INHERITS (cities);
-- Querying parent table reads from child tables too!
SELECT * FROM cities;💡Defining parent and child tables using PostgreSQL Table Inheritance.
🎯 Interview Key Takeaway
Prefer Declarative Partitioning over legacy Table Inheritance for partitioned data models.
#Table Inheritance#INHERITS#Schema Design#Beginner
Back to all PostgreSQL Questions→