Junior — Middle
In what cases is a trigger activated in a database or automation system?
sobes.tech AI
Answer from AI
A trigger in a database or automation system is activated automatically when a certain event occurs. In databases, this is usually data modification operations such as INSERT, UPDATE, or DELETE on a table. For example, a trigger may fire after a new record is added or before an existing one is deleted.
In automation systems, a trigger can be an event such as receiving a message, a change in device status, a specific time, or a condition being met. The trigger initiates a predefined set of actions or scripts.
Thus, a trigger is activated by:
- Data changes (INSERT, UPDATE, DELETE) in the database
- The occurrence of a specific event in the system (e.g., signal, message, timer)
- The fulfillment of a condition set in the trigger logic
Example of an SQL trigger that fires after a record is inserted:
CREATE TRIGGER trg_after_insert
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO audit_log(action, emp_id, action_time)
VALUES ('INSERT', NEW.id, NOW());
END;