sqlddlshopping-cart

Is it normal for the order_id foreign key to be null in shopping cart?


There are the following entities:

Employees

Customers

Products

Orders

Cart_items

Main question is:

"Is it ok to contain NULL values in order_id of Cart_items, when order is not created?" I mean:

  1. is it optimal, or is there a better way to organize the table structure in a database?;
  2. does this structure satisfy the three normal forms?.

I saw this topic: Can a foreign key be NULL and/or duplicate?. But there is no mention of forming a shopping cart structure.


Solution

  • The design is more or less okay. However right now, it looks like cart_items is dependent on orders which is not exactly true. If the use case needs to be extended to include shipping information, it would look weird to have shipping_id in cart_items.

    Usually a cart is a temporary place for the user to place the items he intends to buy. Once he decides to move forward with the purchase, an order is created with the items in the cart and the cart is emptied.

    Mixing ordered items and cart items in the same table is not a good approach. Cart items needs to be accessed fast, hence the table should not be too big in size. Storing ordered items and cart items, will definitely have detrimental effects on performance on the long run. Cart items are also subjected to deletion operations while ordered items are never deleted, a simple bug in the app can also delete ordered items.


    A better approach would be to have a separate table order_items which keeps track of the items of an order.

    Order_items


    An order cannot exist on its own, having customer_id in orders would make sense.

    Orders


    While the table cart_items are just the items the user has in his cart.

    Cart_items