The aim of this tutorial is to provide an introduction to SQL Triggers, which are specialized routines invoked or performed automatically when a specific event such as insert, update, or delete occurs.
By the end of this tutorial, you will understand what SQL Triggers are, their different types, how to create them, and how to use them in your database management projects.
Basic knowledge of SQL language and familiarization with relational database concepts is required.
SQL Triggers are stored procedures which are invoked automatically in response to certain events like Insert, Update, or Delete in a table. They are useful for maintaining the integrity of the data in database tables.
There are two types of triggers:
- Row Level Trigger: This trigger is fired whenever a row gets affected.
- Statement Level Trigger: This trigger is fired once for each SQL statement.
Triggers are also classified into three types based on the event:
- Insert Trigger
- Update Trigger
- Delete Trigger
To create a trigger, you use the CREATE TRIGGER statement with the following syntax:
CREATE TRIGGER trigger_name
trigger_time trigger_event
ON table_name
FOR EACH ROW
trigger_body;
trigger_name is the name of the trigger.trigger_time is the time at which the trigger gets invoked. It may be BEFORE or AFTER.trigger_event is the event that invokes the trigger. It may be INSERT, UPDATE, or DELETE.table_name is the name of the table associated with the trigger.trigger_body is the block of code to be executed when the trigger is fired.Let's create a trigger that will insert a record into a log_table each time a row is inserted into the orders_table.
CREATE TRIGGER orders_after_insert
AFTER INSERT
ON orders_table
FOR EACH ROW
INSERT INTO log_table(order_id, log_message)
VALUES (New.order_id, 'A new order has been created.');
orders_after_insert is the name of the trigger.AFTER INSERT specifies that this trigger will fire after the insertion of a new row in orders_table.ON orders_table indicates that the trigger is associated with the orders_table.FOR EACH ROW means the trigger will fire for each row being inserted.INSERT INTO log_table(order_id, log_message) VALUES (New.order_id, 'A new order has been created.') is the action performed when the trigger is fired.To delete a trigger, you use the DROP TRIGGER statement:
DROP TRIGGER orders_after_insert;
This will delete the orders_after_insert trigger.
In this tutorial, we have covered:
To continue learning about SQL Triggers, you could explore using BEFORE triggers and learn about UPDATE and DELETE triggers.
Try the following exercises to practice your SQL trigger skills:
orders_table into the log_table. orders_table.Here are the solutions for the above exercises:
CREATE TRIGGER orders_before_delete
BEFORE DELETE
ON orders_table
FOR EACH ROW
INSERT INTO log_table(order_id, log_message)
VALUES (Old.order_id, 'An order has been deleted.');
CREATE TRIGGER orders_prevent_delete
BEFORE DELETE
ON orders_table
FOR EACH ROW
BEGIN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'You cannot delete orders.';
END;
Remember, practice makes perfect. Try creating different types of triggers on your own.