Enforcing Business Rules with Oracle Triggers for Product Models and Pricing
This guide describes the creation of Oracle triggers that enforce business rules regarding product models for PCs, laptops, and printers. The first trigger, `ProductModelCheck`, ensures that a product model is valid by checking its existence among the registered PCs, laptops, or printers. The second trigger, `CHECK_PRICE`, prevents the price update of a PC if a lower-priced PC with the same speed exists. Examples demonstrate the triggers' functionality and how they help maintain data integrity within the product database.
Enforcing Business Rules with Oracle Triggers for Product Models and Pricing
E N D
Presentation Transcript
Exercise – PCs, Laptops, Printers Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) Create triggers to enforce the following business rules. • A model of a product must also be the model of a PC, a laptop, or a printer. • When updating the price of a PC, check that there is no lower priced PC with the same speed.
(a) CREATE OR REPLACE TRIGGER ProductModelCheck AFTER INSERT on Product FOR EACH ROW DECLARE --local variables model_local INT; CURSOR model_check IS SELECT model FROM ( (SELECT model from PC) UNION (SELECT model from Laptop) UNION (SELECT model from Printer) ) WHERE model=:new.model; BEGIN OPEN model_check; FETCH model_check INTO model_local; IF model_check%NOTFOUND THEN RAISE_APPLICATION_ERROR(-20000, 'No model number ' || :new.model || ' in PC, Laptop or Printer'); END IF; END ProductModelCheck; Now try: INSERT INTO Product (model, maker, type) VALUES (9999, 'Alex', 'PC'); Since model 9999 isn’t yet in PC, the above will fail.
(b) CREATE OR REPLACE TRIGGER CHECK_PRICE BEFORE UPDATE OF price ON PC FOR EACH ROW DECLARE pragma autonomous_transaction; price_min PC.price%TYPE; CURSOR pc_cur IS SELECT MIN(price) FROM PC WHERE speed=:new.speed; BEGIN OPEN pc_cur; FETCH pc_cur INTO price_min; IF price_min IS NOT NULL AND price_min < :old.price THEN RAISE_APPLICATION_ERROR(-20000, 'price rule violated. Lower price is: ' || price_min); END IF; CLOSE pc_cur; END; Needed in ORACLE if you are accessing the same table that caused the trigger to fire.
Now try… INSERT INTO PC(model,speed,ram,hd,rd,price) VALUES(1111,3,4,80,24,1500); INSERT INTO Product(model,maker,type) VALUES(1111,'DELL','PC'); COMMIT; INSERT INTO PC(model,speed,ram,hd,rd,price) VALUES(2222,3,4,120,32,1000); INSERT INTO Product(model,maker,type) VALUES(2222,'IBM','PC'); COMMIT; UPDATE PC SET price=1400 WHERE model=1111; This update shouldn’t go through according to our constraint, and indeed, it gives: Error report: SQL Error: ORA-20000: price rule violated. Lower price is: 1000 ORA-06512: at "THOMO.CHECK_PRICE", line 15 ORA-04088: error during execution of trigger 'THOMO.CHECK_PRICE'