I want to create a shopping cart in an online store. I have two ways to design shopping cart, but I don't know which one is better.

Solution 1: create a table like below :

enter image description here

in this way for each product for a user add new row.

note UserId and ProductId are uniqe

Solution 2:

use two tables: shopping cart, cartItem

enter image description here

In this way for each user just create a record in Shoopingcart Table .

What is your opinion?

  • 3
    Definitely solution 2. Solution 1 violates 1NF. Mar 29, 2016 at 9:22
  • Solution 2 is best. But I am going with solution one. 😊
    – MUFAzmi
    Jan 29 at 15:43

1 Answer 1


Option 2 is the canonical solution for the Order / OrderItem pattern, which a shopping cart is, in essence. It allows the cart to be treated as a whole and distinct from another cart that may be submitted by the same user an arbitrarily short time later. Fulfilment can be recorded against a cart rather than each item individually, if that is what is required.

  • here is similar shopping cart design with CheckedOut column but do you mean Abandonment status of carts when you said Fulfillment of cart ?
    – Shaiju T
    Sep 26, 2016 at 12:02
  • Are we going to keep all the cart items even after the order is placed with those, if so wont we have a huge table with unwanted data ? Sep 23, 2017 at 8:34
  • @BlackBurn027 That would depend on your business requirements. Typically the records would be kept for a while in case the customer has an enquiry or complaint. There may be an audit requirements to retain records for, say, seven years. Of course the data could be relocated to another system, a data warehouse perhaps, if having them in the OLTP system proves to be a problem. However, a modern installation, with good indexing and partitioning, should be able to handle many, many "cold" records. Sep 23, 2017 at 10:27
  • @MichaelGreen I think you are right. Thanks for the explanation. Sep 23, 2017 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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