Doubt regarding the modeling

Asked

Viewed 34 times

0

Good evening, I have to make a database for a simple online sales store, but I am in a dilemma of which tables to create and I would like your opinion. In case, I created the tables:

  • Client
  • Products
  • Buying

In this case, the purchase would unite the two, with customer and product id, plus other fields. I’m right or I could do it differently?

1 answer

1

You need to make a relationship Many to Many on products and customers, this relationship results in a product table_customer, with a foreign key of products and customers. The need for this is that multiple customers may have purchased the same product, hence the opposite, a single product is related to multiple users (this is useful if you need to know the purchase history of a customer, or a product)

The purchase table I would change to orders, keeping the customer id and the delivery address id (if your system supports multiple addresses of the same customer), the primary key would be the purchase ID, in this table you keep other essential things like total value, type of delivery, date of purchase, such information.

Finally another name table buys_product, where I store by means of foreign order key, and product their respective primary keys, in this table include quantity, unit price and date, maybe other information if necessary.

By completely unaware of the domain of its application, some relationships One to Many and One to One are necessary, but switching in kids, this is one of the alternatives I would adopt, a well-modelled database contributes strongly to the performance of its application, since one of the most expensive processes is precisely the holding of consultations.

  • Type, when the user enters the page he sees the products and selects the ones he wants, going to the cart, with this he selects the quantity, inserts state/city etc. When finalizing the order he selects whether to log in or create a registration. After logging in he finalizes the order and goes to another page that shows his orders.

  • I had done as you said of relationships, but I was getting a lot with the tables and I was not knowing how to insert and how to do the joins, so I wanted to opt for this choice of a simple bench.

  • Your database may even be simple, but you won’t be able to escape from relationships or a monstrous Join to gather all the data you need, so the importance of M2M, O2O and O2M relationships. It is necessary above all to create a balance sheet, not complex of more and not simple of more. As the saying goes, the cheap is expensive.

  • Also requests would not have n-n relationship with customer?

  • Orders and Customers and One to Many, a customer may have multiple orders, but one order is related to only one customer. If you have questions about relationships open another question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.