MySQL - CREATE TRIGGER Statement

Triggers in MySQL are stored programs similar to procedures. These can be created on a table, schema, view and database that are associated with an event and whenever an event occurs the respective trigger is invoked.
Triggers are, in fact, written to be executed in response to any of the following events −
A database manipulation (DML) statement (DELETE, INSERT, or UPDATE)
A database definition (DDL) statement (CREATE, ALTER, or DROP).
A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).
You can create a trigger using the CREATE TRIGGER Statement.
Following is the syntax of the MySQL CREATE TRIGGER Statement.
CREATE TRIGGER trigger_name
trigger_time trigger_event
ON table_name FOR EACH ROW
BEGIN
...
END;

Where, Trigger_name is the name of the trigger you need to create, Trigger_time is the time of trigger activation, Trigger_event can be INSERT, UPDATE, or DELETE. This event causes the trigger to be invoked, Table_name is the name of the table to which the trigger is associated with.
Assume we have created a table with name student as shown below −
mysql> Create table Student(Name Varchar(35), age INT, Score INT);
Query OK, 0 rows affected (1.81 sec)

Following query creates a trigger, this will set the score value 0 if you enter a value that is less than 0 as score.
mysql> DELIMITER //
mysql> Create Trigger sample_trigger BEFORE INSERT ON student FOR EACH ROW
BEGIN
IF NEW.score < 0 THEN SET NEW.score = 0;
END IF; MySQL CREATE TRIGGER Statement
END //
Mysql> DELIMITER ;
Query OK, 0 rows affected (0.44 sec)
If you try to insert records in the student table and if you use a value that is less than 0 as age it will be automatically set to 0.
mysql> INSERT INTO student values ('Jeevan', 22, 8);
mysql> INSERT INTO student values ('Raghav', 26, –3);
mysql> INSERT INTO student values ('Khaleel', 21, –9);
mysql> INSERT INTO student values ('Deva', 30, 9);
If you verify the contents of the student table you can observe that no negative values inserted under the column score −
mysql> SELECT * from student;
+---------+------+-------+
| Name    | age  | Score |
+---------+------+-------+
| Jeevan  | 22   | 8     |
| Raghav  | 26   | 0     |
| Khaleel | 21   | 0     |
| Deva    | 30   | 9     |
+---------+------+-------+
5 rows in set (0.00 sec)
Following is an example query to create a trigger using the after clause −
mysql> CREATE TRIGGER testTrigger
   –> AFTER UPDATE ON Student
   –> FOR EACH ROW
   –> INSERT INTO Student
   –> SET action = 'update',
   –> Name = OLD.Name,
   –> age = OLD.age,
   –> score = OLD.score;
Query OK, 0 rows affected (0.14 sec)
If you verify the contents of the student table you can observe that no negative values inserted under the column score −
mysql> SELECT * FROM Student;
+---------+------+-------+
| Name    | age  | Score |
+---------+------+-------+
| Jeevan  | 22   | 8     |
| Jeevan  | 22   | 8     |
| Raghav  | 22   | 0     |
| Khaleel | 22   | 0     |
| Deva    | 22   | 9     |
+---------+------+-------+
5 rows in set (0.08 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page