0% found this document useful (0 votes)
18 views31 pages

RDBMS PS-03

The document contains various PL/SQL queries and procedures related to RDBMS, including functions for generating Fibonacci series, converting strings to uppercase, and retrieving employee and student records based on specific criteria. It also includes procedures for handling student records in a specific city, displaying employee details department-wise, and managing employee job titles using parameterized cursors. Additionally, it addresses the creation of tables, insertion of data, and the implementation of triggers to prevent duplicate values in a department table.

Uploaded by

ry6492yadav
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)
18 views31 pages

RDBMS PS-03

The document contains various PL/SQL queries and procedures related to RDBMS, including functions for generating Fibonacci series, converting strings to uppercase, and retrieving employee and student records based on specific criteria. It also includes procedures for handling student records in a specific city, displaying employee details department-wise, and managing employee job titles using parameterized cursors. Additionally, it addresses the creation of tables, insertion of data, and the implementation of triggers to prevent duplicate values in a department table.

Uploaded by

ry6492yadav
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/ 31

CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS

DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

1. Write a PL/SQL function to display Fibonacci series for given no.


Query:-
declare
a number := 0;
b number := 1;
temp number;
n number := 6;
i number;
begin
dbms_output.put_line('fibonacci series is :');
dbms_output.put_line(a);
dbms_output.put_line(b);
for i in 2..n
loop
temp:= a + b;
a := b;
b := temp;
dbms_output.put_line(temp);
end loop;
end;
Output:-

2. Write PL/SQL functions that simulate upper function.


Query:-
CREATE OR REPLACE FUNCTION custom_upper(input_string IN VARCHAR2) RETURN VARCHAR2 IS

output_string VARCHAR2(4000);

BEGIN

1|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

-- Check for NULL input

IF input_string IS NULL THEN

RETURN NULL;

ELSE

-- Convert the input string to uppercase character by character

output_string := '';

FOR i IN 1..LENGTH(input_string) LOOP

output_string := output_string || UPPER(SUBSTR(input_string, i, 1));

END LOOP;

RETURN output_string;

END IF;

END custom_upper;

--For display the result

SELECT custom_upper('Hello, Rishabh') FROM DUAL;

3.Write procedure to get employee records based on particular employeeno.


Query:-
--Create a table of employee records

Create table Employee(

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

INTO Employee values(101,'Ashish',10000)

INTO Employee values(102,'Amit',20000)

INTO Employee values(103,'Raj',13000)

INTO Employee values(104,'Rohan',23000)

INTO Employee values(105,'Bachhi',30000)

Select * from dual;

Select * from employee;

-- Create a procedure to get employee records based on employee number

CREATE OR REPLACE PROCEDURE get_employee_info (employee_number_in IN NUMBER) IS

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

FOR employee_rec IN (SELECT employee_id, employee_name, employee_salary

FROM employee

WHERE employee_id = employee_number_in)

LOOP

v_employee_id := employee_rec.employee_id;

v_employee_name := employee_rec.employee_name;

v_employee_salary := employee_rec.employee_salary;

-- Display the employee records

DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id);

DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);

DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_employee_salary);

3|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

END LOOP;

-- Handle cases where the employee number is not found

IF SQL%NOTFOUND THEN

DBMS_OUTPUT.PUT_LINE('Employee with Employee Number ' || employee_number_in || ' not found.');

END IF;

END get_employee_info;

Output:-

-- call the procedure

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

Create table student(

Roll_no number(5) PRIMARY KEY,

Name varchar2(20),

Address varchar2(20),

City varchar2(20),

per number(5));

Insert all

INTO student values(3001,'Rishabh','Amroli','surat',78)

INTO student values(3002,'Jay','varachha','surat',70)

INTO student values(3003,'Jaimin','Ramapur','banglore',60)

INTO student values(3004,'Ashish','Mohannagar','mumbai',75)

INTO student values(3005,'Amit','Vesu','surat',77)

Select * from dual;

Select * from student;

--Creating a procedure

CREATE OR REPLACE PROCEDURE get_students_in_surat IS

