MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
A trigger is a stored program invoked automatically in response to an event such as an INSERT
, UPDATE
, or DELETE
that occurs in the associated table. Triggers can be very useful for maintaining complex integrity constraints, for auditing changes, or for automatically updating derived column values.
Here is a basic guide on how to create a trigger in MySQL.
Syntax:
CREATE TRIGGER trigger_name trigger_time trigger_event ON table_name FOR EACH ROW BEGIN -- SQL statements END;
Where:
trigger_name
: The name of the trigger.trigger_time
: The time when the trigger is invoked. It can be BEFORE
or AFTER
.trigger_event
: The event that activates the trigger. It can be INSERT
, UPDATE
, or DELETE
.table_name
: The name of the table to which the trigger is associated.SQL statements
: The SQL code to be executed when the trigger is invoked.Example:
Suppose you have a table named orders
and you want to create a trigger that logs an entry into an orders_audit
table every time a row is inserted into the orders
table.
CREATE TRIGGER orders_after_insert AFTER INSERT ON orders FOR EACH ROW BEGIN INSERT INTO orders_audit(order_id, audit_message) VALUES (NEW.order_id, CONCAT('New order inserted with ID ', NEW.order_id)); END;
In this example, the trigger orders_after_insert
is created and set to be invoked AFTER INSERT
on the orders
table. The NEW
keyword refers to the new row being inserted into the orders
table. This trigger inserts a new row into the orders_audit
table with the order_id
of the new order and a message noting the insertion.
Notes:
FOR EACH ROW
clause is necessary because a trigger is activated for each row of the table that is affected by the triggering event.INSERT
, UPDATE
, or DELETE
operations.TRIGGER
privilege to create a trigger.BEGIN ... END
block is necessary when you have more than one statement in the trigger body. If you have only one statement, you can omit the BEGIN ... END
block.Creating triggers in MySQL:
-- Create a trigger in MySQL CREATE TRIGGER trigger_name BEFORE INSERT ON my_table FOR EACH ROW BEGIN -- Trigger logic END;
How to use CREATE TRIGGER in MySQL:
CREATE TRIGGER
statement in MySQL to define and create a new trigger.-- Create a trigger in MySQL CREATE TRIGGER trigger_name AFTER UPDATE ON my_table FOR EACH ROW BEGIN -- Trigger logic END;
MySQL trigger example:
-- Create a trigger to update timestamp on insert CREATE TRIGGER update_timestamp BEFORE INSERT ON my_table FOR EACH ROW SET NEW.created_at = NOW();
Defining triggers with MySQL:
CREATE TRIGGER
statement, specifying the trigger name, event (e.g., BEFORE INSERT
), and the trigger logic.-- Create a trigger in MySQL CREATE TRIGGER trigger_name AFTER DELETE ON my_table FOR EACH ROW BEGIN -- Trigger logic END;
Creating and managing triggers in MySQL:
CREATE TRIGGER
statement and altering existing triggers.-- Create a trigger in MySQL CREATE TRIGGER trigger_name BEFORE UPDATE ON my_table FOR EACH ROW BEGIN -- Trigger logic END; -- Alter a trigger in MySQL ALTER TRIGGER trigger_name AFTER DELETE ON my_table FOR EACH ROW BEGIN -- Updated trigger logic END;
Using parameters in MySQL triggers:
-- Create a trigger with parameters in MySQL CREATE TRIGGER trigger_name BEFORE INSERT ON my_table FOR EACH ROW BEGIN -- Trigger logic using NEW.parameter_name END;
Executing and testing MySQL triggers:
-- Test trigger by inserting a new row INSERT INTO my_table (column1, column2) VALUES ('value1', 'value2');