Click to See Complete Forum and Search --> : Table design question


bejitto101
07-03-2009, 07:54 PM
I have two tables, one called "cart" and one called "items". Now I want cart to be able to store a list of "items" and quantities. So I created another table "cart_items" that has a field for the id of the template, the id of the item and the quantity.

Is this good design, or should I add another field for the id of the "cart_item"? Or is there better way to do this all together? I'm just running in trouble when I try to update my cart my cart items don't update correctly. What I'm having to do is delete *all* the "cart_items" attached to a cart and then re-add each "cart_item" and I can't help but think there has to be a better way to do this.

Malgrim
07-04-2009, 05:39 AM
You should always have a primary key in your tables. This doesn't have to be a separate column, though. If you don't allow duplicate entries for the pair of values (cart_id,item_id), then you can define the combination of these two as your primary key for the table cart_items. Updating rows should not be a problem as long as you use both parameters in the WHERE-clause.

UPDATE cart_items SET number=? WHERE cart_id=? AND item_id=?;