Is this a good way to model address information in a relational database?

前端 未结 7 2040
走了就别回头了
走了就别回头了 2020-12-02 09:35

I\'m wondering if this is a good design. I have a number of tables that require address information (e.g. street, post code/zip, country, fax, email). Sometimes the same a

7条回答
  •  情歌与酒
    2020-12-02 10:17

    I actually use this as one of my interview questions. The following is a good place to start:

    Addresses
    ---------
    AddressId (PK)
    Street1
    ... (etc)
    

    and

    AddressTypes
    ------------
    AddressTypeId
    AddressTypeName
    

    and

    UserAddresses (substitute "Company", "Account", whatever for Users)
    -------------
    UserId
    AddressTypeId
    AddressId
    

    This way, your addresses are totally unaware of how they are being used, and your entities (Users, Accounts) don't directly know anything about addresses either. It's all up to the linking tables you create (UserAddresses in this case, but you can do whatever fits your model).

    One piece of somewhat contradictory advice for a potentially large database: go ahead and put a "primary" address directly on your entities (in the Users table in this case) along with a "HasMoreAddresses" field. It seems icky compared to just using the clean design above, but can simplify coding for typical use cases, and the denormalization can make a big difference for performance.

提交回复
热议问题