Experiment No 10
Experiment No 10
Title: Triggers
Theory:
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.
It is important to understand SQL trigger’s advantages and disadvantages so that you can use it
appropriately. In the following sections, we will discuss about the advantages and disadvantages of using
SQL triggers.
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 orafter a
change is made to the data in the tables.
SQL triggers are very useful to audit the changes of data in tables.
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, you can validate user’s inputs
in the client side by using JavaScript or in the server side using server side scripting languages such as
JSP, PHP, ASP.NET, Perl, etc.
SQL triggers are invoked and executed invisibly from client-applications therefore it is difficult to figure
out what happen in the database layer.
In order to create a trigger you use the CREATE TRIGGER statement. The following illustrates the syntax
of the CREATE TRIGGER statement:
ON table_name
BEGIN
...
END
You put the trigger name after the CREATE TRIGGER statement. The trigger name should follow the
naming convention [trigger time]_[table name]_[trigger event] , for example
before_employees_update .
Trigger activation time can be BEFORE or AFTER . You must specify the activation time when you define
a trigger. You use BEFORE keyword if you want to process action prior to the change is made on the
table and AFTER if you need to process action after the change is made.
Trigger event can be INSERT , UPDATE or DELETE . This event causes trigger to be invoked. A trigger only
can be invoked by one event. To define a trigger that is invoked by multiple events, you have to define
multiple triggers, one for each event.
A trigger must be associated with a specific table. Without a table trigger would not exist therefore you
have to specify the table name after the ON keyword.
The SQL statements are placed between BEGIN and END block.
The OLD and NEW keywords are very handy. The OLD keyword refers to the existing record before you
change the data and the NEW keyword refers to the new row after you change the data.
87
Types of MySQL row level triggers
Second, create another table called reminders that stores reminder messages to members
88
Creating AFTER INSERT trigger example
The following statement creates an AFTER INSERT trigger that inserts a reminder into
the reminders table if the birth date of the member is NULL.
89
We inserted two rows into the members table. However, only the first row that has a birth date
value NULL, therefore, the trigger inserted only one row into the reminders table
90