I want to know how to design invoice table.
An invoice include many orders from order table.
Is this invoices table is correctly designed?
order
- order_id (PK)
- User_id (FK)
- total_cost
- order_date
- status
order_item
- order_item_id (PK)
- order_id (FK)
- item_name
- cost
invoice table
- invoice_id (PK)
- invoice_no
- invoice_date
- period_start_date
- period_end_date
- status
invoice_order (an invoice with many orders)
- invoice_order_id (PK)
- invoice_id (FK)
- order_id (FK)
Is invoice_order table necessarry?
I could add invoice_id (FK) field in the order_table instead. The “order. invoice_id” would be updated when I have added a row in the invoice table.
You need the linking table. An order can be in mulitple invoices (if they didn’t pay it!) and an invoice can contain many orders. In a linking table though I would not bother with •invoice_order_id (PK). The PK is a combination of the two FK fields. That guarantees uniqueness and since you are unlikely to have a child table of the link table, you really gain nothing by adding a surrogate key to it. Even if you did the performance difference between joining on two indexed int fields vice one would probably be negligible.