简体   繁体   English

零售数据库设计

[英]Database design for retail sales

I'm developing a complete solution for certain business. 我正在为某些业务开发完整的解决方案。 I have a specific requirement: The system must be able to register sales of products by pieces, let me explain it: If one box of cigarettes contains 20 pieces, the software must be able to register a sale for the entire box or for two cigarettes. 我有一个特定的要求:该系统必须能够按件注册产品销售,请让我解释一下:如果一盒香烟包含20件,该软件必须能够注册整个盒或两支香烟的销售。 。

But since the software must also be able to manage the inventory I'm kind of confused as how to design a database to support this feature. 但是由于该软件还必须能够管理库存,因此我对于如何设计数据库来支持此功能感到困惑。

Here's what a dummy example, I generated it a few seconds ago so I know it's horrible, I just wanted to show a simple example. 这是一个虚拟的示例,我是在几秒钟前生成的,所以我知道它很可怕,我只想显示一个简单的示例。

虚拟模型

I'm planning to add one record on ProductInventory for each available product in the whole store, whenever my client wants to sell some pieces of a certain product the software will: Filter products by barcode, order them by PiecesLeft ascending and subtract the pieces from the top product on the resultset. 我计划在整个商店中为每个商店的每个可用产品在ProductInventory上添加一条记录,每当我的客户想要出售某种产品​​的某些件时,该软件就会:通过条形码过滤产品,按PiecesLeft升序订购并从中减去结果集中的顶级产品。

Is this the correct way to do it? 这是正确的方法吗? I'm uneasy about this approach since I think it would generate too many records on ProductInventory, what do you guys think? 我对这种方法感到不安,因为我认为它会在ProductInventory上生成太多记录,你们怎么看? How would you do it? 你会怎么做?

I was thinking to merge products with identical barcode and PiecesLeft to have less records but I'm not sure how to do it (Perhaps using triggers?) 我本来打算合并具有相同条形码和PiecesLeft的产品以减少记录,但是我不确定该怎么做(也许使用触发器?)

So, to summarize, what I want is any advice you can give me and opinions about my approach. 因此,总而言之,我想要的是您可以给我的任何建议以及关于我的方法的意见。

Thank you for your time. 感谢您的时间。

When you're thinking of inventory, the problem is that the inventory you store isn't necessarily the inventory you sell. 当您考虑库存时,问题在于您存储的库存不一定是您出售的库存。 So I think the right way to do this requires a business process that accurately transforms one "kind" of inventory into another. 因此,我认为正确的方法需要一个业务流程来将一种“种类”的库存准确地转换为另一种。

Let's say you start with 100 cartons of cigarettes in your stored inventory. 假设您从存储的库存中的100盒香烟开始。 Somebody comes in and wants two cigarettes. 有人进来要两支烟。 For accurate inventory, the transformations have to look like this. 为了获得准确的库存,转换必须看起来像这样。

First transformation
100 cartons -> 99 cartons
               10 packs

Second transformation
99 cartons
10 packs ->     9 packs
               20 cigarettes

Finally, the sale is for just two cigarettes, so your end-of-transaction inventory looks like this. 最后,销售仅售两支香烟,因此您的交易结束库存看起来像这样。

99 cartons
 9 packs
18 cigarettes

It's not particularly hard to handle that on the database side. 在数据库端处理它并不是特别困难。 In addition to a plain old inventory schema, you'd need some tables that describe the valid transformations--you can't transform packs of cigarettes into bottles of beer--and you'd need stored procedures or application code to do the transformations. 除了简单的旧库存模式外,您还需要一些表来描述有效的转换-您不能将香烟盒转换成啤酒瓶-并且需要存储过程或应用程序代码来进行转换。

Handling the bar codes is highly application-dependent. 处理条形码在很大程度上取决于应用程序。 In a local nursery (where you buy plants and seedlings), bar code labels often fall off or get lost. 在当地的苗圃(您在那里购买植物和幼苗)中,条形码标签经常掉下来或迷路。 The cashiers have a notebook that contains all the bar codes. 收银员有一个包含所有条形码的笔记本。 When a plant is missing a bar code, they scan the barcode that's in the book. 当工厂缺少条形码时,他们会扫描书中的条形码。

First of all, I'd recommend to get rid of natural PK (barcode). 首先,我建议您摆脱自然的PK(条形码)。 It can be (and probably should be) a unique constraint on barcode field, but not a PK. 它可能是条形码字段上的唯一约束,并且可能不是唯一的约束。

About your cigarettes example - I'd say the box is a product itself as far as your application concerns (it might be implemented as a self-relationship in product table or as a separate entity - say selable_product which has it's own price -I'd expect block to be a bit cheaper than 10 packs ) 关于您的香烟示例-就您的应用程序而言,我想说盒子是产品本身(它可以作为产品表中的自我关系或单独的实体来实现-说selable_product ,它具有自己的价格-I' d期望块比10包便宜一点)

You say you are developing a complete solution so I assume you are also storing the sales in the database. 您说您正在开发一个complete solution所以我假设您也将销售存储在数据库中。

In your Product table you could keep track what the number of pieces for that product are. 在您的Product表中,您可以跟踪该产品的件数。 In your OrderRow (or whatever you would be calling it) you then keep track of how many pieces are sold. 然后在您的OrderRow (或任何您称呼它的行)中,跟踪售出的数量。

To optimize this and decrease the need for joins you could for example add a computed persisted column to Product that holds the total number of pieces - the number of sold pieces. 为了优化这一点并减少加入的需要,您可以例如在“ Product中添加一个计算的持久性列,该列包含总件数-已售件数。

That is all I can think of with your limited information. 仅凭您有限的信息,我便能想到这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM