views:

202

answers:

4

I am thinking of creating a "wishlist". Each user will be able to purchase wishes in a shop. How do I store the wishlist? I am thinking of creating an user table and a wishlist table.

The structure of user table: Columns: id, username, password etc

Columns: id, wish, price, quantity etc

user id is the primary key for user table and its a foreign key in wishlist table. However, when I come to think of it, my wishlist table will have duplicate items for each user, won't it?

Is there a better way to store the wishlist things?

I am using mysql. Thanks

A: 

It is a 1 to n relationship.

Asuming you have a User table with a user_id as primary key then create a table Wish which has user_id as foreign key. Add the attributes you want to this second table and violá.

Luis

Luixv
A: 

You should have another table for wishes (wish id, price, description etc), and you should have a joining table (user id, wish id, quantity, quoted price etc). This table will allow you to have a many-to-many relationship between users and wishes.

(Further to my comment... see? You have two different suggestions already. Ultimately you have to decide what is best, and ideally your business/project requirements should guide you.)

EDIT: make that three different suggestions

Ed Guiness
can u be more specific? Thats mean i need to create 3 tables? Can u list the sample important column names for each?
yes, three tables. I've given suggestions for columns in my answer.
Ed Guiness
+1  A: 

You should also have another table to store the purchases:

Purchases

  • Id
  • UserId
  • WishId
  • PurchaseDate

Users

  • Id
  • Name
  • Password

Wishes

  • Id
  • Wish
  • Price
  • Quantity

Every time a user purchases a wish, you create a record in the Purchases table, and decrement the Quantity count for that wish.

Nippysaurus
can u be more specific? Thats mean i need to create 3 tables? Can u list the sample important column names for each?
Yep. My answer is updated to reflect this now :)
Nippysaurus
This is the most logical way of structuring based upon the question asker's specifications. You may wish to edit further to make note that if you are decrementing the quantity on every purchase, you would want to increment the quantity whenever you receive new stock.
TheTXI
A: 

How to get all records of wishlist?

hay