简体   繁体   English

带有INSERT的Oracle SQL事务

[英]Oracle SQL Transaction with INSERT

I am writing a database of warehouse of groceries. 我正在写一个杂货仓库数据库。 I have tables clients, workers, orders, products and some which are not really important in that question. 我有clients, workers, orders, products等表格clients, workers, orders, products这些表格在该问题中并不重要。

I want to avoid a problem where client makes a new order for product but there isn't enough amount of this product in our warehouse. 我想避免出现这样的问题,即客户重新订购产品,但我们的仓库中没有足够数量的该产品。

I have to make a transaction which avoid situation when two clients are making a new order at the same time and one of them ordered for example the last item while making order by the second client. 我必须进行交易,以避免两个客户同时下新订单,而其中一个在第二个客户下订单时例如最后一个项目下订单的情况。

I don't really know how to do it because only workers can decrease amount of every product. 我真的不知道该怎么做,因为只有工人才能减少每种产品的用量。 So I want so that while deleting a order (which was made by client) by worker the amount of available product was decreased. 因此,我希望在删除工人的订单(由客户做出)时,减少可用产品的数量。

I know that I can put it into trigger but the question is: 我知道可以触发它,但问题是:

how to define the transaction of ordering product with checking available amount. 如何通过检查可用金额来定义订购产品的交易。 Should I count the difference between avalaible amount in table Products and amount of ordered product by the time(unrealized orders). 我应该按时间(未实现的订单)计算表产品中的可用数量与订购产品的数量之间的差额。

How to do it ? 怎么做 ?

对于订单,您必须管理该产品的库存,每当有人要下订单时,您都必须检查产品的可用性。

One thing you could do is place a trigger on the orders table, that subtracts quantity from the products table and raises an exception if the amount is negative. 您可以做的一件事是在orders表上放置一个触发器,该触发器从products表中减去数量,并在数量为负数时引发异常。 The trigger would have like: 触发器将有:

update products set current_quantity = current_quantity - :new.amount_purchased
 where id = :new.product_id
returning current_quantity into l_current_quantity;

if l_current_quantity < 0 then
    raise_application_error( -20001, 'Amount ordered larger than remaining quantity.' );
end if;

You'll have to handle the exception in your ordering procedure. 您必须在订购过程中处理异常。

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

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