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: Trigger: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
BEFORE INSERT … FOR EACH ROW SET NEW.col = …