0% found this document useful (0 votes)
38 views6 pages

DBMS Week-6 Assignment

Uploaded by

yayage3355
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

DBMS Week-6 Assignment

Uploaded by

yayage3355
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Case Study: Implementing Triggers for INSERT and UPDATE Operations

Background

An organization manages its employees' information in a relational database with an employees

table that

stores key details like name, job title, department, salary, and employment date. To ensure data

integrity,

accountability, and real-time notifications of critical changes, the organization wants to automate

certain

actions whenever data in this table is modified.

Objectives

The following objectives need to be met through trigger-based automation:

1. Audit Log: Track every INSERT and UPDATE operation on the employees table. This is useful for

data audits,

especially for fields like salary and job_title, which have compliance and business significance.

2. Automatic Timestamp Update: Update a last_modified timestamp in the employees table

whenever an UPDATE occurs.

3. Notification Trigger: Send an email notification to the HR department whenever an employee's

salary is updated

to a significant value (e.g., above $100,000).

Database Schema
The primary tables involved are as follows:

CREATE TABLE employees (

employee_id INT PRIMARY KEY,

name VARCHAR(100),

job_title VARCHAR(50),

department VARCHAR(50),

salary DECIMAL(10, 2),

employment_date DATE,

last_modified TIMESTAMP

);

CREATE TABLE audit_log (

log_id INT PRIMARY KEY AUTO_INCREMENT,

employee_id INT,

operation_type VARCHAR(10),

modified_column VARCHAR(50),

old_value VARCHAR(100),

new_value VARCHAR(100),

modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

Solution: Implementing Triggers

The solution involves creating two triggers:

1. AFTER INSERT Trigger: Logs the INSERT event to the audit_log table.
2. AFTER UPDATE Trigger: Logs any changes made to sensitive fields (e.g., salary, job_title) in the

audit_log,

updates the last_modified column, and triggers an email notification if the salary exceeds a

threshold.

Trigger Implementation

1. Trigger for INSERT Operation

This trigger captures every new employee addition and logs it in the audit_log table.

CREATE TRIGGER after_employee_insert

AFTER INSERT ON employees

FOR EACH ROW

BEGIN

INSERT INTO audit_log (employee_id, operation_type, modified_column, new_value)

VALUES (NEW.employee_id, 'INSERT', 'ALL', 'New employee added');

END;

Explanation: This AFTER INSERT trigger logs a new record when a new employee is added, with

operation_type as INSERT.

Here, modified_column is set to 'ALL' to denote that an entire row was inserted.

2. Trigger for UPDATE Operation

This trigger logs specific changes in the employees table and manages updates to the last_modified
column.

Additionally, it checks if the new salary exceeds $100,000 and sends an alert if it does.

CREATE TRIGGER after_employee_update

AFTER UPDATE ON employees

FOR EACH ROW

BEGIN

IF OLD.salary != NEW.salary THEN

INSERT INTO audit_log (employee_id, operation_type, modified_column, old_value,

new_value)

VALUES (NEW.employee_id, 'UPDATE', 'salary', OLD.salary, NEW.salary);

IF NEW.salary > 100000 THEN

CALL send_notification('HR Department', 'Salary Update Alert', CONCAT('Salary updated to

', NEW.salary,

' for employee ID: ', NEW.employee_id));

END IF;

END IF;

IF OLD.job_title != NEW.job_title THEN

INSERT INTO audit_log (employee_id, operation_type, modified_column, old_value,

new_value)

VALUES (NEW.employee_id, 'UPDATE', 'job_title', OLD.job_title, NEW.job_title);

END IF;

UPDATE employees SET last_modified = CURRENT_TIMESTAMP WHERE employee_id =


NEW.employee_id;

END;

Benefits and Challenges

Benefits:

- Automatic Auditing: The trigger-based solution ensures an audit trail for every change, enhancing

accountability.

- Timely Notifications: The notification alert system keeps HR informed about significant salary

updates in real-time.

- Data Consistency: The automatic update of the last_modified column ensures that the modification

timestamps are

consistently tracked without manual intervention.

Challenges:

- Performance Overhead: Frequent updates can create performance bottlenecks due to the trigger's

execution overhead,

especially in a high-traffic environment.

- Complexity in Debugging: Troubleshooting issues related to triggers can be complex, as they

execute automatically

and may not always be visible to users.

Conclusion

Using triggers for INSERT and UPDATE operations in the employees table allowed the organization

to implement essential
functionality like auditing, notifications, and timestamp updates automatically. This solution provided

the necessary

tools to maintain data integrity, track changes efficiently, and enable real-time alerts for critical

updates, making

the employee management process more robust and transparent.

You might also like