What if your database could watch every change and never miss a detail?
Why Trigger for audit logging in SQL? - Purpose & Use Cases
Imagine you have a busy database where many users update important records every day. You want to keep track of every change to know who did what and when.
Without automation, you might try to write down changes by hand or run manual reports after the fact.
Manually tracking changes is slow and easy to forget. You might miss important updates or record wrong details. It's like trying to watch every move in a busy store without any cameras.
This leads to mistakes, confusion, and no clear history of changes.
Using a trigger for audit logging means the database automatically records every change as it happens. It's like having a security camera that instantly saves a video whenever something important happens.
This makes tracking changes fast, accurate, and hands-free.
UPDATE employees SET salary = 5000 WHERE id = 10; -- Then manually insert audit record
CREATE TRIGGER audit_salary_update
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
INSERT INTO audit_log(user, action, timestamp) VALUES (CURRENT_USER, 'Updated salary', NOW());
END;It enables reliable, automatic tracking of all data changes without extra work.
In a bank, every transaction must be logged to prevent fraud. Triggers automatically record who changed account balances and when, ensuring trust and security.
Manual tracking is slow and error-prone.
Triggers automate audit logging instantly.
This ensures accurate, reliable change history.
Practice
Solution
Step 1: Understand what audit logging means
Audit logging means keeping a record of changes made to data, such as who changed it and when.Step 2: Identify the role of triggers in audit logging
Triggers automatically run when data changes and can insert records into an audit table to log these changes.Final Answer:
To automatically record changes made to data in a table -> Option AQuick Check:
Audit logging = record data changes automatically [OK]
- Thinking triggers speed up queries
- Confusing audit logging with backups
- Believing triggers manage user permissions
employees?Solution
Step 1: Identify correct trigger timing and event
The question asks for a trigger after INSERT, so it must say AFTER INSERT.Step 2: Check syntax correctness
CREATE TRIGGER trg AFTER INSERT ON employees FOR EACH ROW BEGIN ... END; uses correct syntax: CREATE TRIGGER name AFTER INSERT ON table FOR EACH ROW BEGIN ... END;Final Answer:
CREATE TRIGGER trg AFTER INSERT ON employees FOR EACH ROW BEGIN ... END; -> Option AQuick Check:
Trigger syntax = CREATE TRIGGER ... AFTER INSERT ON ... [OK]
- Using BEFORE UPDATE instead of AFTER INSERT
- Omitting FOR EACH ROW clause
- Wrong trigger event like DELETE instead of INSERT
CREATE TRIGGER audit_update AFTER UPDATE ON products FOR EACH ROW BEGIN INSERT INTO audit_log (product_id, old_price, new_price) VALUES (OLD.id, OLD.price, NEW.price); END;
What will be inserted into
audit_log when a product's price changes from 100 to 120?Solution
Step 1: Understand OLD and NEW in triggers
OLD refers to the row before update, NEW refers to the row after update.Step 2: Analyze the insert statement
The trigger inserts OLD.id, OLD.price, and NEW.price, so product_id is the old id, old_price is 100, new_price is 120.Final Answer:
(product_id: product's id, old_price: 100, new_price: 120) -> Option DQuick Check:
OLD = before update, NEW = after update [OK]
- Mixing OLD and NEW values
- Assuming NULL values for unchanged columns
- Confusing product_id with price values
CREATE TRIGGER log_delete AFTER DELETE ON orders FOR EACH ROW BEGIN INSERT INTO audit_orders (order_id, deleted_at) VALUES (NEW.id, NOW()); END;
Why does this trigger cause an error?
Solution
Step 1: Recall OLD and NEW usage in triggers
In DELETE triggers, only OLD is available because the row is removed; NEW does not exist.Step 2: Identify the error cause
The trigger uses NEW.id in a DELETE trigger, which causes an error because NEW is undefined.Final Answer:
Using NEW keyword in a DELETE trigger causes an error -> Option BQuick Check:
DELETE triggers use OLD, not NEW [OK]
- Using NEW instead of OLD in DELETE triggers
- Confusing FOR EACH ROW and FOR EACH STATEMENT
- Thinking NOW() is disallowed in triggers
customers table into customer_audit. Which approach correctly handles both actions in one trigger?Solution
Step 1: Understand trigger event options
Some SQL systems allow triggers on multiple events like AFTER INSERT OR UPDATE.Step 2: Use conditional logic inside trigger
Inside the trigger, check if the event is INSERT or UPDATE to log accordingly.Step 3: Compare options
Create one trigger AFTER INSERT OR UPDATE and use conditional logic inside correctly combines both events in one trigger with logic; Create two separate triggers: one AFTER INSERT and one AFTER UPDATE uses two triggers, which is valid but not one trigger.Final Answer:
Create one trigger AFTER INSERT OR UPDATE and use conditional logic inside -> Option CQuick Check:
One trigger can handle multiple events with conditions [OK]
- Trying to log UPDATE in an INSERT-only trigger
- Using BEFORE DELETE for INSERT/UPDATE logging
- Not using conditional logic inside multi-event triggers
Ready to prove it?
Pass the test and this pattern moves unknown
