我需要一個函數或觸發器來解決這個問題?如何更改另一個表中的同一列時更改列(postgresql)
customer_details :::
custid name creditid
----------------------------
2 a 1
3 b 2
4 c 3
BALANCE_AMOUNT :::
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
i .E ..,
(更新pay_the_loan設置狀態= 'OK',其中creditid = 2)
then
應該deduct
在BALANCE_AMOUNT表從balance
柱的量和它應該被更新,即,(1000-600 = 400
在BALANCE_AMOUNT表where
balance_amount.credit_type =水果和creditid = 2從平衡量表)
可能交我Function
或Trigger
來解決這個親呃?
'1)'定義條件:更新何時應該發生? '2)'定義實際的更新語句:應該改變什麼,以及如何? '3)'將'2'變換成一個函數,'1'變成一個觸發器。 '4)'利潤! '0)'你需要一些表格中的PG/FK。 – wildplasser
好吧,我會帶着一個這樣的例子,讓這個更好的理解!問題@ wildplasser – 09Q71AO534