mysql trigger MySQL Tutorial: Basic Of Trigger In MySQLSome time ago I’ve discussed the MySQL tutorial are Displaying Data from Multiple Tables. Now I’m trying to continue learning about MySQL. The lesson this time I try to discuss the Basic Of Trigger In MySQL.

What is Trigger? Trigger is a set of commands or syntax that will automatically run if a certain operation occurs in table.

Trigger, For What? Trigger used to call one or more SQL commands automatically before or after the process INSERT, UPDATE or DELETE from a table.

Trigger is often used, among other things:

  • Performing data update automatically if changes occur. Examples are in the sales system, if new items included the stock will increase automatically.
  • Trigger can be used to implement a system log. Every place changes, it automatically saves the log table.
  • Trigger can be used to perform validation and verification of data before the data is stored.

How To Create a New Trigger?

Here is the general form of the command to make the triggers:

CREATE TRIGGER name
[BEFORE|AFTER] [INSERT|UPDATE|DELETE]
ON tablename
FOR EACH ROW statement
  • Name, Name of trigger the variable naming rules / identifier in MySQL
  • [BEFORE | AFTER] is used to determine when the process will be executed automatically, before or after the process.
  • [INSERT | UPDATE | DELETE] is used to determine the event (process) is used as a trigger to execute commands on the trigger.
  • Tablename is the table name where the trigger is located.
  • Statement, is a set of commands or queries that will automatically run if the event / process previously defined active.

Statement or command in a trigger can be one commandment, and can also be several orders at once. If there are several commands in trigger, then use BEGIN and END command to initiate and end the command.

In the statement trigger, we can access the records table before or after the process of using the NEW and OLD. NEW is used to retrieve records that will be processed (insert or update), while OLD is used to access records that have been processed (update or delete).

How To DELETE Trigger?

To delete a trigger, can use the DROP TRIGGER command followed by a tablename and trigger name. Here is the general form of the command to remove trigger.

ROP TRIGGER tablename.triggername;

OK, enough lessons about the Basic Of Trigger In MySQL, hopefully useful. if have any question put your comment below icon wink MySQL Tutorial: Basic Of Trigger In MySQL

GHTime Code(s): nc 

Incoming search terms for the article:

Related Post:

Tagged with:

Filed under: MySQL

Like this post? Subscribe to my RSS feed and get loads more!