假設我有以下模式(只是舉個例子),其中每個客戶有不同的目的 正好有兩個電話號碼:關係模式設計的:「二對一」的關係或「落後刪除級聯」
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
);
將外鍵放在customer
表中以區分家庭電話號碼和工作電話號碼之間的 。這在技術上 使phone
「父」表和customer
孩子,即使客戶 「有」電話號碼,而不是相反。 (該應用程序絕不會查詢沒有其客戶的電話號碼。)
當客戶行被刪除時,我希望它的兩行電話從 被自動刪除。這是從「孩子」到「父母」的級聯,而不是從父母到孩子的 ,這是正常的。我的印象是沒有SQL數據庫 支持這種行爲。我可以使用觸發器來實現這一點,但我認爲 不支持這一事實意味着我正在設計錯誤的模式。
我可以想像了兩種設計方案,這兩者都不似乎非常好:
創建相同
home_phone
和work_phone
表,每個 有customer_id
,使得customer
父。讓
customer
父通過移動外鍵phone
,並在phone
使用 額外的列(如布爾is_home
)兩種類型的電話號碼來區分。
什麼是正確的設計方式?
選項3:用customer_id,phone_id,類型作爲中間鏈接創建第三個表 – Randy