PGoCareerGoCareer prep tools
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

SQL · Theory

BEFORE DELETE Trigger

← All stacks

Theory

91/439

BEFORE DELETE Trigger

A trigger is SQL that runs automatically on INSERT, UPDATE, or DELETE. CREATE TRIGGER trg BEFORE INSERT ON marks FOR EACH ROW SET NEW.score = IF(NEW.score < 0, 0, NEW.score); clamps negative scores.

BEFORE can change NEW values. AFTER sees the row already written. FOR EACH ROW is the MySQL student form. Don’t hide all business rules in triggers — apps become hard to debug.

OLD is the previous row on UPDATE/DELETE. NEW is the incoming row on INSERT/UPDATE.

Trap — trigger that INSERTs into the same table → loop. Trap: huge logic that belongs in the app.

On the example next to this theory: Create Trigger: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

On the example next to this theory: Show Trigger: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

On the example next to this theory: DROP Trigger: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

On the example next to this theory — Before Insert Trigger — iNSERT adds rows. List columns explicitly for clarity.

On the example next to this theory — After Insert Trigger — iNSERT adds rows. List columns explicitly for clarity.

On the example next to this theory: BEFORE DELETE Trigger — dELETE removes matching rows. TRUNCATE clears a whole table faster (MySQL).

Exam tip

BEFORE INSERT … FOR EACH ROW SET NEW.col = …

BEFORE DELETE Trigger — sample query

CREATE TABLE tags (id INTEGER PRIMARY KEY, label TEXT);
INSERT INTO tags VALUES (1, 'old'), (2, 'keep');
DELETE FROM tags WHERE label = 'old';
SELECT * FROM tags;

BEFORE DELETE Trigger — dELETE removes matching rows. TRUNCATE clears a whole table faster (MySQL).

Short notes

  • DefTrigger = automatic SQL on INSERT/UPDATE/DELETE.
  • RuleBEFORE can edit NEW. AFTER cannot. OLD vs NEW.
  • TrapRecursive triggers. Business logic only in triggers.

Questions

1

When does a trigger run?

2

NEW vs OLD?

91 / 439

P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.