CodeGym /Courses /SQL SELF /Getting to Know Triggers

Getting to Know Triggers

SQL SELF
Level 57 , Lesson 0
Available

A trigger is basically a "callback" in a database that reacts to certain events. In other words, a trigger is an automatic reaction to operations like INSERT, UPDATE, or DELETE in tables.

Imagine you have a smart assistant who does stuff for you. For example, every time you add a new student record, the assistant automatically updates the "last modified" date field. A trigger in a database works just like that: it "listens" for events and fires off a programmed reaction.

A trigger combines two things:

  1. Event: something happens in a table (like inserting a record).
  2. Trigger function: the code that runs when the event fires.
Important:

a trigger can't exist without being linked to a function. The function defines what exactly to do when the trigger fires.

Examples of Using Triggers

Let's break down a few situations where triggers can come in handy.

  1. Automatic Data Updates

You want a table where you store student records to have a last_modified field that automatically updates every time a record changes. Instead of updating this field manually every time, you can create a trigger that does it for you.

  1. Logging Changes

You want to track who and when changes data in a table. A trigger can automatically add a record to an audit (log) table every time data changes.

  1. Data Validation

If data is being added to a table that needs to follow certain rules (like a student's age must be over 18), a trigger can check the data before it's inserted.

  1. Automatic Calculations

In the orders table, you store orders, and every time you add an order, you need to update the customer's total purchase amount. Instead of doing this manually, a trigger can automatically update the total.

When to Use Triggers

Now that we know what they can do, let's talk about when you should actually use triggers.

Logging and Auditing: triggers are perfect for creating audit records to track changes in super important tables.

Maintaining Data Integrity: for example, if you delete a course from the database, a trigger can automatically delete all students linked to that course so you don't end up with "dangling" data.

Automating Repetitive Tasks: these tasks can include updating calculated values, updating aggregated data, and so on.

Implementing Business Logic in the DB: instead of relying only on your app code, you can move some logic right into the database layer.

Advantages of Triggers

As you probably guessed, triggers are a powerful tool. Here's what they bring to the table:

Automation: minimal human involvement. For example, tracking users who change data happens automatically.

Less Code Duplication: instead of making devs write update or validation logic in every app, we can build it right into the DB.

Ensuring Data Integrity: a trigger can be an extra layer of protection to make sure your data is always correct.

Disadvantages of Triggers

Of course, like any tool, triggers have their downsides. Let's look at their dark side:

  • Hard to Debug: triggers work "behind the scenes." If they don't behave as expected, debugging them can be a pain.

  • Potential Performance Issues: if a trigger is too complex or fires too often, it can slow down your SQL queries.

  • Hidden Logic: when business logic is "buried" in triggers, it's harder for devs to figure out what's really going on in the database.

Real-World Use Cases

Example 1: Logging Changes

Let's say we have a students table where we store student data. We want to track changes to records. For this, a trigger will add a record to the audit_log table every time student data changes.

Example 2: Automatic Updates

The students table has a last_modified column. We want its value to update every time student data changes. You can do this with a trigger that fires after an update.

2
Task
SQL SELF, level 57, lesson 0
Locked
Defining and Logging Variable Types
Defining and Logging Variable Types
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION