Suppose I have the following schema (just an example) where each customer has exactly two phone numbers with distinct purposes:
create table customer (
id integer primary key,
home_phone_id integer not null,
work_phone_id integer not null,
foreign key (home_phone_id) references phone (id),
foreign key (work_phone_id) references phone (id)
);
create table phone (
id integer primary key,
number varchar(10) not null
);
The foreign keys are placed in the customer
table in order to distinguish
between the home and work phone numbers. This technically
makes phone
the "parent" table and customer
the child, even though a customer
"has" a phone number, not the other way around. (The application would never query for a phone number without its customer.)
When a customer row is deleted, I want its two rows from phone to be automatically deleted. This is a cascade from a "child" to a "parent", not from parent to child as is normal. My impression is that no SQL database supports this behavior. I could use a trigger to accomplish this, but I think the fact that it's not supported means I'm designing the schema wrong.
I can imagine two alternative designs, neither of which seems very good:
Create identical
home_phone
andwork_phone
tables, each with acustomer_id
, makingcustomer
the parent.Make
customer
the parent by moving the foreign key tophone
, and use an extra column inphone
(like a booleanis_home
) to distinguish between the two types of phone numbers.
What's the right way to design this?