Three customer addresses in one table or in separate tables?

前端 未结 7 2246
慢半拍i
慢半拍i 2021-02-07 17:43

In my application I have a Customer class and an Address class. The Customer class has three instances of the Address class:

7条回答
  •  伪装坚强ぢ
    2021-02-07 18:03

    I'd go with denormalized. It's easier.

    If you normalize it, the address table would require you to remove duplicates and relink records from the customer table. If I had the same delivery and invoice address, it seems like it should link to the same record. If I change one, you're required to:

    1. Create a new address record.
    2. Relink the customer record.

    If I change it back you need to check:

    1. Does a similar address entry already exist.
    2. Relink the customer record.

    This programming overhead seems to obviate the advantage of less space that normalization seems to offer. A denormalized solution, like you pointed out, would provide faster queries and easier maintenance (programming-wise). This seems to be the deciding factor for me.

    A normalized solution, like pointed out above, would allow you to add more addresses later. But you'd have to add a field for the foreign key anyways, unless you planned on organizing the tables without linkage from the customer to the address table.

    Advantages of Normalized

    • Less space.
    • Duplicate logic kinda built in (though maybe they weren't actually duplicates?)
    • Support addition of new address fields (kinda).

    Advantages of Denormalized

    • Faster queries.
    • Less programming.

提交回复
热议问题