database automation with mysql triggers and event schedulers

42
Seminar on Database Automation with MySQL Triggers and Event Schedulers Abdul Rahman Sherzad Lecturer at Computer Science faculty of Herat University, Afghanistan PhD Student at TU-Berlin, Germany January 27, 2015

Upload: abdul-rahman-sherzad

Post on 08-Feb-2017

1.201 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Database Automation with MySQL Triggers and Event Schedulers

S e m i n a r o n D a t a b a s e A u t o m a t i o n

w i t h

M y S Q L T r i g g e r s

a n d

E v e n t S c h e d u l e r s

A b d u l R a h m a n S h e r z a d

L e c t u r e r a t C o m p u t e r S c i e n c e f a c u l t y o f H e r a t U n i v e r s i t y , A f g h a n i s t a n

P h D S t u d e n t a t T U - B e r l i n , G e r m a n y

J a n u a r y 2 7 , 2 0 1 5

Page 2: Database Automation with MySQL Triggers and Event Schedulers

MySQL Triggers and Events• MySQL Triggers are operations that are automatically performed when a

specified database event (INSERT, UPDATE, DELETE) occurs.

• Ideally, triggers should be considered when they automate changes which are

specific to the database or management of its data.

– Data integrity, logging and auditing, business logic, perform calculations, run

further SQL commands, etc.

• MySQL Events can be scheduled to run once or more during specific periods.

Effectively, they are database-only cron-jobs and can be used in many cases

such as:

– optimizing database tables, archiving data, cleaning up logs, or generate complex

reports during off-peak time.2

Page 3: Database Automation with MySQL Triggers and Event Schedulers

M o r e a b o u t M y S Q L T r i g g e r s

3

Page 4: Database Automation with MySQL Triggers and Event Schedulers

Introduction to MySQL Triggers

• A trigger is a set of SQL statements that is invoked or fired automatically when a

change is made to the data on the associated table either before or after the data

is changed by DML statements (INSERT, UPDATE or DELETE).

• MySQL version 5.7.2 allows you to define maximum six triggers for each table.

– BEFORE INSERT

– AFTER INSERT

– BEFORE UPDATE

– AFTER UPDATE

– BEFORE DELETE

– AFTER DELETE

MySQL 5.7.2+ removes this limitation and allows creation of multiple triggers for

the same event and action time in a table.4

Page 5: Database Automation with MySQL Triggers and Event Schedulers

Introduction to MySQL Triggers II• The trigger is not invoked with statement that makes change to the table but does

not use INSERT, DELETE or UPDATE statement.

– For example, the TRUNCATE statement removes the whole data of a table but does not

invoke the trigger associated with that table.

• The triggers are invoked with statements that use the INSERT statement behind

the scenes.

– For example, REPLACE statement and LOAD DATA statement.

• Database triggers are special types of stored procedures.

– Special because they are not called explicitly like stored procedures.

• Triggers are supported in MySQL, PostgreSQL, SQLite, Firebird, DB2, Microsoft

SQL Server and Oracle.

– Implementation varies, therefore, the appropriate documentation before coding should

be checked.5

Page 6: Database Automation with MySQL Triggers and Event Schedulers

MySQL Triggers

Advantages

• SQL triggers provide an alternative

way to check the integrity of data.

• SQL triggers provide an alternative

way to run scheduled tasks.

– By using SQL triggers, no need to

wait to run the scheduled tasks

because the triggers are

invoked automatically before or

after a change is made to the data in

tables.

• SQL triggers are very useful to log

and audit the changes of data in

tables.

Disadvantages

• SQL triggers only can provide an extended

validation and they cannot replace all the

validations. Some simple validations have to be

done in the application layer. For example, user's

inputs can be validated on the client side or server

side.

• SQL triggers are invoked and executed invisibly

from 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.

6

Page 7: Database Automation with MySQL Triggers and Event Schedulers

Caution!!!

• Triggers should not be used in place of foreign key constraints.

• Triggers are not a substitute for transactions.

• Avoid duplication of effort.

– The back-end code should be sanitizing user input so it should not be necessary

to repeat those checks within the database.

• Triggers will execute quicker than a series of SQL commands passed by

back-end code

– but using complex triggers on regularly modified tables should be avoided.

7

Page 8: Database Automation with MySQL Triggers and Event Schedulers

MySQL Trigger Limitation

• MySQL triggers cover all features defined in the standard SQL. However,

triggers cannot use:

– SHOW, LOAD DATA, LOAD TABLE, BACKUP DATABASE, RESTORE, FLUSH and

RETURN statements.

– Statements that commit or rollback implicitly or explicitly such as COMMIT,

