Sobes.tech
Junior — Middle

Millistel juhtudel aktiveerub andmebaasis või automatiseerimissüsteemis trigger?

sobes.tech AI

Vastus AI-lt

Trigger in a database or automation system activates automatically when a specific event occurs. In databases, these are usually data modification operations such as INSERT (insertion), UPDATE (update), or DELETE (deletion) in a table. For example, a trigger can fire after adding a new record or before deleting an existing one.

In automation systems, a trigger can be an event such as receiving a message, changing the state of a device, reaching a certain time, or fulfilling a condition. The trigger initiates a predefined set of actions or scripts.

Thus, a trigger activates upon:

  • 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 defined in the trigger logic

Example of an SQL trigger that fires after inserting a record:

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;