【发布时间】:2014-04-08 05:57:53
【问题描述】:
假设我有以下架构(只是一个示例),其中每个客户都有 正好是两个用途不同的电话号码:
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成为父级。通过将外键移动到
phone使customer成为父级,并使用phone中的额外列(如布尔值is_home)以区分两种类型的电话号码。
设计这个的正确方法是什么?
【问题讨论】:
-
选项3:创建第三张表,customer_id,phone_id,中间键入链接
标签: mysql sql database schema schema-design