【发布时间】:2013-07-10 12:19:26
【问题描述】:
我需要一个函数或触发器来解决这个问题??
customer_details :::
custid name creditid
----------------------------
2 a 1
3 b 2
4 c 3
余额金额 :::
creditid credit_type balance
-----------------------------------
1 rent 1000
1 transport 2000
1 food 1000
1 fruits 1500
2 rent 1500
2 transport 1020
2 food 1200
2 fruits 1000
3 transport 1600
3 rent 2000
3 food 1540
3 fruits 1560
Pay_the_loan :::
creditid credit_type Pay status
---------------------------------------------
1 rent 500 null
2 fruits 600 null
3 transport 400 null
1 fruits 500 null
一旦我将pay_the_loan table 中的status 列更新为ok 以获取特定的creditid,即,
(更新 pay_the_loan set status='ok' where creditid=2)
then 它应该是 deduct balance_amount 表中 balance 列中的金额,并且应该更新它,即 balance_amount 表中的 (1000-600=400 where balance_amount.credit_type=fruits 和creditid=2 来自余额金额表)
可以给我发Function 或Trigger 来解决这个问题吗?
【问题讨论】:
-
1)定义条件:什么时候应该更新?2)定义实际的更新语句:应该改变什么,如何改变?3)将2转换为函数,将1转换为触发器。4)利润!0)您需要在表格中添加一些 PG/FK。 -
好的,我会举一个这样的例子,这样可以更好地理解这一点!问题@wildplasser
标签: sql postgresql