如何为客户订单和账单设计表格

时间:2014-06-03 20:45:38

标签: mysql database-design foreign-key-relationship database-normalization table-relationships

enter image description here

为酒店系统设计数据库架构(订单和账单)。 附加的图像显示了数据库模式中的表。

问题是如何设计账单表,以便我可以根据客户的订单计算客户账单?

我的假设是在订单生成后计算账单,而不是反过来,例如在我们下订单之前创建一个账单。

我正在考虑this answer但是它并没有解决我的问题,因为我想从客户订单计算账单。

红色矩形显示订单和账单表之间的关系,这是卡在哪里,我不知道如何设计表。

2 个答案:

答案 0 :(得分:2)

首先是一些语言标准化:

按订单表示销售订单,OrderDetails称为行项目,而帐单通常称为销售发票。

销售发票是付款请求。当你认为有人欠你钱的时候你会发一个。

根据销售订单的条款,有人欠你钱:

  • 订单完成后
  • 服务首次交付后
  • 服务完全交付后
  • 根据销售订单的条款
  • 一段时间后

对于酒店而言,通常您在服务完全交付后要钱,但可能需要存款,或长期逗留的中间费用。

发票不一定适用于一个销售订单。您可以将多个销售订单合并为一个发票。

发票包含引用您要求付款的销售订单行项目的订单项。

您可能必须为同一个人/销售订单签发多张发票。

答案 1 :(得分:0)

EDIT 3添加了设计+清理

每个基表都是满足某些语句的行。找到陈述。

Customer is the rows satisfying: customer [CustomerId] named [CustomerName] lives at ...
Product is the rows satisfying: product [ProductId] is named [Productname] costing [ProductPrice]
OrderDetail is the rows satisfying: orderDetail [OrderDetailId] of order [OrderId] is quantity [quantity] of product [ProductId]
Order is the rows satisfying: customer [CustomerId] ordered [OrderId] on [dateOfOrder]

比尔,你想要哪些行?我猜......

Bill is the rows satisfying:
    Bill [BillId] is for order [OrderId] on [dateOfBill]  ... ???

您可以使用订单查看有关帐单的一些信息。你必须确定除了它的日期和顺序之外你还想知道一个账单(例如写一个账单),然后是什么语句账单行满足(即完成“...”)直接给你这些信息(如同它的日期)或间接(与其订单一样)。

我问了

  

除了日期和顺序之外你还想了解一个账单(例如写一个账单)

BillId
dateOfBill
OrderId
order OrderId's customer's CustomerId, CustomerName, CustomerAddress ...
order OrderId's dateOfOrder
for every orderDetailId's orderDetail whose orderID = OrderId
    quantity, ProductId, ProductNam,e ProductPrice, (quantity * ProductPrice) as productProduct
sum(quantity * ProductPrice) as total
    over every orderDetail with OrderDetailId = OrderId

我问了

  

语句行满足哪些语句直接或间接地为您提供信息

你建议

  

对于账单表,我打算有以下字段   Bill BillId(PK)CustomerId(FK)OrderId(FK)dateOfBill

比尔必须直接给我们一个BillId,dateOfBill和OrderId;他们不在其他地方。但其他一切都可以间接得到。

Bill is the rows satisfying:
    bill [BillId] is for order [OrderId] and was billed on [dateOfBill]

我提到语句的原因是:需要他们查询来确定 FD,密钥,唯一性,Fks和其他约束 。 (而不是使用一个模糊的直觉。)这在设计方法ORM2,NIAM和FCO-IM中是明确的。

我通过查找其行将满足的语句来确定上述帐单的内容:

customer [CustomerId] named [CustomerName] at [CustomerAddress] ...
owes us $[total] for order [OrderId]
ordering [quantity] of product [ProductId] named [ProductName] @ price $[ProductPrice] = [productProduct] 
as recorded in bill [BillId]

这是从每个表的语句中做出的声明,除了我需要一些不在任何表中的语句,即(因此)Bill需要提供的内容。通过用表替换语句,我们将获得其值为我们想要的行的查询。