RDBMS PS-03
RDBMS PS-03
output_string VARCHAR2(4000);
BEGIN
1|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
RETURN NULL;
ELSE
output_string := '';
END LOOP;
RETURN output_string;
END IF;
END custom_upper;
employee_id number(10),
employee_name varchar2(100),
2|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
employee_salary number(10));
Insert all
v_employee_id NUMBER;
v_employee_name VARCHAR2(100);
v_employee_salary NUMBER;
BEGIN
-- Use a cursor to retrieve employee records based on the provided employee number
FROM employee
LOOP
v_employee_id := employee_rec.employee_id;
v_employee_name := employee_rec.employee_name;
v_employee_salary := employee_rec.employee_salary;
3|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
END LOOP;
IF SQL%NOTFOUND THEN
END IF;
END get_employee_info;
Output:-
BEGIN
get_employee_info(101);
END;
Output:-
4. Consider the following table. Student (rollno, primary key, name, address, city, per)
4|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
A. Write procedure to access all records of students who are stay in surat city.
Query:-
--Create a student table
Name varchar2(20),
Address varchar2(20),
City varchar2(20),
per number(5));
Insert all
--Creating a procedure
BEGIN
DECLARE
v_rollno Student.roll_no%TYPE;
v_name Student.name%TYPE;
v_address Student.address%TYPE;
v_city Student.city%TYPE;
v_per Student.per%TYPE;
BEGIN
5|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
FROM Student
LOOP
v_rollno := student_rec.roll_no;
v_name := student_rec.name;
v_address := student_rec.address;
v_city := student_rec.city;
v_per := student_rec.per;
END LOOP;
END;
END get_students_in_surat;
BEGIN
get_students_in_surat;
END;
Output:
6|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
B. Write procedure to display all records of student who are getting 70 per. (Procedure overloading)
Query:-
-- Creating a Procedure
BEGIN
DECLARE
v_rollno Student.roll_no%TYPE;
v_name Student.name%TYPE;
v_address Student.address%TYPE;
v_city Student.city%TYPE;
v_per Student.per%TYPE;
BEGIN
FROM Student
LOOP
v_rollno := student_rec.roll_no;
v_name := student_rec.name;
v_address := student_rec.address;
v_city := student_rec.city;
v_per := student_rec.per;
END LOOP;
7|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
END;
END get_students;
BEGIN
END;
Output:-
5. To write a Cursor to display the list of Employees and Total Salary Department wise.
Query:-
-- Create the Department table
);
Insert ALL
department_id NUMBER(10),
);
Insert ALL
--create a cursor
DECLARE
v_department_id Employee.department_id%TYPE;
v_department_name Department.department_name%TYPE;
v_total_salary NUMBER;
CURSOR department_cursor IS
FROM Department d
9|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
BEGIN
OPEN department_cursor;
LOOP
FROM Employee
LOOP
END LOOP;
END LOOP;
CLOSE department_cursor;
END;
Output:-
10 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
6. To write a Cursor to display the list of employees who are working as a Managers or
Analyst.(Parameterized cursor).
Query:-
--create table of employee
Employee_id number(5),
Employee_name varchar2(20),
job_title varchar2(20));
Insert All
11 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
--Cursor creationg
DECLARE
FROM Employee
v_employee_id Employee.employee_id%TYPE;
v_employee_name Employee.employee_name%TYPE;
v_job_title Employee.job_title%TYPE;
BEGIN
OPEN employee_cursor('Manager');
LOOP
v_employee_id,
v_employee_name,
v_job_title;
END LOOP;
12 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
CLOSE employee_cursor;
END;
Output:-
7. To write a Cursor to find employee with given job and deptno. (Parameterized cursor).
Query:-
select * from Employee;
DECLARE
FROM Employee
v_employee_id Employee.employee_id%TYPE;
v_employee_name Employee.employee_name%TYPE;
v_job_title Employee.job_title%TYPE;
v_department_id Employee.department_id%TYPE;
BEGIN
13 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
LOOP
v_employee_id,
v_employee_name,
v_job_title,
v_department_id;
END LOOP;
CLOSE employee_cursor;
END;
Output:-
8. To write a Cursor to display List of Employees from Emp Table in PL/SQL block.
Query:-
--creating a Emp table
14 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
emp_id number(10),
emp_name varchar2(100),
emp_salary number(10));
Insert all
-- Declare a cursor
DECLARE
CURSOR emp_cursor IS
FROM Emp;
emp_id Emp.emp_id%TYPE;
emp_name Emp.emp_name%TYPE;
emp_salary Emp.emp_salary%TYPE;
BEGIN
OPEN emp_cursor;
LOOP
15 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
DBMS_OUTPUT.PUT_LINE('---------------------');
END LOOP;
CLOSE emp_cursor;
END;
Output:-
16 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
9. To write a TRIGGER to ensure that DEPT TABLE does not contain duplicate of Null values in DEPTNO
column.
Query:-
-- Create the DEPT table
DEPTNO NUMBER,
DNAME VARCHAR2(50),
LOC VARCHAR2(50)
);
INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES (101, 'HR', 'New York');
INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES (102, 'IT', 'India');
17 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES (103, 'Finance', 'USA');
--creating trigger
DECLARE
v_null_count NUMBER;
BEGIN
-- Count the number of rows with NULL in DEPTNO for the new or updated data
FROM DEPT
-- If there is more than one row with NULL DEPTNO, raise an exception
END IF;
END;
Output:-
INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES (NULL, 'Marketing', 'South Africa');
Output:-
18 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
10.To write a Cursor to display the list of Employees and Total Salary Department wise(Using nested
cursor).
Query:-
CREATE TABLE EMP (
empno NUMBER,
name VARCHAR2(50),
salary NUMBER,
deptno NUMBER
);
INSERT INTO EMP (empno, name, salary, deptno) VALUES (111, 'Rohan', 5000, 101);
INSERT INTO EMP (empno, name, salary, deptno) VALUES (112, 'Rishabh', 7500, 102);
--create cursor
DECLARE
v_deptno EMP.deptno%TYPE;
v_total_salary NUMBER;
v_emp_name EMP.name%TYPE;
v_emp_salary EMP.salary%TYPE;
BEGIN
19 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
v_deptno := dept_rec.deptno;
FOR emp_rec IN (SELECT name, salary FROM EMP WHERE deptno = v_deptno) LOOP
v_emp_name := emp_rec.name;
v_emp_salary := emp_rec.salary;
END LOOP;
v_total_salary := 0;
END LOOP;
END;
Output:-
11.Write a PL/SQL block that will accept an employee no from the user and deduct a salary by Rs.2000
from the input employee no if employee has a salary less then 1000 after salary is deducted it display a
message “SALARY IS LESS THEN 10000”. Table: EMP (empno, name, salary).
20 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
Query:-
CREATE TABLE EMP (
);
--pl/sql block
DECLARE
v_empno EMP.empno%TYPE;
v_salary EMP.salary%TYPE;
BEGIN
v_empno := :emp_no; -- You can use the substitution variable &emp_no to input the employee number.
FROM EMP
UPDATE EMP
21 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
ELSE
END IF;
END;
Output:-
12.Write a PL/SQL code to determine the overall effect on a college budget employees are given salary
increase as follows :
20% for professors
Read records from employee table and print payroll report. The output format as under:
Query:-
-- Create the employee table
emp_id NUMBER,
designation VARCHAR2(50),
salary NUMBER
);
INSERT INTO employee (emp_id, designation, salary) VALUES (1, 'Professors', 80000);
INSERT INTO employee (emp_id, designation, salary) VALUES (2, 'Assistant Professors', 60000);
INSERT INTO employee (emp_id, designation, salary) VALUES (3, 'System Analyst', 70000);
INSERT INTO employee (emp_id, designation, salary) VALUES (4, 'Visiting Faculties', 50000);
DECLARE
-- Declare variables to store the budget, number of employees, and total salary increases
v_budget NUMBER := 0;
v_prof_salary_increase NUMBER := 0;
v_asst_prof_salary_increase NUMBER := 0;
v_sys_analyst_salary_increase NUMBER := 0;
v_visiting_salary_increase NUMBER := 0;
v_total_salary_increase NUMBER := 0;
BEGIN
23 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
END IF;
END LOOP;
END;
Output:-
24 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
13.Write a trigger that will not allow changing EMP table data. After office hour i.e. from Monday to
Saturday (from 9:00 A.M. to 5:00 P.M.) .There is no restriction on viewing the data.
Query:-
-- Create the EMP table
emp_name VARCHAR2(50),
emp_salary NUMBER
);
INSERT INTO EMP (emp_id, emp_name, emp_salary) VALUES (1, 'Ashish', 60000);
INSERT INTO EMP (emp_id, emp_name, emp_salary) VALUES (2, 'Amit', 70000);
INSERT INTO EMP (emp_id, emp_name, emp_salary) VALUES (3, 'jay', 80000);
--create trigger
DECLARE
v_current_day VARCHAR2(10);
v_current_time TIMESTAMP;
BEGIN
25 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
-- Check if it's a day within office hours (Mon to Sat, 9:00 AM to 5:00 PM)
END IF;
END;
-- Insert sample data into the EMP table (outside office hours, to avoid trigger exceptions)
INSERT INTO EMP (emp_id, emp_name, emp_salary) VALUES (4, 'Rohan', 90000);
Output:-
14.Write a trigger to update salary of employee in EMP table based on the entered salary. If inputted
salary is less then 5000 then update salary 10000.if entered salary is greater than or equal to 5000 then
update salary 15000.
Query:-
26 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
emp_name VARCHAR2(50),
emp_salary NUMBER
);
INSERT INTO EMP (emp_id, emp_name, emp_salary) VALUES (111, 'Rishabh', 4500);
INSERT INTO EMP (emp_id, emp_name, emp_salary) VALUES (222, 'bachhi', 6000);
INSERT INTO EMP (emp_id, emp_name, emp_salary) VALUES (333, 'Raj', 7500);
BEGIN
END IF;
END;
Output:-
27 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
15.PERSONINFO (empno number, name varchar2, age number) Write a trigger which is prohibit delete
and update operation to user. But allow insertion on Thursday.
Query:-
-- Create the PERSONINFO table
name VARCHAR2(50),
age NUMBER
);
BEGIN
NULL;
ELSE
END IF;
28 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
END;
-- Insert sample data into the PERSONINFO table (on a Thursday to allow insertion)
INSERT INTO PERSONINFO (empno, name, age) VALUES (1, 'Jaimin', 30);
INSERT INTO PERSONINFO (empno, name, age) VALUES (2, 'Amit', 25);
Output:-
16.Emp (eno PK, name, salary) Write a procedure to display top 5 highest getting records of Emp along
with their name.
Query:
--create emp table
name varchar(10),
salary number(5));
Insert all
29 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
-- Create a stored procedure to display the top 5 highest salaries along with names
IS
BEGIN
FROM (
FROM Emp
LOOP
END LOOP;
END;
Begin
GetTop5HighestSalaries_V2();
End;
Output:-
30 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109
31 | P a g e