Using Triggers for Data Automation

Tutorial 3 of 5

Using Triggers for Data Automation

Introduction

This tutorial aims to guide you on how to utilize SQL triggers to automate data changes in your database. You will learn how to create triggers that auto-execute operations when data is inserted, updated, or deleted.

By the end of this tutorial, you will:
- Understand what SQL triggers are and how they work
- Learn how to create, modify, and delete triggers
- Have a solid grasp of how to use triggers for data automation

Prerequisite: Basic knowledge of SQL, including how to create tables, and insert, update and delete records.

Step-by-Step Guide

Triggers are special stored procedures that are defined to execute automatically in response to certain events in a database. They can be defined to run instead of or after INSERT, UPDATE, and DELETE events.

SQL syntax for creating a trigger:

CREATE TRIGGER trigger_name
trigger_time trigger_event
ON table_name FOR EACH ROW
trigger_body;
  • trigger_name: Name for the trigger.
  • trigger_time: Specifies when to trigger: BEFORE or AFTER.
  • trigger_event: The event that activates the trigger: INSERT, UPDATE, or DELETE.
  • table_name: The table associated with the trigger.
  • trigger_body: The SQL code to be executed when the trigger is activated.

Best practices:
- Keep trigger operations as small as possible to avoid slowing down the system.
- Avoid creating recursive triggers. They can lead to an infinite loop and result in system crashes.

Code Examples

Let's create a trigger that logs every deletion from the 'employees' table.

CREATE TRIGGER before_employee_delete
BEFORE DELETE ON employees
FOR EACH ROW
BEGIN
   INSERT INTO employee_audits SET action = 'delete', employee_id = OLD.id, employee_name = OLD.name, action_date = NOW();
END;

In this code:
- before_employee_delete: The name of our trigger.
- BEFORE DELETE ON employees: This trigger will run before a DELETE operation on the 'employees' table.
- FOR EACH ROW: Specifies that the trigger will run for each row being deleted.
- INSERT INTO employee_audits SET action = 'delete', employee_id = OLD.id, employee_name = OLD.name, action_date = NOW();: This is the operation that will run when the trigger is activated. It inserts a record in the 'employee_audits' table with the details of the deleted row.

Summary

You've learned what SQL triggers are, how to create them, and how they can be used to automate tasks such as logging and data validation. The next step would be to learn about different types of triggers and how to use them for more complex automation tasks.

Additional resources:
- SQL Trigger | Oracle
- Triggers | MySQL

Practice Exercises

  1. Write a trigger that updates an 'updated_at' timestamp column whenever a row in the 'employees' table is updated.
  2. Create a trigger that prevents deletion from the 'employees' table if the employee's 'status' column is set to 'ACTIVE'.
  3. Write a trigger that inserts a record into a 'sales_audits' table whenever a row is inserted into a 'sales' table with a 'total' column value greater than 1000.

Solutions and tips will be provided upon request. Happy coding!