I am building a very small shopping cart system with php/mysql. Currently the actual shopping cart (less tables for products, categories and such) consists of two tables.
One table named shoppingCarts which includes the following columns:
- id (PK)
- userId(FK)
- dateCreated
The other table, named shoppingCartItems, consists of:
- productId (FK)
- shoppingCartId (FK)
- dateCreated
To add the amount of one product I can think of two possible scenarios:
- Add a new row for each item added to the shopping cart.
- Add a column ‘amount’ and store the amount in there.
I am wondering which option would be best, what are the ups and downs?
Maybe there is an even better solution I didn’t think of?
I think, the right solution is to have an extra column for the amount, because there is no advantage having one row per item but more redundant data. Imagine if the customer wants 50 items, you would end up with 50 identical rows of data. And you have to count the rows, either with a GROUP BY select or programatically. With the extra amount column you save data and your queries are less complex.