简体   繁体   English

库存管理数据库设计

[英]Stock management database design

I'm creating an Intranet for my company, and we want to have a stock management in it.我正在为我的公司创建一个 Intranet,我们希望在其中进行库存管理。 We sell and rent alarm systems, and we want to have a good overview of what product is still in our offices, what has been rented or sold, at what time, etc.我们销售和出租警报系统,我们希望很好地了解我们办公室中还有哪些产品、哪些产品已出租或出售、什么时间等。

At the moment I thought about this database design :目前我想到了这个数据库设计:

在此处输入图片说明

Everytime we create a new contract, this contract is about a location or a sale of an item.每次我们创建一个新合同时,这个合同都是关于一个地点或一个项目的销售。 So we have an Product table (which is the type of product : alarms, alarm watches, etc.), and an Item table, which is the item itself, with it unique serial number.所以我们有一个 Product 表(它是产品的类型:闹钟、闹钟等)和一个 Item 表,它是项目本身,带有唯一的序列号。 I thought about doing this, because I'll need to have a trace of where a specific item is, if it's at a client house (rented), if it's sold, etc. Products are related to a specific supplier, to whom we can take orders.我考虑过这样做,因为我需要跟踪特定物品的位置,如果它在客户房屋(租用),是否已售出等。产品与特定供应商有关,我们可以与谁联系接受命令。 But here, I have a problem, shouldn't the order table be related to Product ?但是在这里,我有一个问题,订单表不应该与 Product 相关吗?

The main concern here is the link between Stock, Item, Movement stock.这里主要关注的是库存、项目、运动库存之间的联系。 I wanted to create a design where I'd be able to see when a specific Item is pulled out of our stock, and when it enters the stock with the date.我想创建一个设计,让我能够看到特定项目何时从我们的库存中撤出,以及何时进入带有日期的库存。 That's why I thought about a Movement_stock table.这就是为什么我想到了 Movement_stock 表。 The Type_Movement is either In / Out. Type_Movement 是输入/输出。 But I'm a bit lost here, I really don't know how to do it nicely.但是我在这里有点迷茫,我真的不知道该怎么做。 That's why I'm asking for a bit of help.这就是为什么我需要一些帮助。

I have the same need, and here is how I tackled your stock movement issue (which became my issue too).我有同样的需求,这是我如何解决您的库存变动问题(这也成为我的问题)。

在此处输入图片说明

In order to modelize stock movement (+/-), I have my supplying and my order tables.为了对库存变动 (+/-) 进行建模,我有我的supplying表和order表。 Supplying act as my +stock, and my orders my -stock.供应作为我的+库存,我的订单是我的-库存。

If we stop to this, we could compute our actual stock which would be transcribed into this SQL query:如果我们停下来,我们可以计算我们的实际库存,并将其转录为这个 SQL 查询:

SELECT
    id,
    name,
    sup.length - ord.length AS 'stock'
FROM
    product
# Computes the number of items arrived
INNER JOIN (
    SELECT
        productId,
        SUM(quantity) AS 'length'
    FROM
        supplying
    WHERE
        arrived IS TRUE
    GROUP BY
        productId
) AS sup ON sup.productId = product.id
# Computes the number of order
INNER JOIN (
    SELECT
        productId,
        SUM(quantity) AS 'length'
    FROM
        product_order
    GROUP BY
        productId
) AS ord ON ord.productId = product.id

Which would give something like:这会给出类似的东西:

id  name            stock
=========================
 1  ASUS Vivobook       3
 2  HP Spectre         10
 3  ASUS Zenbook        0
    ...

While this could save you one table, you will not be able to scale with it, hence the fact that most of the modelization (imho) use an intermediate stock table, mostly for performance concerns.虽然这可以为您节省一张表,但您将无法使用它进行扩展,因此大多数建模(恕我直言)使用中间stock表,主要是出于性能考虑。

One of the downside is the data duplication, because you will need to rerun the query above to update your stock (see the updatedAt column).缺点之一是数据重复,因为您需要重新运行上面的查询来更新您的库存(请参阅updatedAt列)。

The good side is client performance.好的一面是客户的表现。 You will deliver faster responses through your API.您将通过 API 提供更快的响应。

I think another downside could be if you are managing high traffic store.我认为另一个缺点可能是如果您正在管理高流量商店。 You could imagine creating another table that stores the fact that a stock is being recomputed, and make the user wait until the recomputation is finished (push request or long polling) in order to check if every of his/her items are still available (stock >= user demand).您可以想象创建另一个表来存储正在重新计算库存的事实,并让用户等待直到重新计算完成(推送请求或长轮询)以检查他/她的每个项目是否仍然可用(库存>= 用户需求)。 But that is another deal...但这是另一笔交易......

Anyway even if the stock recomputation query is using anonymous subqueries, it should actually be quite fast enough in most of the relatively medium stores.无论如何,即使股票重新计算查询使用匿名子查询,它实际上在大多数相对中等的商店中应该足够快。

Note注意

You see in the product_order , I duplicated the price and the vat.您在product_order看到,我复制了价格和增值税。 This is for reliability reasons: to freeze the price at the moment of the purchase, and to be able to recompute the total with a lot of decimals (without loosing cents in the way).这是出于可靠性的原因:在购买时冻结价格,并能够用大量小数重新计算总数(不会丢失美分)。

Hope it helps someone passing by.希望能帮到路过的人。

Edit编辑

In practice, I use it with Laravel , and I use a console command , which will compute my product stock in batch (I also use an optional parameter to compute only for a certain product id), so my stock is always correct (relative to the query above), and I never manually update the stock table.在实践中,我将它与Laravel一起使用,并且我使用了一个控制台命令,它将批量计算我的产品库存(我还使用了一个可选参数来仅计算某个产品 id),因此我的库存始终是正确的(相对于上面的查询),我从不手动更新库存表。

This is an interesting discussion and one that also could be augmented with stock availability as of a certain date... This means storing:这是一个有趣的讨论,并且还可以增加某个日期的库存可用性......这意味着存储:

  • Planned Orders for the Product on a certain date特定日期的产品计划订单
  • Confirmed Orders as of a certain date特定日期的确认订单
  • Orders Delivered已交付订单
  • Orders Returned (especially if this is a hire product)退货订单(特别是如果这是租用产品)

Each one of these product movements could be from and to a location这些产品移动中的每一个都可能来自和到达一个位置

The user queries would then include:用户查询将包括:

  • What is my overall stock on hand我手头的总库存是多少
  • What is due to be delivered on a certain date什么应在特定日期交付
  • What will the stock on hand be as of a date overall截至某日,手头的库存总量是多少
  • What will the stock on hand be as of a date for a location截至某个地点的日期,手头的库存将是多少

The inventory design MUST take into account the queries and use cases of the users to determine design and also breaking normalisation rules to provide adequate performance at the right time.库存设计必须考虑到用户的查询和用例来确定设计并打破规范化规则以在正确的时间提供足够的性能。

Lots to consider and it all depends on the software use cases.需要考虑很多,这一切都取决于软件用例。

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

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