HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

which way is better to design shopping Cart Table - SQL Server

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
serversqldesignwaybettercartwhichtableshopping

Problem

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:

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

Note: UserId and ProductId are unique
Solution 2:

Use two tables: Shopping Cart, Cart Item

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

What is your opinion?

Solution

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.

Context

StackExchange Database Administrators Q#133626, answer score: 11

Revisions (0)

No revisions yet.