Exp 3 and 4
Exp 3 and 4
Experiment 3
Queries using aggregate functions (COUNT,AVG,MIN,MAX,SUM),Group by,Orderby.
Employee(E_id, E_name, Age, Salary)
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
2. Count number of employee names from employee table
3. Find the Maximum age from employee table.
4. Find the Minimum age from employee table.
5. Find salaries of employee in Ascending Order.
6. Find grouped salaries of employees.
Solution:
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
mysql> CREATE TABLE Employee (
E_id INT PRIMARY KEY,
E_name VARCHAR(25),
Age INT,
Salary DECIMAL(10, 2)
);
Solution
mysql> CREATE DATABASE COMPANY04;
Query OK, 1 row affected (0.14 sec)
24.
25. CREATE TRIGGER after_insert_salary_difference
26. AFTER INSERT ON CUSTOMERS
27. FOR EACH
28. ROW BEGIN
29. SET @my_sal_diff = CONCAT('salary inserted is ', NEW.SALARY END;
30.
31. DELIMITER ;
#INSERT TRIGGER
DELIMITER $$
CREATE TRIGGER after_insert_salary_difference
AFTER INSERT ON CUSTOMERS
FOR EACH ROW
BEGIN
DECLARE my_sal_diff VARCHAR(255);
SET @my_sal_diff = CONCAT('salary inserted is ', NEW.SALARY );
END $$
DELIMITER ;
2. Create Trigger for UPDATE Operation
#UPDATE TRIGGER
DELIMITER $$
CREATE TRIGGER after_update_salary_difference
AFTER UPDATE ON CUSTOMERS
FOR EACH ROW
BEGIN
DECLARE my_sal_diff VARCHAR(255);
SET @my_sal_diff = CONCAT('Salary changed from ', OLD.SALARY, ' to ',
NEW.SALARY);
END $$
DELIMITER ;
Note: Each operation (`INSERT`, `UPDATE`, `DELETE`) will trigger the respective
trigger(`after_insert_salary_difference`,`after_update_salary_difference`,`after_delete_sala
ry_difference`), which will display the salary change or difference associated with
that operation. By using separate triggers for each operation and utilizing the ` OLD`
and `NEW` keywords appropriately within the trigger bodies, you can effectively
capture and handle changes to the `SALARY` column in the `CUSTOMERS` table
in MySQL.