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

SQL · Theory

Before Insert Trigger

← All stacks

Theory

87/439

Before Insert 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.

Exam tip

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

Before Insert Trigger — sample query

CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, price REAL);
INSERT INTO products (id, name, price) VALUES
  (1, 'Mouse', 499),
  (2, 'Keyboard', 999);
SELECT * FROM products;

Before Insert Trigger — iNSERT adds rows. List columns explicitly for clarity.

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?

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