How to Create Triggers in phpMyAdmin

In this tutorial I will show you how to create MySQL triggers in phpMyAdmin.

Creating Triggers in MySQL

In order to create a trigger in MySQL we can follow the recommendations that we can found in http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html. There, they explain us perfectly how we should create our trigger and they even provide some examples.

Here’s a sample code.

CREATE TRIGGER ins_student AFTER INSERT ON user
FOR EACH ROW
BEGIN
IF NEW.type = 'student' THEN
    INSERT INTO student (`id`) VALUES (NEW.id);
END IF;
END;

Here’s the explanation:
ins_student is the name of the trigger
– it is executed after the insert action on user table
– this trigger will fire on each insertion of a new row
– block which asserts if the type of the new record is equal to the literal ‘student’, then the new id is inserted in the student table.

Creating the Trigger through phpMyAdmin

If we try to put this code in a SQL statement in phpMyAdmin and run it as it is, it will give us some problems with separators from phpMyAdmin itself. You can go to the Triggers tab in phpMyAdmin and click on Add Trigger button to add a new trigger. The screenshot below shows the dialog box that appears on clicking the button.

Creating triggers through phpMyAdmin

Here’s the explanation:
– trigger name is add_menu_items_trigger
– it is triggered after INSERT action on Menu_Items table
– the definition defines the SQL query to be executed

This content has been helpful to you?

Thanks for contributing!

Yes No