+ All Categories
Home > Documents > MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated...

MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated...

Date post: 27-Jul-2019
Category:
Upload: trinhkiet
View: 216 times
Download: 1 times
Share this document with a friend
12
MySQL Triggers By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and not for commercial use.
Transcript
Page 1: MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete. A SQL trigger is a special type

MySQL Triggers

By Prof. B.A.Khivsara

Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and not for commercial use.

Page 2: MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete. A SQL trigger is a special type

What is Trigger?

A SQL trigger is a set of SQL statements stored in the database catalog.

A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete.

A SQL trigger is a special type of stored procedure.

It is special because it is not called directly like a stored procedure.

The main difference between a trigger and a stored procedure is that a trigger is called automatically when a data modification event is made against a table whereas a stored procedure must be called explicitly.

Page 3: MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete. A SQL trigger is a special type

Advantages of triggers

SQL triggers provide an alternative way to check the integrity of data.

SQL triggers can catch errors in business logic in the database layer.

SQL triggers provide an alternative way to run scheduled tasks.

By using SQL triggers, you don’t have to wait to run the scheduled tasks because the triggers are invoked automatically before or after a change is made to the data in the tables.

SQL triggers are very useful to audit the changes of data in tables.

Page 4: MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete. A SQL trigger is a special type

Disadvantages of triggers

SQL triggers only can provide an extended validation and they cannot replace all the validations.

SQL triggers are invoked and executed invisible from the client applications, therefore, it is difficult to figure out what happen in the database layer.

SQL triggers may increase the overhead of the database server.

Page 5: MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete. A SQL trigger is a special type

MySQL Triggers types

BEFORE INSERT – activated before data is inserted into the table.

AFTER INSERT – activated after data is inserted into the table.

BEFORE UPDATE – activated before data in the table is updated.

AFTER UPDATE – activated after data in the table is updated.

BEFORE DELETE – activated before data is removed from the table.

AFTER DELETE – activated after data is removed from the table.

Page 6: MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete. A SQL trigger is a special type

Trigger Syntax

TRIGGER trigger_name

{ BEFORE | AFTER }

{ INSERT | UPDATE | DELETE }

ON tbl_name

FOR EACH ROW

trigger_body

Page 7: MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete. A SQL trigger is a special type

Trigger Example-1: Create two tables emp and emp_update as shown below

Create table emp(EmpNo INT, Lname VARCHAR(50), Salary int(5));

Create table emp_audit (EmpNo INT, Lname VARCHAR(50), changedat DATETIME, action VARCHAR(50));

Page 8: MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete. A SQL trigger is a special type

Trigger Example-1: Insert values in only emp table as shown below

Insert into emp values

(101, ‘John’,9000),

(102, ‘Pawar’,9000),

(103, ‘Jagruti’,7000);

Page 9: MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete. A SQL trigger is a special type

Trigger Example-1 write a trigger for before update on emp MySQL>DELIMITER //

MySQL>CREATE TRIGGER t1

AFTER UPDATE ON emp

FOR EACH ROW

BEGIN

INSERT INTO emp_audit

SET EmpNo = OLD. EmpNo,

Lname= OLD.Lname,

changedat = NOW()

action = 'update';

END //

MySQL>DELIMITER ;

MySQL>SHOW TRIGGERS; // To see information about triggers

Page 10: MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete. A SQL trigger is a special type

Trigger Example-1 Trigger will automatically run in background after update statement on emp

UPDATE emp SET Lname = ‘Pratik’ WHERE EmpNo = 101;

SELECT * FROM emp;

SELECT * FROM emp_audit;

Page 11: MySQL Triggers - WordPress.com · A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete. A SQL trigger is a special type

Assignment

Write a database trigger on Library table.

The System should keep track of the records that are being updated or deleted.

The old value of updated or deleted records should be added in Library_Audit table.


Recommended