代码之家  ›  专栏  ›  技术社区  ›  ololoken

在订单中保留易失性数据

  •  2
  • ololoken  · 技术社区  · 8 年前

    假设我有表:客户和订单,我想用不可更改的客户信息(如地址、名称等)存储订单,但不想将所有这些信息复制到订单表。有三种选择:

    a) Mapping table for base customers
    orders
        ....
        customer_id; link to the customers table
        baseCustomer_id; link to the customers_base table
        ....
    customers
        id; 
        base_id; link to the base customers table;
        ....
    customers_base
        id
        ....
    
    b) Versioning: (if new customer, create version 0 for base customer, and version 1 to have permament record)
    orders
        ....
        customer_id
        customer_version
        ....
    customers
       id
       version
       ....
    c) Create a copy of customer info for each order and store it into the same table; 
    orders
       ....
       customer_id
       ....
    customers
       id
       ....
       copy_of; refers to the customers.id if null represents base customer entity
    

    因此,问题是:从不同的角度来看,如数据库设计、可读性、实现复杂性,哪种方法更可取?

    1 回复  |  直到 8 年前
        1
  •  1
  •   jontejj    8 年前

    我推荐类似于Jeffrey L Whitledge在 database-structure-for-storing-historical-data

    Customer
    --------
    CustomerId (PK)
    Name
    AddressId (FK)
    PhoneNumber
    Email
    
    Order
    -----
    OrderId (PK)
    CustomerId (FK)
    ShippingAddressId (FK)
    BillingAddressId (FK)
    TotalAmount
    
    Address
    -------
    AddressId (PK)
    AddressLine1
    AddressLine2
    City
    Region
    Country
    PostalCode
    
    etc.
    

    每个可以更改的数据都应该分组,例如地址在这里,如果地址有任何更改,很容易生成新实体,订单行可以继续引用旧实体。

    在数据仓库术语中,这通常称为 star schema 在这里,您可以区分事实表和维度表。