MySQL Trigger Example

MySQL Trigger Example



A  trigger is an SQL statements or a set of SQL statements which is stored to be activated or fired when an event associating with a database table occurs. The event can be any event including INSERT, UPDATE  and DELETE.


DELIMITER &&

CREATE TRIGGER test_trigger BEFORE INSERT ON clubs
FOR EACH ROW

BEGIN
INSERT INTO user(username) Value ('Mark');
END &&

DELIMITER ;


Sometimes a trigger is referred as a special kind of stored procedure in term of procedural code inside its body. The difference between a trigger and a stored procedure is that a trigger is activated or called when an event happens in a database table, a stored procedure must be called explicitly. For example you can have some business logic to do before or after inserting a new record in a database table.
Advantages of using atrigger


  • A SQL Trigger provides an alternative way to check integrity.
  • A SQL trigger can catch the errors in business logic in the database level.
  • A SQL trigger provides an alternative way to run scheduled tasks. With SQL trigger, you don’t have to wait to run the scheduled tasks. You can handle those tasks before or after changes being made to database tables.
  • A SQL trigger is very useful when you use it to audit the changes of data in a database table.


Disadvantages of using a trigger


  • A SQL trigger only can provide extended validation and cannot replace all the validations. Some simple validations can be done in the application level.  For example, you can validate input check in the client side by using javascript or in the server side by server script using PHP or ASP.NET.
  • A SQL Triggers executes invisibly from client-application which connects to the database server so it is difficult to figure out what happen underlying database layer.
  • A SQL Triggers run every updates made to the table therefore it adds workload to the database and cause system runs slower.


MySQL finally supports one of the most important features of an enterprise database server which is called trigger since version 5.0.2. Trigger is implemented in MySQL by following the syntax of standard SQL:2003. When you create a trigger in MySQL, its definition stores in the file with extension .TRG in a database folder with specific name as follows: /data_folder/database_name/table_name.trg

The file is in plain text format so you can use any plain text editor to modify it.

While trigger is implemented in MySQL has all features in standard SQL but there are some restrictions you should be aware of like following:


  1. It is not allowed to call a stored procedure in a trigger.
  2. It is not allowed to create a trigger for views or temporary table.
  3. It is not allowed to use transaction in a trigger.
  4. Return statement is disallowed in a trigger.


Creating a trigger for a database table causes the query cache invalidated. Query cache allows you to store the result of query and corresponding select statement. In the next time, when the same select statement comes to the database server, the database server will use the result which stored in the memory instead of parsing and executing the query again.

All trigger for a database table must have unique name. It is allowed that triggers for different tables having the same name but it is recommended that trigger should have unique name in a specific database.

No comments:

Post a Comment