AdvancedAdvanced PostgreSQL
Table inheritance in PostgreSQL
The query
SQL
-- Parent table
CREATE TABLE vehicles (
vehicle_id SERIAL PRIMARY KEY,
make VARCHAR(100),
model VARCHAR(100),
year INT,
price NUMERIC
);
-- Child tables inherit all parent columns
CREATE TABLE cars (
doors INT,
body_type VARCHAR(50)
) INHERITS (vehicles);
CREATE TABLE trucks (
payload_kg NUMERIC,
axle_count INT
) INHERITS (vehicles);
-- Query all vehicles (parent + children)
SELECT * FROM vehicles; -- includes cars and trucks
SELECT * FROM ONLY vehicles; -- only parent rowsNote
“Table inheritance: child inherits parent columns. SELECT from parent returns all descendants. ONLY restricts to single table.