0% found this document useful (0 votes)
2 views4 pages

exp 8

The document outlines the creation of various SQL triggers for managing employee data in a database. It includes triggers for auditing new employee additions, preventing negative salaries, logging salary updates, and preventing deletions of employees in the HR department. Each section provides steps for creating tables, triggers, and testing the functionality with SQL commands.

Uploaded by

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

exp 8

The document outlines the creation of various SQL triggers for managing employee data in a database. It includes triggers for auditing new employee additions, preventing negative salaries, logging salary updates, and preventing deletions of employees in the HR department. Each section provides steps for creating tables, triggers, and testing the functionality with SQL commands.

Uploaded by

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

1.

Creating a Trigger for Auditing (AFTER INSERT)

Scenario: Maintain an audit log when a new employee is added.

Step 1: Create Tables

CREATE TABLE Employees (

EmpID INT PRIMARY KEY,

Name VARCHAR(50),

Age INT,

Department VARCHAR(50)

);

CREATE TABLE Employee_Audit (

AuditID INT AUTO_INCREMENT PRIMARY KEY,

EmpID INT,

Action VARCHAR(50),

Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP

);

Step 2: Create Trigger

DELIMITER $$

CREATE TRIGGER After_Employee_Insert

AFTER INSERT ON Employees

FOR EACH ROW

BEGIN

INSERT INTO Employee_Audit (EmpID, Action)

VALUES (NEW.EmpID, 'INSERTED');

END$$

DELIMITER ;

Step 3: Insert Data into Employees

INSERT INTO Employees VALUES (1, 'Alice', 30, 'HR');

INSERT INTO Employees VALUES (2, 'Bob', 25, 'IT');


Step 4: View Employee_Audit Table

SELECT * FROM Employee_Audit;

2. Creating a Trigger for Preventing Negative Salary (BEFORE INSERT)

Scenario: Prevent employees from being inserted with a negative salary.

Step 1: Modify Employees Table

ALTER TABLE Employees ADD COLUMN Salary DECIMAL(10,2);

Step 2: Create Trigger

DELIMITER $$

CREATE TRIGGER Before_Employee_Insert

BEFORE INSERT ON Employees

FOR EACH ROW

BEGIN

IF NEW.Salary < 0 THEN

SIGNAL SQLSTATE '45000'

SET MESSAGE_TEXT = 'Salary cannot be negative';

END IF;

END$$

DELIMITER ;

Step 3: Insert Valid and Invalid Data

INSERT INTO Employees (EmpID, Name, Age, Department, Salary) VALUES (3, 'Charlie', 28, 'Finance',
5000.00);

-- This will fail

INSERT INTO Employees (EmpID, Name, Age, Department, Salary) VALUES (4, 'David', 32, 'IT', -
2000.00);

Step 4: Check Employees Table

SELECT * FROM Employees;

3. Creating a Trigger for Updating Audit Log (AFTER UPDATE)

Scenario: Log when an employee's salary is updated.

Step 1: Create Another Audit Table


CREATE TABLE Salary_Audit (

AuditID INT AUTO_INCREMENT PRIMARY KEY,

EmpID INT,

OldSalary DECIMAL(10,2),

NewSalary DECIMAL(10,2),

Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP

);

Step 2: Create Trigger

DELIMITER $$

CREATE TRIGGER After_Salary_Update

AFTER UPDATE ON Employees

FOR EACH ROW

BEGIN

IF OLD.Salary <> NEW.Salary THEN

INSERT INTO Salary_Audit (EmpID, OldSalary, NewSalary)

VALUES (OLD.EmpID, OLD.Salary, NEW.Salary);

END IF;

END$$

DELIMITER ;

Step 3: Update Employee Salary

UPDATE Employees SET Salary = 6000.00 WHERE EmpID = 3;

Step 4: Check Salary_Audit Table

SELECT * FROM Salary_Audit;

4. Creating a Trigger for Preventing Employee Deletion (BEFORE DELETE)

Scenario: Prevent deletion of employees in the "HR" department.

Step 1: Create Trigger

DELIMITER $$
CREATE TRIGGER Before_Employee_Delete

BEFORE DELETE ON Employees

FOR EACH ROW

BEGIN

IF OLD.Department = 'HR' THEN

SIGNAL SQLSTATE '45000'

SET MESSAGE_TEXT = 'HR employees cannot be deleted';

END IF;

END$$

DELIMITER ;

Step 2: Try Deleting Employees

DELETE FROM Employees WHERE EmpID = 1; -- HR Employee (Should Fail)

DELETE FROM Employees WHERE EmpID = 2; -- IT Employee (Should Pass)

You might also like