DELETE Triggers

In this lesson we will learn how to create triggers associated with the DELETE statement.

We'll cover the following

DELETE Triggers

Delete triggers for a table are fired when a DELETE statement is executed. Just like the insert and update triggers, delete triggers can be executed before or after a record is deleted from the table. Since the DELETE statement is meant to delete a record, the columns do not have a NEW value. Only the OLD value of a column is accessible and that too cannot be updated.

Delete triggers can be used to archive deleted records. In some cases, BEFORE DELETE triggers are used to stop an invalid delete operation for example, if there are two tables for Course and Registration information, then it does not make sense to delete a course when there are students registered for it. Delete triggers can also be used to update a summary table or maintain a change log after records are removed from the table. Delete triggers are not available for views.

Syntax

CREATE TRIGGER trigger_name [BEFORE | AFTER] DELETE

ON table_name

FOR EACH ROW

trigger_body

Connect to the terminal below by clicking in the widget. Once connected, the command line prompt will show up. Enter or copy-paste the command ./DataJek/Lessons/49lesson.sh and wait for the mysql prompt to start-up.

Get hands-on with 1400+ tech skills courses.