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?


Option 2 is the cannonical 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.

| improve this answer | |
  • 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 '16 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 ? – BlackBurn027 Sep 23 '17 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. – Michael Green Sep 23 '17 at 10:27
  • @MichaelGreen I think you are right. Thanks for the explanation. – BlackBurn027 Sep 23 '17 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.