1

This is something I should really know, but I haven't done any db work for quite a while and just wanted some clarification on a structure for a simple mysql database.

The basic premise is a shopping cart - so we have catalogue, customer and orders.For now, the customer isn't going to sign up, they will enter details every time.

My main struggle is how to structure the customer and order table as obviously it would be good to have these separate.

Is the best method just to have the customer table with an fkey to the order table, and the order table simply references the item in the catalogue with another fkey?

Any clarification is much appreciated.

2 Answers 2

2

You have 4 tables at the very basic level: users, basket, items, basket_items.

Orders would work just like basket and basket_items so let's keep it simple for now.

A Record in basket_items is just a basket_id and an item_id - it's a link table used for resolving this kind of many-to-many relationship (each basket has many items, each item can be in many baskets).

The basket or orders table, as you say, just have a foreign key to the customers table - each order is by one, and only one customer.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you can have an orders table, in which new user session (or log in, later) a new order will be created. It will contain the customer_id, the quantity of items, the item_id (fkey) and the total (calculated dynamically if you wanted to)

3 Comments

The bit im struggling with is there can be more than one item in the basket and I don't want to be comma separating them.
The orders table can't contain IDs of all the items. You need a separate order_items table to link the two.
of course it's not convenient, but it will contain the item_id linked with the item table and the quantity of the order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.