Triggers in MySQL

Jan 8, 2012 by

Triggers in MySQL

Almost all developers are heard about Triggers and all knows that mysql support triggers and triggers are adding an advantages to mysql.Triggers are the SQL statements are stored in database.

Triggers are the SQL statements which add functionality to your tables so that they perform a certain series of actions when a some queries are executed. We can say in easy language is Triggers are some conditions performed when INSERT, UPDATE or DELETE events are made in the table without using two separate queries.

Sometimes developers are prefer to use store procedures rather than triggers but triggers are one kind of store procedures which contain procedural code into body.The difference between a trigger and a stored procedure is that a trigger is called when an event occurs in a table whereas a stored procedure must be called explicitly.

SYNTAX:

PHP
1
2
3
4
CREATE TRIGGER trigger_name trigger_time
trigger_event ON table_name
FOR EACH ROW
trigger_body

CREATE TRIGGER statement is used to create triggers.Trigger action time can be BEFORE or AFTER.You can use BEFORE when you want to process action before changes are made in the table and AFTER if you need to process action after changes are made in the table.trigger_body is the statement to execute when the trigger activates

Let’s start creating the trigger in MySQL with simple example.In the database, we have table customers as follows

PHP
1
2
3
4
5
6
CREATE TABLE <code>customers</code> (
        <code>customer_id</code> int(11) NOT NULL,
        <code>lastName</code> varchar(50) NOT NULL,
        <code>firstName</code> varchar(50) NOT NULL,
      PRIMARY KEY (<code>customer_id</code>)
)

Now let’s create relational table for customer.you need to create a new table called customer_info_rel.

PHP
1
2
3
4
5
6
CREATE TABLE customer_info_rel (
id int(11) NOT NULL AUTO_INCREMENT,
customer_id int(11) NOT NULL,
email varchar(50) NOT NULL,
PRIMARY KEY (id)
)

So now i am going to apply trigger on basis of relational table.If i delete user or customer from main table customers then triggers are occurs which delete record from relational table customer_info_rel also.

PHP
1
2
3
4
5
6
7
8
DELIMITER $$
CREATE TRIGGER trigger_customer BEFORE
DELETE ON customers
FOR EACH ROW
BEGIN
DELETE FROM customer_info_rel WHERE customer_id = OLD.id;
END$$
DELIMITER ;

NOTE: For Trigger you need to use DELIMITER $$ before the trigger and DELIMITER ; after the trigger.

ADVANTAGES:

  1. Triggers are the alternate way for data integrity.
  2. Trigger is useful when you want to audit the changes of data in a database table.

DISADVANTAGES:
(Some points Which are not supported in Trigger)

  1. Transaction isn’t allow in trigger.
  2. We can’t use return statement in trigger.
  3. Can’t call store procedure with trigger.
  4. Can’t use trigger with views.

Next we will check for stored procedures

Related Posts

Share This

1 Comment

Trackbacks/Pingbacks

  1. Triggers in MySQL | Database | Syngu - [...] A Trigger is a MySQL command that is triggered when a specific set of conditions are met. For example ...

Leave a Comment

Top of Page