BEGIN

-- Declare variables to store student information

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

-- Use a cursor to retrieve student records in Surat city

FOR student_rec IN (SELECT roll_no, name, address, city, per

5|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

FROM Student

WHERE city = 'Surat')

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;

-- Display the student records

DBMS_OUTPUT.PUT_LINE('Roll No: ' || v_rollno);

DBMS_OUTPUT.PUT_LINE('Name: ' || v_name);

DBMS_OUTPUT.PUT_LINE('Address: ' || v_address);

DBMS_OUTPUT.PUT_LINE('City: ' || v_city);

DBMS_OUTPUT.PUT_LINE('Percentage: ' || v_per);

END LOOP;

END;

END get_students_in_surat;

--calling the procedure

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

CREATE OR REPLACE PROCEDURE get_students(percentage_in IN NUMBER) IS

BEGIN

-- Declare variables to store student information

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

-- Use a cursor to retrieve student records with the specified percentage

FOR student_rec IN (SELECT roll_no, name, address, city, per

FROM Student

WHERE per = percentage_in)

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;

-- Display the student records

DBMS_OUTPUT.PUT_LINE('Roll No: ' || v_rollno);

DBMS_OUTPUT.PUT_LINE('Name: ' || v_name);

DBMS_OUTPUT.PUT_LINE('Address: ' || v_address);

DBMS_OUTPUT.PUT_LINE('City: ' || v_city);

DBMS_OUTPUT.PUT_LINE('Percentage: ' || v_per);

END LOOP;

7|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

END;

END get_students;

--calling the procedure

BEGIN

get_students(70); -- To get students with 70% percentage

END;

Output:-

5. To write a Cursor to display the list of Employees and Total Salary Department wise.
Query:-
-- Create the Department table

CREATE TABLE Department (

department_id NUMBER(10) PRIMARY KEY,

department_name VARCHAR2(100) NOT NULL

);

Insert ALL

INTO Department values(111,'CS')

INTO Department values(222,'IT')

INTO Department values(333,'DS')

INTO Department values(444,'IT')

select * from dual;


8|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

select * from department;

-- Create the Employee table

CREATE TABLE Employee (

employee_id NUMBER(10) PRIMARY KEY,

employee_name VARCHAR2(100) NOT NULL,

employee_salary NUMBER(10, 2) NOT NULL,

department_id NUMBER(10),

FOREIGN KEY (department_id) REFERENCES Department(department_id)

);

Insert ALL

INTO Employee values(101,'Rishabh',1000,111)

INTO Employee values(102,'Rohit',800,222)

INTO Employee values(103,'Ram',1100,333)

INTO Employee values(104,'Amit',900,444)

select * from dual;

Select * from Employee;

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

SELECT d.department_id, d.department_name, SUM(e.employee_salary) AS total_salary

FROM Department d

LEFT JOIN Employee e ON d.department_id = e.department_id

9|Page
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

GROUP BY d.department_id, d.department_name;

BEGIN

OPEN department_cursor;

LOOP

FETCH department_cursor INTO v_department_id, v_department_name, v_total_salary;

EXIT WHEN department_cursor%NOTFOUND;

-- Display department-wise employee details

DBMS_OUTPUT.PUT_LINE('Department: ' || v_department_name);

DBMS_OUTPUT.PUT_LINE('Total Salary: ' || v_total_salary);

-- Fetch and display employees within the department

FOR emp_rec IN (SELECT employee_id, employee_name, employee_salary

FROM Employee

WHERE department_id = v_department_id)

LOOP

DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_rec.employee_id);

DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_rec.employee_name);

DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_rec.employee_salary);

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

create table Employee(

Employee_id number(5),

Employee_name varchar2(20),

job_title varchar2(20));

Insert All

into Employee values(11,'Hari','Manager')

11 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

into Employee values(22,'Ram','HR')

into Employee values(33,'Shyam','Analyst')

Select * from dual;

--Cursor creationg

DECLARE

CURSOR employee_cursor (job_title_param VARCHAR2) IS

SELECT employee_id, employee_name, job_title

