Reputation: 11
I'm somewhat new to SQL.
I need to make an invoice that can be queried to create a document that looks something like this:
Here's the crow's foot diagram I made:
I'm not sure if the customer shipping and billing address should be a separate entity. I'm also not so sure about the tblProductLine relationship to the tblItem and tblInvoice. Did I diagram it right?
Did I match the schema and setup the relationships correctly? Also, Could someone explain when foreign keys are necessary ? (since I wasn't sure where to put them) I'm assuming they go at the end of the 1 to many relationships to reference the previous table that called it, but I'm not sure... which tables need a foreign key?
Anyways... any help or comments would be great!
Upvotes: 0
Views: 1433
Reputation: 5421
Typically ORDERS and INVOICES though very closely related are decoupled; you're conflating them into a single entity.
The CUSTOMER places an ORDER for one or more PRODUCTS. That generates the Order Header and Order Detail. The merchant issues an INVOICE referencing the Order. But in a very simple mom-and-pop operation you could dispense with ORDERS and let the INVOICE entity represent the order.
ORDER|INVOICE DETAIL
id int PK
headerid foreign key references INVOICEHEADERS or ORDERHEADERS *mutatis mutandis*
productid foreign key references PRODUCTS(id)
quantity
extendedamount
Foreign keys are necessary because they prevent things like creating an order for a product that does not exist, or invoicing a customer that does not exist. They ensure that the database row contains no impossibilities.
Typically you'd have CUSTOMERADDRESSES as a separate table that refers back to CUSTOMERS. A customer can have one or more addresses.
Whenever an entity can "have one or more of X" that's a sign that you need a separate table to capture the Xes.
Upvotes: 1
Reputation: 55514
Some thoughts in random order:
Employee
have an InvoiceID
? They can certainly have more than one invoice?Address
-table and give your customer a ShippingAddress
and a BillingAddress
(optional), both referencing Address
.Quantity
and Amount
in tblProductLine
?ItemDescription
, but then you can't really find out that the red and the blue Jag Something are the same productsize
and color
into a sub-table. If you need to rename a product, you only change one record instead of many.Upvotes: 1