简体   繁体   中英

how to update the records from one table to another

I have 2 tables:

1)NewAccount
2)Deposit

NewAccount table contains fields as:

1)accNum as primary key 2)amount

Deposit table contains fields as:

1)accNum as foreign key
2)DepositAmount

My question is if I click on one button it should insert all values to the deposit table as well as it should add that amount to the amount field of NewAccount table.

Here is an example:

if amount field of NewAccount has 500.00 if I add deposit amount of rs 400 .then amount should update to rs.900 in NewAccount table of amount field

在 NewAccount 表上创建更新触发器,这样一旦你更新你的 NewAccount 表,它应该触发存款更新,设置 depositAmount = ammount

    INSERT INTO NewAccount (accNum, ammount)
    SELECT accNum, DepositAmmount
    FROM Deposit a
    WHERE NOT EXISTS (SELECT 1 FROM NewAcount b where b.accNum=a.accNum)

    UPDATE c
    SET c.ammount=d.DepositAmmount + c.ammount
    FROM NewAccount c
    JOIN Deposit d on d on c.accNum=d.accNum

Create Insert trigger on Deposit Table, when you insert record in Deposit table, trigger will be invoked and NewAccount amount will be updated with respect to accNum.

delimiter |
CREATE TRIGGER updateamount AFTER INSERT ON Deposit
  FOR EACH ROW
  BEGIN
    UPDATE NewAccount set ammount = ammount + NEW.DepositAmmount where accNum = NEW.accNum;
  END;
|

delimiter ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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