FROM Employee

WHERE job_title = job_title_param;

v_employee_id Employee.employee_id%TYPE;

v_employee_name Employee.employee_name%TYPE;

v_job_title Employee.job_title%TYPE;

BEGIN

-- Open the cursor

OPEN employee_cursor('Manager');

-- Fetch and display employees with the specified job title

LOOP

FETCH employee_cursor INTO

v_employee_id,

v_employee_name,

v_job_title;

EXIT WHEN employee_cursor%NOTFOUND;

DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id);

DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);

DBMS_OUTPUT.PUT_LINE('Job Title: ' || 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 the cursor

CLOSE employee_cursor;

END;

Output:-

7. To write a Cursor to find employee with given job and deptno. (Parameterized cursor).
Query:-
select * from Employee;

Alter table Employee add department_id number(5);

update Employee set Department_id=333 where employee_id=33;

--Creating cursor with job and dept parameter

DECLARE

CURSOR employee_cursor (job_param VARCHAR2, deptno_param NUMBER) IS

SELECT employee_id, employee_name, job_title, department_id

FROM Employee

WHERE job_title = job_param AND department_id = deptno_param;

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

-- Open the cursor with specific job and department number

13 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

OPEN employee_cursor('Analyst', 333);

-- Fetch and display employees matching the criteria

LOOP

FETCH employee_cursor INTO

v_employee_id,

v_employee_name,

v_job_title,

v_department_id;

EXIT WHEN employee_cursor%NOTFOUND;

DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id);

DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);

DBMS_OUTPUT.PUT_LINE('Job Title: ' || v_job_title);

DBMS_OUTPUT.PUT_LINE('Department ID: ' || v_department_id);

END LOOP;

-- Close the cursor

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

Create table Emp(

emp_id number(10),

emp_name varchar2(100),

emp_salary number(10));

Insert all

INTO Emp values(101,'Ashish',10000)

INTO Emp values(102,'Amit',20000)

INTO Emp values(103,'Raj',13000)

INTO Emp values(104,'Rohan',23000)

INTO Emp values(105,'Bachhi',30000)

Select * from dual;

-- Declare a cursor

DECLARE

CURSOR emp_cursor IS

SELECT emp_id, emp_name, emp_salary

FROM Emp;

-- Declare variables to store employee information

emp_id Emp.emp_id%TYPE;

emp_name Emp.emp_name%TYPE;

emp_salary Emp.emp_salary%TYPE;

BEGIN

-- Open the cursor

OPEN emp_cursor;

-- Fetch and display employee information

LOOP

FETCH emp_cursor INTO emp_id, emp_name, emp_salary;

15 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

EXIT WHEN emp_cursor%NOTFOUND;

DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id);

DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);

DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary);

DBMS_OUTPUT.PUT_LINE('---------------------');

END LOOP;

-- Close the cursor

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

