如何在 PostgreSQL 中编写触发器来将字段与另一个表中的字段进行比较
这是我第一次尝试触发器。我已经了解触发器背后的概念和用法,但是我不太确定如何编写该过程。
基本上,我有一个表,我们将其称为“Money”,其中有一个名为“Max_transaction_amount”的字段,以及另一个名为“Currency_ID”的字段。然后,我有一个名为“Transactions”的表。基本上,我想要实现的是一种机制,每当新交易添加到“交易”表中时,都会进行检查以查看与某种货币相关的所有交易的“计数”不超过“名为“Money”的表中的“Max_transaction_amount”,相对于“Currency_ID”。
对于有数据库经验的用户来说,这似乎是一个非常简单的问题,但是我仍在学习,并且想编写这样一个过程,以便有一个良好的开端。
不幸的是,我无法提供任何代码,因为我对如何执行此操作没有丝毫线索。然而,这就是我“认为”可能起作用的:
- 触发器需要在更新“交易”表时执行
- 该过程通过检查其“Currency_ID”来检查正在添加的记录中的“Max_transaction_amount”。
- 执行 select 语句来查找符合该特定“Currency_ID”的行数
- 将检查“Money”表中的“Max_transaction_amount”字段,以查看
- “Transactions”表中 该特定货币的最大金额是多少,将与该特定货币相关的所有记录的计数与“max_transaction_amount”进行比较,
- 如果它小于或等于最大金额,则保存更改。
任何帮助将不胜感激!
我尝试编写以下过程...虽然语法被接受,但如果我尝试超出数量,则不会发生任何情况。有人能引导我走向正确的方向吗?
BEGIN
IF ((SELECT COUNT(Currency_ID) FROM "Transactions" WHERE "Currency_ID" = NEW.Currency_ID) > (SELECT "max_transaction_amount" FROM "Money" WHERE "Currency_ID" = NEW.Currency_ID)) THEN
RAISE EXCEPTION 'Error';
END IF;
END;
This is the first time I'm trying out a trigger. I've understood the concept and use behind triggers, however I'm not too sure how to go about writing the procedure.
Basically I've got a table, which we'll call 'Money', which has a field called 'Max_transaction_amount', and another field called 'Currency_ID' Then, I've got a table called 'Transactions'. Basically, what I want to achieve is a mechanism whereby whenever a new transaction is added to the 'Transactions' table, a check is carried out to see that the 'Count' of all the transactions pertaining to a certain currency does not exceed the 'Max_transaction_amount' in the table called 'Money', with respect to the 'Currency_ID'.
It seems like a very simple problem for users experienced with databases, however I'm still learning and would like to write such a procedure to get started off on the right foot.
Unfortunately I can't provide any code because I haven't the slightest clue as to how I'm going to do this. However, this is what I 'think' might work:
- The trigger would need to execute on update of the 'Transactions' table
- The procedure checks the 'Max_transaction_amount' in the record which is being added, by checking its 'Currency_ID'.
- A select statement is executed to find the amount of rows which conform to that specific 'Currency_ID'
- The 'Max_transaction_amount' field in the 'Money' table will be checked to see what the max amount is for that particular currency
- In the 'Transactions' table, the Count of all the records pertaining to that particular currency is compared to the 'max_transaction_amount'
- If it is less than or equal to the max amount, then the changes are saved.
Any assistance would be greatly appreciated!
I've tried writing the following procedure... while the syntax is accepted, nothing happens if I try to exceed the amount. Could anyone kindly guide me in the right direction?
BEGIN
IF ((SELECT COUNT(Currency_ID) FROM "Transactions" WHERE "Currency_ID" = NEW.Currency_ID) > (SELECT "max_transaction_amount" FROM "Money" WHERE "Currency_ID" = NEW.Currency_ID)) THEN
RAISE EXCEPTION 'Error';
END IF;
END;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定我是否理解 max_transaction_amount 的存储方式,但对我来说,听起来您可以通过资金表中的余额列和检查约束来实现此目的。每次将行插入到事务表中时,余额都会更新。
现在您只需确保每次插入事务时都会更新 current_balance 。这似乎是规则有意义的罕见情况之一(但也可以在触发器中完成 - 特别是如果您需要进行更多检查):
现在更新
current_balance
列时,检查约束将确保不超过max_transaction_amount
。如果您需要覆盖表的 UPDATE,您需要创建另一个规则:
当然这两个规则也可以使用单个触发器函数来完成。
Not sure if I understood the way the max_transaction_amount is stored, but to me it sounds like you can achieve this with a balance column in the moneys table and a check constraint. The balance would then be updated each time a row is inserted into the transactions table
Now you only need to make sure that the current_balance is updated each time a transaction is inserted. This seems to be one of the rare occasions where a rule makes sense (but could be done in a trigger just as well - especially if you need to do more checks):
Now when updating the
current_balance
column, the check constraint will make sure that themax_transaction_amount
is not exceeded.If you need to cover
UPDATE
s to the table, you need to create another rule:Of course the two rules could be done with a single trigger function as well.