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

mysql 4

The document outlines the creation of a database named 'lab4expie' and a table 'CUSTOM2' to store employee information. It also establishes a logging table 'SalaryChange1' and three triggers to log salary changes upon insertion, updating, and deletion of records in 'CUSTOM2'. Finally, it includes test commands to demonstrate the functionality of the triggers and a query to retrieve all logs from the logging table.

Uploaded by

alanjames192004
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 views3 pages

mysql 4

The document outlines the creation of a database named 'lab4expie' and a table 'CUSTOM2' to store employee information. It also establishes a logging table 'SalaryChange1' and three triggers to log salary changes upon insertion, updating, and deletion of records in 'CUSTOM2'. Finally, it includes test commands to demonstrate the functionality of the triggers and a query to retrieve all logs from the logging table.

Uploaded by

alanjames192004
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/ 3

-- Create the database

CREATE DATABASE lab4expie;

-- Use the database

USE lab4expie;

-- Create the CUSTOM2 table

CREATE TABLE CUSTOM2 (

ID INT PRIMARY KEY AUTO_INCREMENT,

NAME VARCHAR(255),

AGE INT,

ADDRESS VARCHAR(255),

SALARY DECIMAL(10, 2)

);

-- Create a logging table to store salary change messages

CREATE TABLE SalaryChange1 (

ID INT PRIMARY KEY AUTO_INCREMENT,

LogMessage VARCHAR(255),

Timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

-- INSERT TRIGGER

DELIMITER //

CREATE TRIGGER after_insert_salary_difference2

AFTER INSERT ON CUSTOM2

FOR EACH ROW


BEGIN

-- Log the salary inserted

INSERT INTO SalaryChange1 (LogMessage)

VALUES (CONCAT('Salary inserted is ', NEW.SALARY));

END;//

DELIMITER ;

-- UPDATE TRIGGER

DELIMITER //

CREATE TRIGGER after_update_salary_difference2

AFTER UPDATE ON CUSTOM2

FOR EACH ROW

BEGIN

-- Log the salary difference after update

INSERT INTO SalaryChange1 (LogMessage)

VALUES (CONCAT('Salary difference after update is ', NEW.SALARY - OLD.SALARY));

END;//

DELIMITER ;

-- DELETE TRIGGER

DELIMITER //

CREATE TRIGGER after_delete_salary_difference2

AFTER DELETE ON CUSTOM2

FOR EACH ROW

BEGIN

-- Log the salary deleted

INSERT INTO SalaryChange1 (LogMessage)


VALUES (CONCAT('Salary deleted is ', OLD.SALARY));

END;//

DELIMITER ;

-- Test INSERT TRIGGER

INSERT INTO CUSTOM2(NAME, AGE, ADDRESS, SALARY)

VALUES ('Shankara', 35, '123 Main St', 50000.00);

-- Test UPDATE TRIGGER

UPDATE CUSTOM2 SET SALARY = 55000.00 WHERE ID = 1;

-- Test DELETE TRIGGER

DELETE FROM CUSTOM2 WHERE ID = 1;

-- Retrieve all logs from the SalaryChange1 table to view the changes

SELECT * FROM SalaryChange1;

You might also like