










































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











































1
2
4
Function to Perform Conditional
Update
5
7
INSERT
DELETE
UPDATE
BEFORE AFTER
8
Before Update On table
After Update On table
Before Update Row 442
After Update Row 442
Update Row 442
… other rows
10
11
Sale(SaleID, SaleDate, …)
OrderItem(OrderID, ItemID, Quantity, …)
Order(OrderID, OrderDate, …)
Inventory(ItemID, QOH, …)
SaleItem(SaleID, ItemID, Quantity, …)
AFTER INSERT UPDATE Inventory SET QOH = QOH – newrow.Quantity
WHEN newrow.QOH < newrow.Reorder INSERT {new order} INSERT {new OrderItem}
changes.
13
Savings Accounts
Inez: 5340.
Checking Accounts
Inez: 1424.
Transaction
Steps Savings Balance Checking Balance
Problem arises if transaction is not completed
14
16
CREATE FUNCTION TransferMoney(Amount Currency, AccountFrom Number, AccountTo Number) RETURNS NUMBER curBalance Currency; BEGIN DECLARE HANDLER FOR SQLEXCEPTION BEGIN ROLLBACK; Return -2; -- flag for completion error END; START TRANSACTION; -- optional SELECT CurrentBalance INTO curBalance FROM Accounts WHERE (AccountID = AccountFrom); IF (curBalance < Amount) THEN RETURN -1; -- flag for insufficient funds END IF UPDATE Accounts SET CurrentBalance = CurrentBalance – Amount WHERE AccountID = AccountFrom; UPDATE Accounts SET CurrentBalance = CurrentBalance + Amount WHERE AccountID = AccountTo; COMMIT; RETURN 0; -- flag for success END; Docsity.com
17
SAVEPOINT StartOptional; UPDATE … UPDATE … If error THEN ROLLBACK TO SAVEPOINT StartOptional; END IF COMMIT;
time
start Required elements
StartOptional Risky steps
commit
Partial rollback
Receive Payment Balance Place New Order
19
20
ID Balance Jones $ $
Read balance 800
Subtract pmt -
Save new bal. 600
Read balance Receive error message that it is locked.