Introduction to SQL Triggers

Tutorial 1 of 5

Introduction to SQL Triggers

1. Introduction

Goal of the Tutorial

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.

Learning Outcomes

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.

Prerequisites

Basic knowledge of SQL language and familiarization with relational database concepts is required.

2. Step-by-Step Guide

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

Creating a 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.

Best Practices and Tips

  1. Use triggers to ensure data integrity rules that cannot be enforced using constraints.
  2. Keep your trigger logic as simple as possible.
  3. Avoid using a trigger to replace or duplicate the functionality available through constraints.

3. Code Examples

Example 1: Creating a Trigger

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.

Example 2: Deleting a Trigger

To delete a trigger, you use the DROP TRIGGER statement:

DROP TRIGGER orders_after_insert;

This will delete the orders_after_insert trigger.

4. Summary

In this tutorial, we have covered:

  • What SQL Triggers are and the types of triggers.
  • How to create and delete a trigger.
  • Best practices when working with triggers.

To continue learning about SQL Triggers, you could explore using BEFORE triggers and learn about UPDATE and DELETE triggers.

5. Practice Exercises

Try the following exercises to practice your SQL trigger skills:

  1. Exercise 1: Create a trigger that logs every deletion from the orders_table into the log_table.
  2. Exercise 2: Create a trigger that prevents the deletion of records from the orders_table.

Here are the solutions for the above exercises:

  1. Solution 1:
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.');
  1. Solution 2:
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.