0
0
SQLquery~20 mins

Trigger for audit logging in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Audit Logging Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this audit trigger insertion?

Given a trigger that logs inserts into employees table, what will be the content of audit_log after inserting a new employee?

SQL
CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE audit_log (action VARCHAR(10), emp_id INT, emp_name VARCHAR(50));

CREATE TRIGGER log_employee_insert
AFTER INSERT ON employees
FOR EACH ROW
INSERT INTO audit_log VALUES ('INSERT', NEW.id, NEW.name);

INSERT INTO employees VALUES (1, 'Alice');

SELECT * FROM audit_log;
A
action | emp_id | emp_name
-------+--------+---------
INSERT | 1      | Alice
B
action | emp_id | emp_name
-------+--------+---------
INSERT | NULL   | NULL
CNo rows returned
DSyntax error in trigger definition
Attempts:
2 left
💡 Hint

Remember that NEW keyword in triggers refers to the inserted row.

Configuration
intermediate
2:00remaining
Which trigger definition correctly logs DELETE actions?

Choose the correct SQL trigger that logs deletions from employees into audit_log with action 'DELETE'.

ACREATE TRIGGER log_employee_delete BEFORE DELETE ON employees FOR EACH ROW INSERT INTO audit_log VALUES ('DELETE', NEW.id, NEW.name);
BCREATE TRIGGER log_employee_delete AFTER DELETE ON employees FOR EACH ROW INSERT INTO audit_log VALUES ('DELETE', NEW.id, NEW.name);
CCREATE TRIGGER log_employee_delete AFTER DELETE ON employees FOR EACH ROW INSERT INTO audit_log VALUES ('DELETE', OLD.id, OLD.name);
DCREATE TRIGGER log_employee_delete BEFORE DELETE ON employees FOR EACH ROW INSERT INTO audit_log VALUES ('DELETE', OLD.id, OLD.name);
Attempts:
2 left
💡 Hint

For DELETE triggers, OLD holds the row being deleted.

Troubleshoot
advanced
2:00remaining
Why does this audit trigger fail to log updates?

Given this trigger, why does it fail to log the updated data in audit_log after updating employees?

SQL
CREATE TRIGGER log_employee_update
AFTER UPDATE ON employees
FOR EACH ROW
INSERT INTO audit_log VALUES ('UPDATE', OLD.id, OLD.name);
AThe trigger is AFTER UPDATE but should be BEFORE UPDATE.
BThe trigger uses OLD values instead of NEW values for updated data.
CThe trigger syntax is invalid because it lacks BEGIN...END block.
DThe audit_log table does not exist.
Attempts:
2 left
💡 Hint

Consider which values reflect the updated data after an update.

🔀 Workflow
advanced
2:00remaining
Order the steps to create an audit logging trigger for INSERT and UPDATE

Put these steps in the correct order to create a trigger that logs both INSERT and UPDATE actions on employees.

A3,2,1,4
B2,1,3,4
C1,3,2,4
D1,2,3,4
Attempts:
2 left
💡 Hint

Think about what you need before creating triggers and testing.

Best Practice
expert
3:00remaining
Which approach best ensures audit logging does not block main transactions?

In a high-traffic database, which method best prevents audit logging triggers from slowing down main data changes?

AUse asynchronous logging by writing audit entries to a queue table and processing them later
BWrite audit logs synchronously inside the trigger using INSERT statements
CDisable triggers during peak hours to improve performance
DLog audit data only once per day using a scheduled batch job
Attempts:
2 left
💡 Hint

Consider how to avoid slowing down the main transaction.