Skip to content
Advertisement

Database structure for “customer” table having many orders per customer and many items per order

I am trying to create a database where each customer has several orders(new orders daily) and each order has several items. I had planned creating a table of customers and creating a table per order and populating this table with an “items” table. I think this approach is too complicated and cumbersome since the number of orders can reach the thousands, I don’t think having thousands of tables is maintainable. What do you think would be an appropriate structure for this? Any help is greatly appreciated.

Sorry if this is a noobish question, I am learning to program. And this is my first ever attempt at database design.

Advertisement

Answer

You need four tables, something like this:

Possible Simplified Database Model

Customers

Contains a list of customers. One row per Customer. Would contain all the customers information – their contact details, etc…

Orders

Contains a list of orders. One row per order. Each order is placed by a customer and has a Customer_ID – which can be used to link back to the customer record. Might also store the delivery address, if different from the customers address from their record – or store addresses in separate tables.

OrderItems

Contains a list of order items. One row for each item on an order – so each Order can generate multiple rows in this table. Each item ordered is a product from your inventory, so each row has a product_id, which links to the products table.

Products

Contains a list of products. One row per product. Similar to the customers table, but for products – contains all the product details.

Here’s the SQL code that you could use to create this structure – it will create a database for itself called mydb:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement