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

SQL · Theory

Show Trigger

← All stacks

Theory

85/439

Show 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.

Exam tip

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

Show Trigger — sample query

-- Show Trigger
CREATE TABLE demo (
  id INTEGER PRIMARY KEY,
  label TEXT NOT NULL,
  qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
  (1, 'alpha', 2),
  (2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;

Show Trigger: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

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?

85 / 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.