The accepted answer for this question is super useful: How can you represent inheritance in a database?
I really like this solution which he calls Table Per Type Inheritance:
CREATE TABLE policies (
policy_id int,
date_issued datetime,
-- // other common attributes ...
);
CREATE TABLE policy_motor (
policy_id int,
vehicle_reg_no varchar(20),
-- // other attributes specific to motor insurance ...
FOREIGN KEY (policy_id) REFERENCES policies (policy_id)
);
CREATE TABLE policy_property (
policy_id int,
property_address varchar(20),
-- // other attributes specific to property insurance ...
FOREIGN KEY (policy_id) REFERENCES policies (policy_id)
);
However, I don't understand how to do a select statement. At the end of the answer he says:
Searching for all the policies regardless of the subtype now becomes very easy: No UNIONs needed - just a SELECT * FROM policies.
That will return all the data from policies, but what about the subtypes? Should the statement be this: SELECT * FROM policies INNER JOIN policy_motor ON policies.policy_id=policy_motor.id INNER JOIN policy_property ON policy_property.id=policies.id WHERE date_issued = yesterday
For my particular case, I have 70 subtypes. Will I need to inner join all 70 tables or am I missing some easy way to do this?