How to Set Customer Table with Multiple Phone Numbers? - Relational Database Design

前端 未结 2 1874
北荒
北荒 2021-02-10 13:49
CREATE TABLE Phone
(
phoneID - PK
.
.
.
);

CREATE TABLE PhoneDetail
(
phoneDetailID - PK
phoneID - FK points to Phone
phoneTypeID ...
phoneNumber ...
.
.
.
);

CREATE T         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-10 14:45

    I think you've overdesigned it. I see no use for a separate Phone + PhoneDetail table. Typically there are two practical approaches.

    1) Simplicity -Put all of the phones in the Customer record itself. Yes, it breaks normalization rules, but its very simple in practice and usually works as long as you provide (Work, Home, Mobile, Fax, Emergency). Upside is code is simply to write, time to implementation is shorter. Retrieving all the phones with a customer record is simple, and so is using a specific type of phone (Customer.Fax).

    The downsides : adding additional phone types later is a little more painful, and searching for phone numbers is kludgy. You have to write SQL like "select * from customer where cell = ? or home = ? or work = ? or emergency = ?". Assess your design up front. If either of these issues is a concern, or you don't know if it may be a concern, go with the normalized approach.

    2) Extensibility - Go the route you are going. Phone types can be added later, no DDL changes. Customer -> CustomerPhone

    Customer (
       customerId
    )
    
    CustomerPhone (
       customerId references Customer(customerId)
       phoneType references PhoneTypes(phoneTypeId)
       phoneNumber
    )
    
    PhoneTypes (
       phoneTypeId   (H, W, M, F, etc.)
       phoneTypeDescription
    )
    

提交回复
热议问题