13

I have to make some changes to an e-commerce system to add some additional information and wanted to take the opportunity to possibly make some improvements and make it more flexible. When a customer places an order, we have to store several items of information with each item ordered; for example, the product price, the shipping price, the tax collected, any adjustments that were made.

I am debating if these fields should be stored discretely, such as (simplified example):

ORDER_LINE_ITEM
    OrderLineItemID
    ProductID
    Qty
    Price
    Shipping
    Handling      
    SalesTax
    Adjustment

For example, I could then calculate the total price the customer paid:

SELECT Qty*(Price+Shipping+Handling+SalesTax) As TotalCollected FROM ORDER_LINE_ITEM

Or if I should use a more indirect structure:

ORDER_LINE_ITEM
    OrderLineItemID
    ProductID
    Qty

ORDER_LINE_ITEM_ENTRIES
    OrderLineItemEntryID
    OrderLineItemID
    EntryType
    Value

For example:

1 | 1 | Price      | $10
2 | 1 | Shipping   | $5
3 | 1 | Handling   | $1
4 | 1 | SalesTax   | $1
5 | 1 | Adjustment | -$3.50

The advantage with this is that I can store additional information later on without altering the table schema. However retrieving the information and running reports becomes more complicated and slower.

Is there a best practice for storing this information in order/invoice databases?

Thanks in advance,

Dan

2
  • What did you end up picking?
    – Josh C.
    Sep 18, 2015 at 3:52
  • I ended up doing the more denormalized structure where I have Price, Shipping, Handling, etc as part of each line item and TotalPrice, TotalShipping, TotalHandling, etc as part of the oder. The order totals are recalculated by the business layer whenever any value is modified (which is rare). This makes it very fast and easy to retrieve for reports (which happens often) without the consumers of the data having to know the structure.
    – Dan C
    Sep 19, 2015 at 5:00

3 Answers 3

10

You can find several fairly standard database designs for common problems at Database Answers. Click here for their data models page.

There are several sample models having to do with invoicing.

3
  • Excellent info. I have been looking for something like this. Though I don't know how "standard" this is (e.g. they use underscores in their naming, while I have seen more modern designs done using MixedCase names)
    – Dan C
    Jul 21, 2012 at 20:15
  • 4
    @DanC - I would frankly take any model from Database Answers with a very large grain of salt. I've found their models tend to be impractically simple for real-world applications. It's good to take a look at a generic model when you're starting out on a new design. It might bring to your attention something that you haven't thought of. At the end of the day you should model your schema based on your business requirements not on someone else's.
    – Joel Brown
    Jul 22, 2012 at 13:20
  • Yes, the database answers make a better starting point than a final solution. Dec 6, 2016 at 18:06
1

I'd do a mixed approach, having both:

ORDER_LINE_ITEM
    OrderLineItemID
    ProductID
    Qty
    Price
    Shipping
    Handling      
    SalesTax
    Adjustment
    Extras

ORDER_LINE_ITEM_ENTRIES
    OrderLineItemEntryID
    OrderLineItemID
    EntryType
    Value    

And then doing

SELECT Qty*(Price+Shipping+Handling+SalesTax+Extras) As TotalCollected FROM ORDER_LINE_ITEM

Then you can work most of the time with a single table, but if you need to search for the details of an item (or add new things that affect the price), you can do it (using the extra field).

In another topic, I'd consider if all those fields should really be on ORDER_LINE_ITEM. I don't know your business, but in the ones I know, the Shipping, Handling and even price is calculated over the whole order, not on just one item (And it's not always possible to split it into separated items). It's also pretty common to have an "Admin" user or password, who can come and sell whatever he wants, inputting arbitrary fields for everything, and ignoring all normal business rules. So you might consider doing something like:

ORDER_LINE_ITEM
    OrderLineItemID
    OrderId
    ProductID
    Qty

ORDER
    OrderId
    ItemsTotalPrice
    Shipping
    Handling
    SalesTaxes
    Adjusment
    FinalPrince

You could create a details table to specify how the values of the order (taxes, shipping, etc..) were created...

In general I've found that the best approach is to have an entity which has the final information of all the order (or operation), and then additional sub-entities that specify how the "total" values were calculated.

1
  • 1
    That's a good point, and our current system does indeed have the shipping, handling, and tax at the order level. However, we are expanding integration with third party order systems, like Amazon, and they provide those values on each order item. It would make it easier to manage refunds and other transactions if we keep these values in a similar format instead of aggregating them into the order level. I am trying to do so in the best way possible.
    – Dan C
    Jul 21, 2012 at 20:07
0

Conceptually, is better ur second aproach, because order information doesnt belong directly to a product, a product may exists without " Shipping", "taxex" , besides, like this way, you are storing less data, dont need repeat product information.

ORDERITEM
    OrderItemID
    ProductID
    Qty

ORDERITEMENTRIES
    OrderItemID
    EntryType
    Value
2
  • Thanks for the feedback. The product record itself (name, description, current price, etc) would of course be in a separate table. My example is for an individual line item in the order that tells us what product ID was purchased and how much the customer paid for it. I updated the table names in my example to better illustrate this.
    – Dan C
    Jul 21, 2012 at 1:06
  • 1
    Ah srry, i got it, then, in that case, is better your first aproach, because, doesnt make sense that a line_item_order exists without a price for example, besides paying the price for extending model, just for has EntryType like "variable attribute", it doesn't worth.
    – levi
    Jul 21, 2012 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.