TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2443 3.06K
๐Ÿ”ฅ Now, letโ€™s move to the next topic:

Triggers in SQL
(Automation inside database ๐Ÿ’ฏ)

๐Ÿง  1. What is a Trigger?
A Trigger is a special SQL block
๐Ÿ‘‰ that runs automatically
๐Ÿ‘‰ when an event happens in a table

Think like this ๐Ÿ‘‡
๐Ÿ‘‰ โ€œAutomatic action on INSERT / UPDATE / DELETEโ€

โšก 2. Why Use Triggers?
โœ” Automatic logging
โœ” Data validation
โœ” Audit tracking
โœ” Prevent invalid operations

โšก 3. Types of Triggers
BEFORE INSERT โ†’ Runs before inserting data
AFTER INSERT โ†’ Runs after inserting data
BEFORE UPDATE โ†’ Runs before updating
AFTER UPDATE โ†’ Runs after updating
BEFORE DELETE โ†’ Runs before deleting
AFTER DELETE โ†’ Runs after deleting

๐Ÿ”ฅ 4. Basic Trigger Example
๐Ÿ‘‰ Automatically log inserted employee

CREATE TABLE employee_log (
log_message VARCHAR(255)
);

DELIMITER //

CREATE TRIGGER after_employee_insert
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO employee_log
VALUES (CONCAT('New employee added: ', NEW.name));
END //

DELIMITER ;

๐Ÿง  5. Important Keywords
NEW โ†’ New inserted/updated value
OLD โ†’ Previous value before update/delete

โšก 6. BEFORE UPDATE Example
๐Ÿ‘‰ Prevent negative salary
DELIMITER //

CREATE TRIGGER check_salary
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
IF NEW.salary < 0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Salary cannot be negative';
END IF;
END //

DELIMITER ;

โŒ 7. Drop Trigger
DROP TRIGGER after_employee_insert;

๐ŸŽฏ 8. Practice Tasks
1. Create AFTER INSERT trigger
2. Create BEFORE UPDATE trigger
3. Prevent negative salary using trigger
4. Log deleted employees
5. Drop created trigger

โšก Mini Challenge ๐Ÿ”ฅ
๐Ÿ‘‰ Create trigger to automatically save deleted employee names into another table

๐Ÿ”ฅ Mini Challenge Solution

๐Ÿ‘‰ Automatically save deleted employee names into another table

โœ… Step 1: Create Log Table

CREATE TABLE deleted_employees (
emp_id INT,
name VARCHAR(50),
deleted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

โœ… Step 2: Create Trigger

DELIMITER //

CREATE TRIGGER log_deleted_employee
AFTER DELETE ON employees
FOR EACH ROW
BEGIN
INSERT INTO deleted_employees(emp_id, name)
VALUES (OLD.emp_id, OLD.name);
END //

DELIMITER ;

๐Ÿง  How It Works

๐Ÿ‘‰ AFTER DELETE โ†’ runs automatically after deletion

๐Ÿ‘‰ OLD.emp_id and OLD.name
Access deleted row values before they disappear

โœ… Example

DELETE FROM employees
WHERE emp_id = 101;

โœ” Deleted employee info automatically saved in deleted_employees table ๐Ÿ’ฏ

๐Ÿ”ฅ Pro Tip
Triggers are powerful but:
โŒ Too many triggers can slow database
โœ… Use them carefully ๐Ÿ’ฏ

Double Tap โค๏ธ For More
  • โค 7
More from @sqlanalyst
  1. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  2. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  3. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  6. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’