Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Inside the body of a trigger, MySQL provides two virtual pseudo-records: OLD and NEW. These records represent the state of the row before and after the triggering DML modification.
Understanding the availability of OLD and NEW across different DML events is crucial for data auditing and integrity enforcement.
| Event | OLD Available? | NEW Available? | Modifiable via SET? |
|---|---|---|---|
INSERT | No (Row didn't exist prior) | Yes (Represents new incoming row) | Yes (in BEFORE INSERT) |
UPDATE | Yes (Represents values before edit) | Yes (Represents edited values) | Yes (in BEFORE UPDATE) |
DELETE | Yes (Represents values being removed) | No (Row is deleted; no new state) | No (OLD is strictly read-only) |
SET NEW.colIn BEFORE triggers, you can overwrite the values that will be written to disk:
NEW columns in BEFORE triggers. In AFTER triggers, the row has already been committed to storage, so NEW becomes read-only!Suppose management mandates that no employee's salary can ever be decreased, and single raises cannot exceed 20%:
By comparing OLD.col with NEW.col, you can determine precisely which columns were modified during an UPDATE:
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Trigger Fundamentals & Event Hooks | Building an Automated Audit Logging System with Triggers |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.