MySQL Triggers

MySQL Triggers

 MySQL Triggers



In MySQL, a trigger is a stored program invoked automatically in response to an event such as insertupdate, or delete that occurs in the associated table. For example, you can define a trigger that is invoked automatically before a new row is inserted into a table.

MySQL supports triggers that are invoked in response to the INSERTUPDATE or DELETE event.

The SQL standard defines two types of triggers: row-level triggers and statement-level triggers.

  • A row-level trigger is activated for each row that is inserted, updated, or deleted.  For example, if a table has 100 rows inserted, updated, or deleted, the trigger is automatically invoked 100 times for the 100 rows affected.
  • A statement-level trigger is executed once for each transaction regardless of how many rows are inserted, updated, or deleted.

MySQL supports only row-level triggers. It doesn’t support statement-level triggers.

Advantages of triggers

  • Triggers provide another way to check the integrity of data.
  • Triggers handle errors from the database layer.
  • Triggers give an alternative way to run scheduled tasks. By using triggers, you don’t have to wait for the scheduled events to run because the triggers are invoked automatically before or after a change is made to the data in a table.
  • Triggers can be useful for auditing the data changes in tables.

Disadvantages of triggers

  • Triggers can only provide extended validations, not all validations. For simple validations, you can use the NOT NULLUNIQUECHECK and FOREIGN KEY constraints.
  • Triggers can be difficult to troubleshoot because they execute automatically in the database, which may not invisible to the client applications.
  • Triggers may increase the overhead of the MySQL Server.

Managing MySQL triggers

  • Show triggers – list triggers in a database, table by specific patterns.
Reactions

Post a Comment

0 Comments

close