0% found this document useful (0 votes)
20 views8 pages

Exp 3 and 4

The document outlines experiments related to database management, focusing on SQL queries using aggregate functions and the creation of triggers for a customers table. It includes instructions for creating an Employee table, performing various queries, and implementing triggers to capture salary changes during insert, update, and delete operations. Detailed SQL commands and their explanations are provided for each task.

Uploaded by

madhukeshs0742
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)
20 views8 pages

Exp 3 and 4

The document outlines experiments related to database management, focusing on SQL queries using aggregate functions and the creation of triggers for a customers table. It includes instructions for creating an Employee table, performing various queries, and implementing triggers to capture salary changes during insert, update, and delete operations. Detailed SQL commands and their explanations are provided for each task.

Uploaded by

madhukeshs0742
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/ 8

DATABASE MANAGEMENT SYSTEM ( BCS403 )

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)
);

mysql > insert into Employee values(101,'Shravya',19,20000);


mysql > insert into Employee values(102, 'keerthi',32,15000);
mysql > insert into Employee values(103, 'bhuvan',18,30000);
mysql > insert into Employee values(104, 'akash',55,50000);
mysql> insert into Employee values(105, 'Sumanth ',20,20000);

Department of CSE, SVIT, Bengaluru Page |1


DATABASE MANAGEMENT SYSTEM ( BCS403 )
2. Count number of employee names from employee table:

mysql>SELECT COUNT(E_name) FROM Employee;

3. Find the Maximum age from employee table:

mysql>SELECT MAX(Age) FROM Employee;

4. Find the Minimum age from employee table:

mysql>SELECT MIN(Age) FROM Employee;

5. Find salaries of employees in Ascending Order:


mysql>SELECT E_name, Salary FROM Employee ORDER BY Salary ASC;

Department of CSE, SVIT, Bengaluru Page |2


DATABASE MANAGEMENT SYSTEM ( BCS403 )

6. Find grouped salaries of employees:


mysql>SELECT Salary, COUNT(*) AS Num_of_Employees
FROM Employee GROUP BY Salary;

 COUNT(E_name) : counts the number of non-NULL values in the `E_name` column.


 MAX(Age) : finds the maximum age among the employees.
 MIN(Age):finds the minimum age among the employees.
 ORDER BY Salary ASC: sorts the employees based on their salaries in ascending order.
 GROUP BY Salary: groups employees by their salaries and counts the number of
employees for each salary.

Department of CSE, SVIT, Bengaluru Page |3


DATABASE MANAGEMENT SYSTEM ( BCS403 )
Experiment 4
Create a row level trigger for the customers table that would fire for INSERT or
UPDATE or DELETE operations performed on the CUSTOMERS table. This trigger
will display the salary difference between the old & new Salary.
CUSTOMERS(ID,NAME,AGE,ADDRESS,SALARY)

Solution
mysql> CREATE DATABASE COMPANY04;
Query OK, 1 row affected (0.14 sec)

mysql> USE COMPANY04;


Database changed

mysql>CREATE TABLE CUSTOMERS (ID INT PRIMARY KEY,


NAME varchar(20) NOT NULL,
AGE INT,
ADDRESS varchar(25),
SALARY Decimal (10,2));

Department of CSE, SVIT, Bengaluru Page |4


DATABASE MANAGEMENT SYSTEM ( BCS403 )

Note: To achieve the desired functionality of capturing changes on `INSERT`,


`UPDATE`, or `DELETE` operations and displaying the salary difference in MySQL,
you’ll need to create separate row-level triggers for each operation (`INSERT`,
`UPDATE`,`DELETE`). These triggers will capture the `OLD` and `NEW` values of the
`SALARY`column and display the salary difference when an INSERT, UPDATE, or
DELETE.

mysql>insert into CUSTOMERS values (111, 'kavana', 20 , 'Bengaluru', 29000) ;


mysql >insert into CUSTOMERS values (222, 'Darshan', 35 , 'Hassan', 15000);

1. Create Trigger for INSERT Operation


2. #INSERT TRIGGER
3. DELIMITER
4.
5. CREATE TRIGGER after_insert_salary_difference
6. AFTER INSERT ON CUSTOMERS
7. FOR EACH
8. ROW BEGIN
9. SET @my_sal_diff = CONCAT('salary inserted is ', NEW.SALARY END;
10.
11. DELIMITER ;
12. #INSERT TRIGGER
13. DELIMITER
14.
15. CREATE TRIGGER after_insert_salary_difference
16. AFTER INSERT ON CUSTOMERS
17. FOR EACH
18. ROW BEGIN
19. SET @my_sal_diff = CONCAT('salary inserted is ', NEW.SALARY END;
20.
21. DELIMITER ;
22. #INSERT TRIGGER
23. DELIMITER

Department of CSE, SVIT, Bengaluru Page |5


DATABASE MANAGEMENT SYSTEM ( BCS403 )

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 ;

Department of CSE, SVIT, Bengaluru Page |6


DATABASE MANAGEMENT SYSTEM ( BCS403 )

3. Create Trigger for DELETE Operation


#DELETE TRIGGER
DELIMITER $$

CREATE TRIGGER after_delete_salary_difference


AFTER DELETE ON CUSTOMERS
FOR EACH ROW
BEGIN
SET @my_sal_diff = CONCAT('salary deleted is ', OLD.SALARY) ;
END $$
DELIMITER;

Testing the Trigger:


Once the triggers are created, you can perform `INSERT`, `UPDATE`, or `DELETE`
operations on the `CUSTOMERS` table to observe the salary difference messages
generated by the triggers.

mysql>SHOW TRIGGERS LIKE 'CUSTOMERS';

Test Insert Trigger


mysql> INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY) VALUES
(1,'Shankara', 35, '123 Main St', 50000.00);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT @my_sal_diff AS SAL_DIFF;

Test Update Trigger

Department of CSE, SVIT, Bengaluru Page |7


DATABASE MANAGEMENT SYSTEM ( BCS403 )

mysql> UPDATE CUSTOMERS SET SALARY = 55000.00 WHERE ID = 1;


Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT @my_sal_diff AS SAL_DIFF;

Test Delete Trigger

mysql> DELETE FROM CUSTOMERS WHERE ID = 1;


Query OK, 1 row affected (0.01 sec)

mysql> SELECT @my_sal_diff AS SAL_DIFF;

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.

Department of CSE, SVIT, Bengaluru Page |8

You might also like