Need Trigger in MySQL db
There are three tables, Orders, Executed, Completed.
'Orders' table with orderid, stockname, price, no of stocks to be purchased, date
1, ball, 10, 100, 21/5/16
the execution can happen with reference to orderid, in small batches of orders. So when the required no of stocks are bought, lets say in 3 small transactions, which are stored in 'executed' table which the following details
executionid, orderid, stockname, price of purchase, no of stocks purchased, date
1,1,ball, 10, 25, 21/5/16
2,1,ball, 10, 25, 22/5/16
3,1,ball, 10, 50, 23/5/16
then when the orders are completed, we need to transfer the order id row from order table to Completed table, which will look like
comepletedid, orderids, executionids, stockname, price of purchase, no of stocks purchased, date
1,1,1,ball, 10, 25, 21/5/16
2,1,2,ball, 10, 25, 22/5/16
3,1,3,ball, 10, 50, 23/5/16
(order no = executed no with same order id then remove from orders table and put it in completed table), to make sure that orders table has only pending orders not completed orders entries.
How to write this trigger in db code for this - removing values from orders table and inserting them into competed table when the no of stocks to be purchased equal no of stocks purchased. ex For 100 stocks required to buy, at the level of 50 stocks bought, I want the transaction table updated for 50 stocks and in orders table the required quantity reduced to 50, and then when orders = executed, then remove the data from orders table.
I had a thought on order status column within orders, but dropped the idea as - the orders table would be queried every second almost for executions, its better in the longrun to have orders table only for pending orders not executed orders
Aucun commentaire:
Enregistrer un commentaire