ROLLBACK, START TRANSACTION, LOCK / UNLOCK TABLES, ALTER, CREATE, DROP,

RENAME, etc.

– Prepared statements such as PREPARE, EXECUTE, etc.

– Dynamic SQL statements.

MySQL version 5.1.4+ a trigger can call a stored procedure or stored function, which

was a limitation is the previous versions.

8

Page 9: Database Automation with MySQL Triggers and Event Schedulers

MySQL Trigger Storage and Backup

• MySQL stores triggers in a data directory e.g.

/data/database_name/ with the files named table_name.TRG

and trigger_name.TRN

– The table_name.TRG file maps the trigger to the corresponding table.

– the trigger_name.TRN file contains the trigger definition.

• To back up the MySQL triggers

– Copy the trigger files to the backup directory.

– Use the mysqldump tool.

9

Page 10: Database Automation with MySQL Triggers and Event Schedulers

Create MySQL Trigger Basic Syntax

10

CREATE TRIGGER trigger_name trigger_time trigger_event

ON table_name

FOR EACH ROW

BEGIN

-- the trigger body

-- this code is applied to every

-- inserted/updated/deleted row

END;

Page 11: Database Automation with MySQL Triggers and Event Schedulers

Create MySQL Trigger Syntax Details

• The trigger name comes after the CREATE TRIGGER statement.

– The trigger name should follow the naming convention [table name]_[trigger

time]_ [trigger event].

• Trigger activation time can be BEFORE or AFTER.

• The trigger event can be INSERT, UPDATE or DELETE.

• The table name must specify after the ON keyword.

– A trigger must be associated with a specific table. Without a table trigger would not

exist.

• The trigger body is a set of SQL commands to run and are placed between

BEGIN and END block. This is where the logic for the trigger is defined.

11

Page 12: Database Automation with MySQL Triggers and Event Schedulers

Notice!

• In the trigger defined for INSERT

– NEW keyword can be used only.

– OLD keyword can not be used.

• In the trigger defined for DELETE

– there is no new row. Therefore, the OLD keyword can be used only.

• In the UPDATE trigger

– OLD keyword refers to the row before it is updated.

– NEW keyword refers to the row after it is updated.

12

Page 13: Database Automation with MySQL Triggers and Event Schedulers

M y S Q L T r i g g e r A p p l i c a t i o n S c e n a r i o

13

Page 14: Database Automation with MySQL Triggers and Event Schedulers

MySQL Trigger Application Scenario

• A small example database with two tables as follow:

– Blog: stores a unique ID, the title, content, and a deleted flag.

– Audit: stores a basic set of historical changes with an ID, the blog ID, the old blog

title, the change type (NEW, EDIT or DELETE), and the date/time of that change.

• INSERT into the blog table

– A new entry into the audit table containing the blog ID, the title, and a type of 'NEW'

(or 'DELETE' if it was deleted immediately) should be added.

• UPDATE in the blog table

– A new entry into the audit table containing the blog ID, the title, and a type of 'EDIT'

(or 'DELETE' if the deleted flag is set) should be added.

The 'changetime' attribute will automatically be set to the current time.

14

Page 15: Database Automation with MySQL Triggers and Event Schedulers

Database Schema

15

Page 16: Database Automation with MySQL Triggers and Event Schedulers

Blog Schema

