Trigger is a Mysql script which evokes on different events
before insert
after insert
before update
after update
before delete
after delete
Trigger Syntax
CREATE TRIGGER triggerName BEFORE/AFTER INSERT/UPDATE/DELETE ON Table
Example
mysql> CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));
mysql> CREATE TRIGGER sumofinsertion BEFORE INSERT ON account -> FOR EACH ROW SET @sum = @sum + NEW.amount;
Now when you insert data in account the trigger evokes
BEFORE INSERT means the trigger will execute before the insert action
Let insert some data
mysql> SET @sum = 0; mysql> INSERT INTO account VALUES(1,25.05),(2,837.50),(3,-123.00); mysql> SELECT @sum AS 'Total amount inserted';
+-----------------------+ | Total amount inserted | +-----------------------+ | 739.55 | +-----------------------+Similarly can be set for AFTER and BEFORE of INSERT / UPDATE / DELETEYou can Drop Trigger using syntaxDROP TRIGGERsumofinsertion;ThanksCheers
No comments:
Post a Comment