Adam Smith Adam Smith - 1 month ago 12
SQL Question

Which is the best approach for creating Shopping cart for E-commerce site

I want to make a shopping cart for an e-commerce site. So I got two solution in my mind
1) Create a single cart table for every user, if we do so then I think it will take more time to return a data for a particular user. ["Select * from cart where user_id = "user1" "]

2) Create separate cart table for each user at the time of user registration. So it will take less time but occupy more space.

Can you tell me which approach to follow or any other Idea.I know this is not the write place to ask this Ques. But I don't have nay choices.

Answer

I think that you should read some documention on how to modelize.

You have to construct your application by respecting the 3rd normal form.

Without that you will have plenty of trouble when going live in production.

So take 2 hours for read and it will save you several hours / day of hot fixing your app.

To answer the question :

Take the first solution.

One shop cart for all user.

Before thinking physical (a table) you have to think logical (entiy).

In your use case, you have a user (entity) who will put article (entity) in a shop cart (entity again! ).

Subtility here, a shop cart will containt in fact several Article.

Then you start to put relation between that :

User-0,n---Having-----1,1-ShopCart
ShopCart-0,n----Contain----0,n-Article

This is the logical view.

then you gonna transform that to physical view, and yo uhave those tables :

User(Id, name, ...)
ShopCart(Id, User_id, date_isued, ...)
Article(Id, type, name, price ...)
Cart_List(ShopCart_ID, Artcile_Id, quantity)

That's four tables, for only three entities.

The example is basic and need work before reflecting your business logic.

Comments