多单元产品的数据库设计

Ano*_*ous 5 database-design

我正在为零售业务设计一个数据库。有些产品可以以多个单位出售,例如,铅笔可以以一支和打出售,纸张可以以张、令和广单位出售。基本上,每种产品都可以以多个单位出售。

软件需要支持

  1. 可以接收许多单位供应商的产品。有时我们可能会订购 1 支铅笔,下次我们会订购 2 盒铅笔。
  2. 可以以多个单位销售产品,例如,我们必须能够在同一张账单中销售 1 个盒子和 2 支铅笔。
  3. 可以查询实际库存商品。

以下是我的初步设计。

Table Products
ProductId | Barcode | Name   | CurrentPriceId
1         | XXXX    | Pencil | 1

Table Prices
Id | Amount
1  | 0.49

Table Units
UnitId | Name
1      | Ea
2      | Box

Table UnitConverter
ProductId | FromUnitId | Multiplier | ToUnitId |
1         | 1          | 24         | 2        | // 24 pencils equals 1 box

Table Inventories
Id | ProductId | UnitId | Quantity | PurchasePrice
1  | 1         | 1      | 48       | 0.23

Table Invoices
Id | ProductId | UnitId | Quantity | PriceId
1  | 1         | 1      | 27       | 1 
Run Code Online (Sandbox Code Playgroud)

我的设计有什么缺陷吗?有什么我想念的吗?

小智 4

从此模型中删除了定价,只是为了使这个示例简单而精确!使用基本单位非常重要,这意味着您必须使用基本单位维护每个 SKU(条形码)。当然,可以使用基本单位或基于单位转换配置的任何其他适用单位进行购买、存储和销售。

这是我想提出的设计,请查看并告诉我您的想法!

Table: Products
ProductId | Barcode | Name   | BaseUnitId
1         | XXXX    | Pencil | 1

Table: Units
UnitId | Name
1      | Each / Pieces
2      | Box

Table: UnitConversion
ProductId | BaseUnitId | Multiplier | ToUnitId |
1         | 1          | 24         | 2        | // 24 pencils in a box

//Better to store the Inventories in Base Unit. You can perform conversion on the fly only if the requirement arise.
Table: Inventories
Id | ProductId | UnitId | Quantity 
1  | 1         | 1          | 48                //In pieces

Table Invoices
Id | ProductId | UnitId | Quantity
1  | 1         | 2      | 1.5                   //Sold/Purchased 1.5 boxes that means 36=(1.5*24) pieces

//You can perform transaction against the products in any legitimate Unit (validate against UnitConversion table)
Run Code Online (Sandbox Code Playgroud)