0% found this document useful (0 votes)
2 views

Assignment 12

The document outlines the creation of two MySQL tables: 'Employee' for storing employee details and 'update_info' for tracking salary changes. It includes the insertion of three employee records and the implementation of a trigger that logs salary updates into the 'update_info' table. An example update of an employee's salary is demonstrated, followed by a query showing the updated records and the logged changes.

Uploaded by

Mrinmoy Pathak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 12

The document outlines the creation of two MySQL tables: 'Employee' for storing employee details and 'update_info' for tracking salary changes. It includes the insertion of three employee records and the implementation of a trigger that logs salary updates into the 'update_info' table. An example update of an employee's salary is demonstrated, followed by a query showing the updated records and the logged changes.

Uploaded by

Mrinmoy Pathak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

mysql> use assignment_12;

Database changed
mysql>

mysql> CREATE TABLE Employee (


-> emp_id INT PRIMARY KEY,
-> name VARCHAR(50),
-> salary DECIMAL(10, 2)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE update_info (


-> emp_id INT,
-> old_salary DECIMAL(10, 2),
-> new_salary DECIMAL(10, 2),
-> update_date DATETIME
-> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO Employee (emp_id, name, salary) VALUES


-> (1, 'John Doe', 50000.00),
-> (2, 'Jane Smith', 60000.00),
-> (3, 'Bob Johnson', 75000.00);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql>
mysql> SELECT * FROM Employee;
+--------+-------------+----------+
| emp_id | name | salary |
+--------+-------------+----------+
| 1 | John Doe | 50000.00 |
| 2 | Jane Smith | 60000.00 |
| 3 | Bob Johnson | 75000.00 |
+--------+-------------+----------+
3 rows in set (0.00 sec)

mysql>
mysql> DELIMITER $$
mysql> CREATE TRIGGER update_info_after_update
-> AFTER UPDATE ON Employee
-> FOR EACH ROW
-> BEGIN
-> IF OLD.salary != NEW.salary THEN
-> INSERT INTO update_info (emp_id, old_salary, new_salary,
update_date)
-> VALUES (NEW.emp_id, OLD.salary, NEW.salary, NOW());
-> END IF;
-> END;
-> $$
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql>
mysql> -- Update an employee's salary
mysql> UPDATE Employee SET salary = 55000.00 WHERE emp_id = 2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT * FROM Employee;


+--------+-------------+----------+
| emp_id | name | salary |
+--------+-------------+----------+
| 1 | John Doe | 50000.00 |
| 2 | Jane Smith | 55000.00 |
| 3 | Bob Johnson | 75000.00 |
+--------+-------------+----------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM update_info;


+--------+------------+------------+---------------------+
| emp_id | old_salary | new_salary | update_date |
+--------+------------+------------+---------------------+
| 2 | 60000.00 | 55000.00 | 2023-11-08 15:49:07 |
+--------+------------+------------+---------------------+
1 row in set (0.00 sec)

You might also like