Triggers

SQL · Views & Triggers

Theory

A trigger is a block of SQL that the database runs automatically when a specific event happens on a table: an INSERT, UPDATE or DELETE. You define when it fires (BEFORE or AFTER the change, or INSTEAD OF it for views) and whether it runs once per affected row (FOR EACH ROW) or once per statement. Inside a row-level trigger, NEW holds the incoming values and OLD the previous ones.

A typical use is auditing: AFTER INSERT ON employees FOR EACH ROW, write NEW.id and the current time into audit_log. BEFORE triggers can validate or adjust values before they are saved. Triggers run inside the same transaction, so if the trigger fails, the original change is rolled back too. Because they are invisible to the person running the statement, heavy or chained triggers make systems slow and hard to debug. Syntax differs a lot between engines.

Why we use it

Some actions must happen every time data changes, no matter which app or person made the change: logging, keeping a last_updated column current, enforcing complex rules. Triggers guarantee it inside the database.

When to use it

Use triggers for auditing, automatic timestamps and integrity rules that constraints cannot express. Prefer constraints for simple rules and application or pipeline code for business workflows, so logic stays visible and testable.

How it works

  1. An INSERT, UPDATE or DELETE runs on employees
  2. Is a trigger defined for this event? Yes: Check its timing. No: Change runs normally.
  3. BEFORE trigger: inspect or change NEW values
  4. Apply the change to the table
  5. AFTER trigger: e.g. write a row to audit_log
  6. Did the trigger raise an error? Yes: Roll back the whole statement. No: Commit together.
  7. Change and trigger work succeed as one unit

Syntax

CREATE TRIGGER trigger_name
AFTER INSERT ON table
FOR EACH ROW
BEGIN ... END;

Example

CREATE TRIGGER log_new_employee
AFTER INSERT ON employees
FOR EACH ROW
INSERT INTO audit_log VALUES (NEW.id, NOW());

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic