0% found this document useful (0 votes)
21 views5 pages

DBEX Ex6 Stored Procedures and Functions 112

1. The document discusses stored procedures and functions in MySQL. It provides examples of creating procedures to sort and retrieve employee data based on parameters. Functions are also created, such as to return the number of employees in a department or who report to a given manager. 2. Examples include procedures to sort employees by salary, get employee details by SSN, retrieve the maximum salary, count employees in a department, delete an employee, and increase all salaries by a percentage. 3. Functions are created to return the number of new employees, subordinates of a manager, and the manager of a given department.

Uploaded by

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

DBEX Ex6 Stored Procedures and Functions 112

1. The document discusses stored procedures and functions in MySQL. It provides examples of creating procedures to sort and retrieve employee data based on parameters. Functions are also created, such as to return the number of employees in a department or who report to a given manager. 2. Examples include procedures to sort employees by salary, get employee details by SSN, retrieve the maximum salary, count employees in a department, delete an employee, and increase all salaries by a percentage. 3. Functions are created to return the number of new employees, subordinates of a manager, and the manager of a given department.

Uploaded by

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

IT22312 – DATABASE CONCEPTS LABORATORY

EX.NO: 6
DATE: STORED PROCEDURES AND FUNCTIONS

AIM:

QUERIES:

1. Create a procedure proc_sort_by__sal() to display the details of the employee sorted


by their salary in descending order (hint: use no parameter)
mysql> DELIMITER &&
mysql> CREATE PROCEDURE proc_sort_by__sal()
-> BEGIN
-> SELECT SALARY FROM EMPLOYEE_45 ORDER BY SALARY DESC;
-> END &&
mysql> DELIMITER ;

mysql> CALL proc_sort_by__sal;


+----------+
| SALARY |
+----------+
| 55000.00 |
| 43000.00 |
| 40000.00 |
| 38000.00 |
| 30000.00 |
| 25000.00 |
| 25000.00 |
| 25000.00 |
+----------+

2.Create a procedure proc_get_emp(..) to display the details of the employee (given the
SSN) (hint: use IN parameter)

mysql> DELIMITER &&


mysql> CREATE PROCEDURE proc_get_emp(E_SSN char(9))
-> BEGIN
-> SELECT FNAME FROM EMPLOYEE_45 WHERE SSN=E_SSN;
-> END &&
Query OK, 0 rows affected (0.03 sec)
mysql> DELIMITER ;
mysql> CALL proc_get_emp('123456789');
+--------+
| FNAME |
+-------+
| John |
+-------+

ROLL NO:2127220801112 PAGE NO:


3.Create a procedure proc_get_max_salary() to retrieve the maximum salary offered y
the company (hint : use OUT)

mysql> DELIMITER &&


mysql> CREATE PROCEDURE proc_get_max_salary(OUT MAX DECIMAL(10,2))
-> BEGIN
-> SELECT MAX(SALARY) INTO MAX FROM EMPLOYEE_45;
-> END &&
Query OK, 0 rows affected (0.03 sec)

mysql> DELIMITER ;
mysql> CALL proc_get_max_salary(@MAX);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT @MAX;


+----------+
| @MAX |
+----------+
| 55000.00 |
+----------+

4.Create a procedure proc_get_no_of_employees() to retrieve the number of employees


in the given department (hint : use INOUT)
mysql> DELIMITER $$
mysql> create procedure proc_get_no_of_employees(inout dept_name varchar(15))
-> BEGIN
-> select count(*) from employee_45 e,department_45 d where e.dno=d.dnumber
and dept_name=dname group by dname;
-> END $$
Query OK, 0 rows affected (0.01 sec)
mysql> set @dept_name='headquaters';
Query OK, 0 rows affected (0.00 sec)

mysql> call proc_get_no_of_employees(@dept_name);


+----------+
| count(*) |
+----------+
| 1 |
+----------+

5.Create a procedure proc_delete_employee() to delete the employees matching the


given name. (insert dummy tuples and delete)

mysql> DELIMITER //
mysql> CREATE PROCEDURE proc_delete_employee(IN nameToDelete VARCHAR(100))
-> BEGIN
-> DELETE FROM employee_45 WHERE CONCAT(Fname, ' ', Lname) = nameToDelete;
-> END //
Query OK, 0 rows affected (0.02 sec)

mysql> DELIMITER ;
mysql> CALL proc_delete_employee('New Employee');
Query OK, 1 row affected (0.02 sec)

mysql> SELECT * FROM EMPLOYEE_45;


