4. In MySQL, a trigger is a stored program invoked automatically in
response to an event such as insert, update, or delete that
occurs in the associated table.
◦ For example, you can define a trigger that is invoked automatically before
a new row is inserted into a table.
Triggers in MySQL
5. Triggers provide another way to check the integrity of data.
Which are not possible through declarative constraints
Triggers handle errors from the database layer.
Triggers give an alternative way to run scheduled tasks. By using
triggers, you don’t have to wait for the scheduled events to run
because the triggers are invoked automatically before or after a
change is made to the data in a table.
Triggers can be useful for auditing the data changes in tables.
Auditing information in a table by recording the changes made
and who made them is called as an audit trail
Advantages of triggers
7. A row-level trigger :
◦ It is activated for each row that is inserted, updated, or deleted. For
example, if a table has 100 rows inserted, updated, or deleted, the trigger
is automatically invoked 100 times for the 100 rows affected.
A statement-level trigger:
◦ It is executed once for each transaction regardless of how many rows are
inserted, updated, or deleted.
MySQL supports only row-level triggers. It doesn’t support
statement-level triggers.
DML Triggering Type
12. CREATE TABLE
emp_hist
( empno int(5),
oldSal int(10),
newSal int(10));
After Row-Level Trigger
-- drop trigger LOG_TRIG ;
CREATE TRIGGER LOG_TRIG
AFTER UPDATE ON emp
FOR EACH ROW
BEGIN
DECLARE errorMessage VARCHAR(255);
SET errorMessage = 'Sal cannot decrease';
IF NEW.sal > OLD.sal THEN
INSERT INTO emp_hist VALUES (OLD.empno, OLD.sal,
NEW.sal);
else
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = errorMessage;
END IF;
END$$
13. Before MySQL version 5.7.2, you can only create one trigger for
an event in a table
◦ e.g., you can only create one trigger for the BEFORE UPDATE or AFTER
UPDATE event.
MySQL 5.7.2+ lifted this limitation and allowed you to create
multiple triggers for a given table that have the same event and
action time.
These triggers will activate sequentially when an event occurs.
Create Multiple Triggers
14. CREATE TRIGGER trigger_name
{BEFORE|AFTER}{INSERT|UPDATE|
DELETE}
ON table_name FOR EACH ROW
{FOLLOWS|PRECEDES}
existing_trigger_name
BEGIN
-- statements
END$$
Create Multiple Triggers
In this syntax, the FOLLOWS or PRECEDES
specifies whether the new trigger
should be invoked before or after an
existing trigger.
• The FOLLOWS allows the new
trigger to activate after an existing
trigger.
• The PRECEDES allows the new
trigger to activate before an existing
trigger.
16.
SHOW TRIGGERS statement to show the triggers, you will not see the
order that triggers activate for the same event and action time
Notice that to execute the SHOW TRIGGERS statement, you need to have the SUPER
privilege.
SHOW TRIGGERS
FROM database_name;
SHOW TRIGGERS
FROM database_name
LIKE 'pattern';
SHOW TRIGGERS
IN database_name;
Information on trigger order
17. Sales persons should always receive commission of Rs. 100 at
least.. Employees who are not sales persons should never
receive commission.
Salaries should never be decreased. Increments should not be
more than 10% of the previous salary
Emp table cannot be updated after office hour and on weekend
Recap and Practice
Editor's Notes
#4: SET GLOBAL log_bin_trust_function_creators = 1;
#9: First, specify the name of the trigger that you want to create after the CREATE TRIGGER keywords. Note that the trigger name must be unique within a database.
Next, specify the trigger action time which can be either BEFORE or AFTER which indicates that the trigger is invoked before or after each row is modified.
Then, specify the operation that activates the trigger, which can be INSERT, UPDATE, or DELETE.
After that, specify the name of the table to which the trigger belongs after the ON keyword.
Finally, specify the statement to execute when the trigger activates. If you want to execute multiple statements, you use the BEGIN END compound statement.
#10: CREATE OR REPLACE TRIGGER chk_emp_sal
AFTER UPDATE OF sal
ON emp
FOR EACH ROW
BEGIN
IF :NEW.sal < :OLD.sal
THEN
RAISE_APPLICATION_ERROR(-20101,’Salary cannot be decremented’);
END IF;
END;