Example Of Trigger Before Update:
Here I have two table named as Product Trigger table and Product Price History table:
Scenario:
Whenever I update Unit price in my “PRODUCT TRIGGER”
table. The old record should be inserted on the “PRODUCT PRICE HISTORY” table.
Here I have two table named as Product Trigger table and Product Price History table:
PRODUCT PRICE HISTORY Table |
PRODUCT TRIGGER Table |
Now,
I’m Trying to create a trigger with the concept whenever I updating in a “PRODUCT
TRIGGER” table, the old record in the “PRODUCT TABLE” will be inserted in the “PRODUCT
PRICE HISTORY” table based on “PRODUCT TRIGGER” table “UNIT PRICE” with the time.
Code:
/*---------------------------------------------------------*/
CREATE trigger "KABIL_PRACTICE"."TRIGGER
price_history_trigger"
BEFORE UPDATE OF unit_price
ON "KABIL_PRACTICE"."product_Trigger" referencing old row as old
FOR EACH ROW
BEGIN
INSERT INTO "KABIL_PRACTICE"."product_price_history" VALUES
(
:old.product_id,
:old.product_name,
:old.supplier_name,
:old.unit_price,
Current_timestamp );
END;
/*-----------------------------------------------------------*/
Now update a record in
PRODUCT TRIGGER table:
Code:
update "KABIL_PRACTICE"."product_Trigger" set unit_price =
850 where product_id = 2;
Results:
No comments:
Post a Comment