0

ER Diagram From what I understand, 1)table customer and table order should have a one to many relationship since customer has orders. 2)table order and table payment should have a one to many relationship since a single order can have multiple payments. 3)table order and table delivery should have a many to one relationship. Is this correct? Am I missing something else?

6
  • It is entirely possible to have more than one payment for an order, e.g. when two people share the payment. Also, the relationship you show between Order and Delivery is impossible -- each delivery has exactly one OrderID.
    – mustaccio
    Commented Dec 23, 2020 at 14:20
  • @mustaccio One delivery can have more than one order if orders are placed simultaneously. So I thought it should have a many-to-one relationship. Could you please elaborate more on how should I connect these two tables(Order and Delivery)? Commented Dec 23, 2020 at 14:50
  • In your diagram the Delivery entity has the OrderID attribute, meaning one deliver can only be related to one order.
    – mustaccio
    Commented Dec 23, 2020 at 15:04
  • @mustaccio So if I remove the OrderID attribute from Delivery and put DeliveryID in Order entity, would it be correct? Commented Dec 23, 2020 at 15:13
  • 1
    Price is also usually per order line as well. Otherwise when the price changes all your old orders change.
    – Brian
    Commented Dec 24, 2020 at 14:31

1 Answer 1

1

The design depends on your business requirements, but in general that sounds like a good design and valid for the context.

Some very minor things you can consider are the following:

  1. In real life different Customers can technically live at the same Address, and therefore if you normalized CustomerAddresses to their own Table, it would reduce redundancy and theoretically can improve performance.

  2. Some real life scenarios allow a Customer to use multiple Payment methods for the same Order. If this will never be a use case in your system then you can ignore this. But if it's possible you want to support that feature, then you'd need to update the relationship between Order and Payment so that it was one-to-many.

  3. You may want to consider to split the Order table into two tables OrderHeader and OrderLine. Then there would be a one-to-many relationship between the two tables, since multiple lines can go on the same header. The header would store the general Order information like OrderId, OrderDate, OrderTime, CustomerId but the OrderLine table would have a separate line for each type of item ordered, and it would store the PizzaId and Quantity (and any other specific information, such as customizations to the item ordered).

    As it stands right now with your current design, your Customer can only order one type of Pizza per Order. By splitting Order into two tables like above, they'd be able to order multiple different kinds of Pizza on the same Order (or even other types of items that may be sold later on, like cheesy bread and soda).

4
  • 1
    I understand. Thanks a lot, kind sir. Commented Dec 23, 2020 at 14:04
  • No problem, if you have any further questions, feel free to ask, and I'll do my best to answer!
    – J.D.
    Commented Dec 23, 2020 at 14:06
  • I've updated my post picture. I tried to do what you suggested. Is this correct? Commented Dec 23, 2020 at 14:17
  • @KhishigdelgerGanbold Almost. You need to add the OrderId to the OrderLine table as a foreign key so it can reference it's respective OrderHeader. It also should have it's own primary key, likely called something like OrderLineId or just LineId if you prefer.
    – J.D.
    Commented Dec 23, 2020 at 14:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.