ROLL NO:2127220801112 PAGE NO:
+----------+-------+---------+-----------+------------+--------------------------+-
-----+----------+-----------+-----+
| FNAME | MINIT | LNAME | SSN | BDATE | address |
SEX | SALARY | SUPER_SSN | DNO |
+----------+-------+---------+-----------+------------+--------------------------+-
-----+----------+-----------+-----+
| John | B | Smith | 123456789 | 1965-01-09 | 731 Fondren, Houston, TX |
M | 33000.00 | 333445555 | 5 |
| Franklin | T | Wong | 333445555 | 1955-12-18 | 638 Voss,Houston,TX |
M | 44000.00 | 888665555 | 5 |
| joyce | a | english | 453453453 | 1972-07-31 | 5631 rice ,Houston,TX |
f | 27500.00 | 333445555 | 5 |
| ramesh | k | narayan | 666884444 | 1962-09-15 | 975 fire oak,Humble,TX |
M | 41800.00 | 333445555 | 5 |
| james | e | borg | 888665555 | 1937-11-10 | 450 stone,Houston,TX |
M | 60500.00 | null | 1 |
| jennifer | s | Wallace | 987654321 | 1941-06-20 | 291 berry,bellaire,TX |
f | 47300.00 | 888665555 | 4 |
| ahmed | v | jabber | 987987987 | 1969-03-29 | 980 dallas,Houston,TX |
M | 27500.00 | 987654321 | 4 |
| alicia | j | zelya | 999887777 | 1968-01-19 | 3321 castle,spring,TX |
f | 27500.00 | 987654321 | 4 |
+----------+-------+---------+-----------+------------+--------------------------+-
-----+----------+-----------+-----+
8 rows in set (0.00 sec)

6.Create a procedure proc_rollout_bonus() to increase the salary of all employees by a


given percentage

mysql> DELIMITER $$
mysql> CREATE PROCEDURE proc_rollout_bonus(IN in_percentage DECIMAL(5, 2))
-> BEGIN
-> UPDATE employee_45
-> SET Salary = Salary + (Salary * in_percentage / 100);
-> END $$
mysql> DELIMITER ;
mysql> CALL proc_rollout_bonus(10.0);
mysql> select salary from employee_45;
+----------+
| salary |
+----------+
| 33000.00 |
| 44000.00 |
| 27500.00 |
| 41800.00 |
| 60500.00 |
| 47300.00 |
| 27500.00 |
| 27500.00 |
+----------+
FUNCTION

1.Create a function fun_get_freshers() to return the number of employees joined this


year

mysql> CREATE FUNCTION fun_get_freshers() RETURNS INT DETERMINISTIC


-> BEGIN

ROLL NO:2127220801112 PAGE NO:


-> DECLARE current_year INT;
-> DECLARE fresher_count INT;
-> SET current_year = YEAR(NOW());
-> SELECT COUNT(*) INTO fresher_count
-> FROM employee_45
-> WHERE YEAR(Bdate) = current_year;
-> RETURN fresher_count;
-> END $$
mysql> DELIMITER ;
mysql> SELECT fun_get_freshers() AS freshers_count;
+----------------+
| freshers_count |
+----------------+
| 0 |
+----------------+

2.Create a function fun_get_subordinate_count() to return the number of employees


who work under the given mgr_ssn
mysql> DELIMITER &&
mysql> CREATE FUNCTION FUN_GET_SUBORDINATE_COUNT(MGRSSN CHAR(9))
-> RETURNS INT
-> DETERMINISTIC
-> BEGIN
-> DECLARE SUBORDINATE_COUNT INT;
-> SET SUBORDINATE_COUNT = (SELECT COUNT(*) FROM EMPLOYEE_45 WHERE SSN =
MGRSSN);
-> RETURN SUBORDINATE_COUNT;
-> END &&
mysql> DELIMITER ;
mysql> SET @result = FUN_GET_SUBORDINATE_COUNT('987654321');
mysql> SELECT @result AS SubordinateCount;
+------------------+
| SubordinateCount |
+------------------+
| 1 |
+------------------+

3.Create a function fun_get_no_of_employees() to return the number of employees in


the given department
mysql>DELIMITER &&
mysql>create FUNCTION FUN_GET_NO_OF_EMPLOYEES(IN_DEPT_NAME VARCHAR(15)) RETURNS
INT DETERMINISTIC
->BEGIN
->DECLARE EMPLOYEE_COUNT INT;
->SELECT COUNT(*) INTO EMPLOYEE_COUNT FROM EMPLOYEE_45 E,DEPARTMENT_45 D WHERE
E.DNO=D.DNUMBER AND DNAME=IN_DEPT_NAME GROUP BY DNAME;
->RETURN EMPLOYEE_COUNT;
->END &&
mysql> DELIMITER ;
mysql> SELECT FUN_GET_NO_OF_EMPLOYEES('RESERCH');
+------------------------------------+
| FUN_GET_NO_OF_EMPLOYEES('RESERCH') |
+------------------------------------+
| 4 |
+------------------------------------+

ROLL NO:2127220801112 PAGE NO:


4.Display the ssn of the manager of the given department number using a function.
mysql> CREATE FUNCTION fun_get_manager_ssn(in_department_number INT)
-> RETURNS CHAR(9) DETERMINISTIC
-> BEGIN
-> DECLARE manager_ssn CHAR(9);
-> SELECT e.SSN INTO manager_ssn
-> FROM employee_45 e
-> JOIN department_45 d ON e.SSN = d.Mgr_ssn
-> WHERE d.Dnumber = in_department_number;
-> RETURN manager_ssn;
-> END $$
mysql> DELIMITER ;
mysql> SELECT fun_get_manager_ssn(5) AS manager_ssn;
+-------------+
| manager_ssn |
+-------------+
| 333445555 |
+-------------+

RESULT:

ROLL NO:2127220801112 PAGE NO:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy