Simple MySQL database structure question
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 basi开发者_JAVA技巧c 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.
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.
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)
精彩评论