CREATE TABLE blog (

id INT NOT NULL AUTO_INCREMENT,

title VARCHAR(255),

content text,

deleted TINYINT(1) unsigned NOT NULL DEFAULT '0',

PRIMARY KEY (id),

KEY (deleted)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

16

Page 17: Database Automation with MySQL Triggers and Event Schedulers

Audit Schema

CREATE TABLE audit (

id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,

blog_id INT NOT NULL,

blog_title VARCHAR(255) NOT NULL,

changetype ENUM('NEW','EDIT','DELETE') NOT NULL,

changetime timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

KEY (blog_id),

CONSTRAINT FOREIGN KEY (blog_id) REFERENCES blog (id)

ON DELETE CASCADE ON UPDATE CASCADE

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

17

Page 18: Database Automation with MySQL Triggers and Event Schedulers

Notice: Delimiter

• Most of the time the trigger body requires a number of SQL

commands separated by a semi-colon (;).

• To create the full trigger code the delimiter must be changed

to something else. For example

– $$

– //

• Finally, after trigger code completion the delimiter back should

be set to a semi-colon.

18

Page 19: Database Automation with MySQL Triggers and Event Schedulers

Trigger: AFTER INSERT

DELIMITER $$

CREATE TRIGGER blog_after_insert AFTER INSERT

ON blog

FOR EACH ROW

BEGIN

IF NEW.deleted THEN

SET @changetype = 'DELETE';

ELSE

SET @changetype = 'NEW';

END IF;

INSERT INTO audit (blog_id, blog_title, changetype) VALUES (NEW.id, NEW.title, @changetype);

END; $$

DELIMITER ;

19

Page 20: Database Automation with MySQL Triggers and Event Schedulers

Trigger: AFTER UPDATE

DELIMITER $$

CREATE TRIGGER blog_after_update AFTER UPDATE

ON blog

FOR EACH ROW

BEGIN

IF NEW.deleted THEN

SET @changetype = 'DELETE';

ELSE

SET @changetype = 'EDIT';

END IF;

INSERT INTO audit (blog_id, blog_title, changetype) VALUES (NEW.id, OLD.title, @changetype);

END; $$

DELIMITER ;

20

Page 21: Database Automation with MySQL Triggers and Event Schedulers

Test the Triggers

• Let's see what happens when we insert a new post into our blog table:

INSERT INTO blog (title, content) VALUES ('First blog', 'The first blog content');

SELECT * FROM blog;

SELECT * FROM audit;

• Let's update our blog text:

UPDATE blog SET title = 'First blog Edited' WHERE id = 1;

SELECT * FROM audit;

• Finally, let's mark the post as deleted:

UPDATE blog SET deleted = 1 WHERE id = 1;

SELECT * FROM audit;

21

Page 22: Database Automation with MySQL Triggers and Event Schedulers

M o r e a b o u t M y S Q L E v e n t s

22

Page 23: Database Automation with MySQL Triggers and Event Schedulers

Introduction to MySQL Events

• MySQL events were added in MySQL 5.1.6 and offer an alternative to

scheduled tasks and cron jobs.

• Events can be used to create backups, delete out-of-date and old records,

aggregate data for reports, etc.

• Unlike standard triggers which execute given a certain condition, an event

is an object that is triggered by the passage of time and is sometimes

referred to as a 'temporal trigger'.

• MySQL events can be scheduled to run either once or at a recurring interval

– when the server traffic will be low.

23

Page 24: Database Automation with MySQL Triggers and Event Schedulers

Starting the Event Scheduler

• The MySQL event scheduler is a process that runs in the

background and constantly looks for events to execute.

• Before creating or scheduling an event, first the scheduler

needs to turn on, with issuing the following command:

SET GLOBAL event_scheduler = ON;

• With the following the scheduler status is revealed if it is ON or

OFF:

SHOW PROCESSLIST;

24

Page 25: Database Automation with MySQL Triggers and Event Schedulers

Create MySQL Event Basic Syntax

CREATE EVENT event_name

ON SCHEDULE the_schedule

[ON COMPLETION [NOT] PRESERVE]

[ENABLE | DISABLE | DISABLE ON SLAVE]

DO BEGIN

-- the event body

END;

25

Page 26: Database Automation with MySQL Triggers and Event Schedulers

The Schedule Settings

• Run once on a specific date/time:

– AT 'YYYY-MM-DD HH:MM.SS'

• AT '2016-01-15 02:00.00'

• Run once after a specific period has elapsed:

– AT CURRENT_TIMESTAMP + INTERVAL n [HOUR|MONTH|WEEK|DAY|MINUTE]

• AT CURRENT_TIMESTAMP + INTERVAL 1 DAY

• Run at specific intervals forever:

– EVERY n [HOUR|MONTH|WEEK|DAY|MINUTE]

• EVERY 1 DAY

• Run at specific intervals during a specific period:

– EVERY n [HOUR|MONTH|WEEK|DAY|MINUTE] STARTS date ENDS date

• EVERY 1 DAY STARTS CURRENT_TIMESTAMP + INTERVAL 1 WEEK ENDS '2017-01-01 00:00.00'

26

Page 27: Database Automation with MySQL Triggers and Event Schedulers

Caution!!!

• An event is normally dropped once its schedule has expired

– ON COMPLETION NOT PRESERVE

• To prevent that behavior set

– ON COMPLETION PRESERVE

27

Page 28: Database Automation with MySQL Triggers and Event Schedulers

Run Once Event Example

DELIMITER $$

CREATE EVENT one_time_event_example

ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 MINUTE

DO BEGIN

UPDATE blog SET title = "One time event example" WHERE id = 1;

END; $$

DELIMITER ;

• This event will run once, one hour from the time it was created.

28

Page 29: Database Automation with MySQL Triggers and Event Schedulers

Preserved Event Example

DELIMITER $$

CREATE EVENT preserved_event_example

ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR

ON COMPLETION PRESERVE

DO BEGIN

UPDATE blog SET title = "Preserve event example" WHERE id = 1;

END; $$

DELIMITER ;

• To specify a preserved event, the ON COMPLETION PRESERVE clause

must be used.

29

Page 30: Database Automation with MySQL Triggers and Event Schedulers

Reoccurring Event Example

DELIMITER $$

CREATE EVENT reoccurring_event_example

ON SCHEDULE EVERY 1 HOUR

DO BEGIN

UPDATE blog SET title = "Reoccurring event example" WHERE id = 1;

END; $$

DELIMITER ;

• To specify a recurring event, the EVERY clause must be used.

• In this example, the reoccurring event would start tomorrow and continue

to run every hour for a full year.

30

Page 31: Database Automation with MySQL Triggers and Event Schedulers

M y S Q L E v e n t A p p l i c a t i o n S c e n a r i o

31

Page 32: Database Automation with MySQL Triggers and Event Schedulers

MySQL Event Application Scenario

• The database example used for trigger application has a problem.

– The old blog posts are marked as deleted rather than being removed from the blog

table.

– The blog table will grow indefinitely and become slower over time.

– The old blog posts could be purged, but that would remove them forever.

• As a result, we will move the blog posts and their associated audit records to

archive tables.

– The archive tables can grow without affecting the speed of the main web application and

we can undelete the old blog posts if necessary.

• Two archive tables are required:

– blog_archive: identical to the blog table except it does not require a deleted flag or an

auto-incrementing ID.

– audit_archive: identical to the audit table except the timestamp is not automatically

generated and it does not require an auto-incrementing ID.

32

Page 33: Database Automation with MySQL Triggers and Event Schedulers

MySQL Event Application Scenario II

• A scheduled event is required to:

– Copies blog posts from blog to blog_archive when the deleted flag is set

to 1.

– Copies the associated audit entries for those blog posts from audit to

audit_archive.

– Physically deletes the archived blog posts from the blog table.

– Referential integrity has been defined with a foreign key. Therefore, all

the associated audit entries for those blog posts will also be removed.

33

Page 34: Database Automation with MySQL Triggers and Event Schedulers

Database Schema

34

Page 35: Database Automation with MySQL Triggers and Event Schedulers

Blog Archive Schema

CREATE TABLE blog_archive (

id INT NOT NULL,

title VARCHAR(255),

content text,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

35

Page 36: Database Automation with MySQL Triggers and Event Schedulers

Audit Archive Schema

CREATE TABLE audit_archive (

id INT PRIMARY KEY NOT NULL,

blog_id INT NOT NULL,

blog_title VARCHAR(255),

changetype ENUM('NEW','EDIT','DELETE') NOT NULL,

changetime timestamp NOT NULL,

KEY (blog_id),

CONSTRAINT FOREIGN KEY (blog_id) REFERENCES blog_archive (id)

ON DELETE CASCADE ON UPDATE CASCADE

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

36

Page 37: Database Automation with MySQL Triggers and Event Schedulers

The Archive Event

DELIMITER $$

CREATE EVENT archive_blog_posts

ON SCHEDULE EVERY 1 WEEK STARTS '2016-01-22 01:00:00'

DO BEGIN

-- copy deleted posts

INSERT INTO blog_archive (id, title, content) SELECT id, title, content FROM blog

WHERE deleted = 1;

-- copy associated audit records

INSERT INTO audit_archive (id, blog_id, blog_title, changetype, changetime)

SELECT audit.id, audit.blog_id, audit.blog_title, audit.changetype, audit.changetime

FROM audit JOIN blog ON audit.blog_id = blog.id WHERE blog.deleted = 1;

-- remove deleted blogs and audit entries

DELETE FROM blog WHERE deleted = 1;

END; $$

DELIMITER ;

• In the example, the event runs every week on Friday morning.37

Page 38: Database Automation with MySQL Triggers and Event Schedulers

Wrap up

• The Triggers are powerful tools for protecting the integrity of

the data in the databases

– Data integrity, logging and auditing, business logic, perform

calculations, run further SQL commands, etc.

• The Events are very useful to automate some database

operations

– optimizing database tables, cleaning up logs, archiving data, or

generate complex reports during off-peak time.

38

Page 39: Database Automation with MySQL Triggers and Event Schedulers

Abdul Rahman

Sherzad

• Thank you for your participation

– Herat University

– Hariwa Institute of Higher Education

– Ghalib University

– Khaja Abdullah Ansari Institute of

Higher Education

– ICT Companies

Lecturer at Computer Science

faculty of Herat University,

Afghanistan.

PhD Student at Technical

University of Berlin (TU-Berlin),

Germany.

January 27, 2015

39

Page 40: Database Automation with MySQL Triggers and Event Schedulers

40

Page 41: Database Automation with MySQL Triggers and Event Schedulers

41

Page 42: Database Automation with MySQL Triggers and Event Schedulers

42