Triggers in MySQL

10:57 AM Unknown 0 Comments


MySQL Triggers

In this section, you will learn how to work with the MySQL triggers. By definition, a trigger or database trigger is a stored program executed automatically to respond to a specific event e.g.,  insert, update or delete occurred in a table.
The database trigger is powerful tool for protecting the integrity of the data in your MySQL databases.(http://www.mysqltutorial.org/mysql-triggers.aspx) 

Create the database

After this little definition about triggers let's make something worthy.
problematic :We need to generate a profile for every new user added to data base.

1.Create the data base ,our database name is "test"

2.Create the triggers 


DROP TRIGGER IF EXISTS `mytrigger` ;

CREATE DEFINER = `root`@`localhost` TRIGGER `mytrigger` AFTER INSERT ON `users` 
FOR EACH
ROW BEGIN 
INSERT INTO profile( user_id, username, photo ) 
VALUES (
new.id, new.username, "/img/default.jpg"
);

END




How it works ?

All you need is watching this video that explain step by step how to create it .










Download the project

https://drive.google.com/file/d/0B0FucB-or3U5S18wZjRTOUZ0T3c/view?usp=sharing