0% found this document useful (0 votes)
16 views5 pages

Experiment No 10

Uploaded by

Anisha Cotta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Experiment No 10

Uploaded by

Anisha Cotta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No :10 Date: / / 2023

Title: Triggers

Aim: To study the implementation of MYSQL 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.

Advantages of using SQL 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 orafter a
change is made to the data in the tables.

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

Disadvantages of using SQL triggers

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.

SQL triggers may increase the overhead of the database server.


86
In MySQL, a trigger is a set of SQL statements that is invoked automatically when a change is made to
the data on the associated table. A trigger can be defined to be invoked either before or after the data is
changed by INSERT,UPDATE or DELETE statement

MySQL trigger syntax

In order to create a trigger you use the CREATE TRIGGER statement. The following illustrates the syntax
of the CREATE TRIGGER statement:

CREATE TRIGGER trigger_name trigger_time trigger_event

ON table_name

FOR EACH ROW

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

 a BEFORE INSERT trigger


 an AFTER INSERT trigger
 a BEFORE UPDATE trigger
 an AFTER UPDATE trigger
 a BEFORE DELETE trigger
 an AFTER DELETE trigger
MySQL AFTER INSERT trigger example
Consider the following AFTER INSERT trigger example.
Setting up a sample table

First, create a new table called members:

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.

Testing the MySQL AFTER INSERT trigger


First, insert two rows into the members table

Second, query data from the members table:

Third, query data from reminders table:

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

Conclusion: Studied & implemented MYSQL Triggers.

90

You might also like