CREATE TABLE DEPT (

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

CREATE OR REPLACE TRIGGER prevent_duplicate_null_deptno

BEFORE INSERT OR UPDATE ON DEPT

FOR EACH ROW

DECLARE

v_null_count NUMBER;

BEGIN

-- Count the number of rows with NULL in DEPTNO for the new or updated data

SELECT COUNT(*) INTO v_null_count

FROM DEPT

WHERE DEPTNO IS NULL;

-- If there is more than one row with NULL DEPTNO, raise an exception

IF v_null_count > 1 THEN

RAISE_APPLICATION_ERROR(-20001, 'Cannot have duplicate NULL values in DEPTNO column.');

END IF;

END;

Output:-

INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES (NULL, 'Marketing', 'South Africa');

select * from dept;

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 some sample data into the table

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

-- Declare variables to hold department and total salary

v_deptno EMP.deptno%TYPE;

v_total_salary NUMBER;

v_emp_name EMP.name%TYPE;

v_emp_salary EMP.salary%TYPE;

BEGIN

-- Open the outer cursor

FOR dept_rec IN (SELECT DISTINCT deptno FROM EMP) LOOP

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;

-- Declare the inner cursor to fetch employees within a department

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;

-- Accumulate the total salary for the department

v_total_salary := v_total_salary + v_emp_salary;

END LOOP;

-- Display the department and total salary

DBMS_OUTPUT.PUT_LINE('Department ' || v_deptno || ': Total Salary = ' || v_total_salary);

-- Reset the total salary for the next department

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 (

empno NUMBER, -- Employee number

salary NUMBER -- Employee salary

);

-- Insert some sample data into the table

INSERT INTO EMP (empno, salary) VALUES (101, 9500);

INSERT INTO EMP (empno, salary) VALUES (102, 10500);

INSERT INTO EMP (empno, salary) VALUES (103, 11500);

--pl/sql block

DECLARE

v_empno EMP.empno%TYPE;

v_salary EMP.salary%TYPE;

BEGIN

-- Accept employee number from the user

v_empno := :emp_no; -- You can use the substitution variable &emp_no to input the employee number.

-- Check if the employee exists and fetch the salary

SELECT salary INTO v_salary

FROM EMP

WHERE empno = v_empno;

IF v_salary < 10000 THEN

-- Deduct Rs. 2000 from the salary

v_salary := v_salary - 2000;

-- Update the salary in the EMP table

UPDATE EMP

SET salary = v_salary

WHERE empno = v_empno;

21 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

DBMS_OUTPUT.PUT_LINE('Salary is less than 10000. Rs. 2000 deducted.');

ELSE

DBMS_OUTPUT.PUT_LINE('Salary is not less than 10000.');

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

12% for assistant professors

5% for visiting faculties

Read records from employee table and print payroll report. The output format as under:

College budget for the year 2006-2007:

Designation No. of Employees Salary-Increases


Professors 999 999999.99
Assistant Professors 999 999999.99
System Analyst 999 999999.99
Visiting Faculties 999 999999.99
Total 9999 9999999.99

Query:-
-- Create the employee table

CREATE TABLE employee (


22 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

emp_id NUMBER,

designation VARCHAR2(50),

salary NUMBER

);

-- Insert sample data into the employee table

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

-- Create a PL/SQL block

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

-- Loop through the employee records and calculate salary increases

FOR emp IN (SELECT * FROM employee) LOOP

IF emp.designation = 'Professors' THEN

v_prof_salary_increase := v_prof_salary_increase + (0.20 * emp.salary);

ELSIF emp.designation = 'Assistant Professors' THEN

v_asst_prof_salary_increase := v_asst_prof_salary_increase + (0.12 * emp.salary);

ELSIF emp.designation = 'System Analyst' THEN

23 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

v_sys_analyst_salary_increase := v_sys_analyst_salary_increase + (0.05 * emp.salary);

ELSIF emp.designation = 'Visiting Faculties' THEN

v_visiting_salary_increase := v_visiting_salary_increase + (0.05 * emp.salary);

END IF;

END LOOP;

-- Calculate the total budget and total salary increase

v_budget := v_budget + v_prof_salary_increase + v_asst_prof_salary_increase + v_sys_analyst_salary_increase +


v_visiting_salary_increase;

v_total_salary_increase := v_prof_salary_increase + v_asst_prof_salary_increase + v_sys_analyst_salary_increase +


v_visiting_salary_increase;

-- Print the payroll report

DBMS_OUTPUT.PUT_LINE('College budget for the year 2006-2007:');

DBMS_OUTPUT.PUT_LINE('Designation No. of Employees Salary-Increases');

DBMS_OUTPUT.PUT_LINE('Professors ' || v_prof_salary_increase || ' ' || v_prof_salary_increase);

DBMS_OUTPUT.PUT_LINE('Assistant Professors ' || v_asst_prof_salary_increase || ' ' ||


v_asst_prof_salary_increase);

DBMS_OUTPUT.PUT_LINE('System Analyst ' || v_sys_analyst_salary_increase || ' ' ||


v_sys_analyst_salary_increase);

DBMS_OUTPUT.PUT_LINE('Visiting Faculties ' || v_visiting_salary_increase || ' ' || v_visiting_salary_increase);

DBMS_OUTPUT.PUT_LINE('Total ' || v_total_salary_increase || ' ' || v_total_salary_increase);

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

CREATE TABLE EMP (

emp_id NUMBER PRIMARY KEY,

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

CREATE OR REPLACE TRIGGER prevent_office_hours_changes

BEFORE INSERT OR UPDATE OR DELETE ON EMP

FOR EACH ROW

DECLARE

v_current_day VARCHAR2(10);

v_current_time TIMESTAMP;

BEGIN

-- Get the current day of the week

25 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

v_current_day := TO_CHAR(SYSDATE, 'DY');

-- Get the current time

v_current_time := TO_TIMESTAMP(TO_CHAR(SYSDATE, 'HH24:MI:SS'), 'HH24:MI:SS');

-- Check if it's a day within office hours (Mon to Sat, 9:00 AM to 5:00 PM)

IF v_current_day IN ('MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT') AND

v_current_time >= TO_TIMESTAMP('09:00:00', 'HH24:MI:SS') AND

v_current_time <= TO_TIMESTAMP('17:00:00', 'HH24:MI:SS') THEN

-- Raise an exception to prevent the change

RAISE_APPLICATION_ERROR(-20001, 'Data changes are not allowed during office hours.');

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

select * from emp;

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

-- Create the EMP table

CREATE TABLE EMP (

emp_id NUMBER PRIMARY KEY,

emp_name VARCHAR2(50),

emp_salary NUMBER

);

-- Insert sample data into the EMP table

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

-- Create the update_employee_salary trigger

CREATE OR REPLACE TRIGGER update_employee_salary

BEFORE INSERT OR UPDATE ON EMP

FOR EACH ROW

BEGIN

-- Check if the new salary is less than 5000

IF :NEW.emp_salary < 5000 THEN

:NEW.emp_salary := 10000; -- Update salary to 10,000

ELSIF :NEW.emp_salary >= 5000 THEN

:NEW.emp_salary := 15000; -- Update salary to 15,000

END IF;

END;

--insert data acording to trigger

insert into emp values(444,'jay',5000);

insert into emp values(555,'jaimin',3000);

select * from emp;

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

CREATE TABLE PERSONINFO (

empno NUMBER PRIMARY KEY,

name VARCHAR2(50),

age NUMBER

);

-- Create the trigger

CREATE OR REPLACE TRIGGER restrict_delete_update

BEFORE DELETE OR UPDATE ON PERSONINFO

FOR EACH ROW

BEGIN

-- Check if the operation is attempted on a Thursday

IF TO_CHAR(SYSDATE, 'DY') = 'THU' THEN

-- Allow insertions on Thursdays

NULL;

ELSE

-- Raise an exception to prohibit delete and update

RAISE_APPLICATION_ERROR(-20001, 'Delete and update operations are not allowed.');

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

Select * from personinfo;

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

Create table Emp(

Eno number(5) PRIMARY KEY,

name varchar(10),

salary number(5));

Insert all

INTO Emp values(101,'Ashish',10000)

INTO Emp values(102,'Amit',20000)

INTO Emp values(103,'Raj',13000)

INTO Emp values(104,'Rohan',23000)

INTO Emp values(105,'Bachhi',30000)

INTO Emp values(106,'Ram',12000)

Select * from dual;

29 | P a g e
CLASS : - SY BSC(IT) SEM-3 SUBJECT : - RDBMS
DATE : - 5/11/2023 ENROLMENT NO:-2202040601109

Select * from emp;

-- Create a stored procedure to display the top 5 highest salaries along with names

CREATE OR REPLACE PROCEDURE GetTop5HighestSalaries_V2

IS

BEGIN

FOR r IN (SELECT name, salary

FROM (

SELECT name, salary

FROM Emp

ORDER BY salary DESC

WHERE ROWNUM <= 5)

LOOP

DBMS_OUTPUT.PUT_LINE('Name: ' || r.name || ', Salary: ' || r.salary);

END LOOP;

END;

--call the procedure

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

You might also like