0

我对 SQL 有点陌生。

我需要制作一张可以查询的发票,以创建一个看起来像这样的文档:

截屏

这是我制作的鱼尾纹图:

鱼尾纹图

我不确定客户送货地址和帐单地址是否应该是一个单独的实体。我也不太确定 tblProductLine 与 tblItem 和 tblInvoice 的关系。我画的对吗?

我是否匹配了架构并正确设置了关系?另外,有人可以解释什么时候需要外键吗?(因为我不确定将它们放在哪里)我假设它们在 1 到多个关系的末尾去引用调用它的前一个表,但我不确定......哪些表需要外国钥匙?

无论如何...任何帮助或评论都会很棒!

4

2 回答 2

1

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.

于 2011-07-04T10:42:04.600 回答
1

一些随机顺序的想法:

  1. 对于某些实体,您的鱼尾纹位于错误的位置。一名员工可以有几张发票,但我假设一张发票由一名员工“拥有”。
  2. 为什么你Employee有一个InvoiceID?他们当然可以有不止一张发票?
  3. 考虑添加一个Address-table 并为您的客户提供 aShippingAddress和 a BillingAddress(可选),两者都引用Address.
  4. Quantityin和Amountin和有什么不一样tblProductLine
  5. 您从哪里获得运输和处理的价格?
  6. 您将尺寸存储在一个额外的字段中,而不是颜色。颜色可能是 的一部分ItemDescription,但是您无法真正发现红色和蓝色Jag Something是同一产品
    根据您的要求,最好将该表标准化并移动sizecolor子表中。如果您需要重命名一个产品,您只需更改一条记录而不是多条记录。
于 2011-07-04T10:16:24.833 回答