DBMS NEP Lab Manual
DBMS NEP Lab Manual
DBMS NEP Lab Manual
Words of Concern
Point to be As Allan Bloom has said "Education is the movement from darkness to light".
Through this handbook, I have tried to illuminate what might otherwise appear as black
boxes to some. In doing so, I have used references from several other authors to synthesize
or simplify or elaborate information. This is not possible without omitting details that I deem
trivial while dilating the data that I consider relevant to topic. Every effort has been made to
avoid errors. Inspite of this, some errors might have crept in. Any errors or discrepancies
noted maybe brought to my notice which I shall rectify in my next revision.
This handbook is solely for educational purpose and is not for sale. This handbook shall not
be reproduced or distributed or used for commercial purposes in any form or by any means.
Thanks,
Umme Habiba V S
3rd Semester
BCA
Interface College of Computer Applications (ICCA)
Davangere
Bibliography
Syllabus
Course Credits: 02
Formative Assessment Marks: 25
Duration of SEE/Exam: 03 Hours
Summative Assessment Marks: 25
Student would be able to create tables, execute queries and PL/SQL programs.
Part A
Create a table called Employee with the following structure.
Name Type
Empno Number
Ename Varchar2(20)
Job Varchar2(20)
Mgr Number
Salary Number
Add a column commission with domain to the Employee table.
Insert any five records into the table.
Update the column details of job
Rename the column of Employ table using alter command.
Delete the employee whose empno is 19.
Name Type
Branch name varchar2(20)
Branchcity varchar2(20)
Asserts Number
Increase the size of data type for asserts to the branch.
Add and drop a column to the branchtable.
Insert values to thetable.
Update the branch namecolumn
Delete any two columns from thetable
Delete the row of the table with somecondition.
Show the average salary for all departments employing more than
threepeople.
Display employees who earn more than the lowest salary in department30
Show that value returned by sign (n)function.
How many days between day of birth to currentdate.
a. Show that two substring as singlestring.
List all employee names, salary and 15% rise insalary.
Display lowest paid emp details under eachmanager
Display the average monthly salary bill for eachdeptno.
Show the average salary for all departments employing more than
twopeople.
By using the group by clause, display the eid who belongs to deptno 05
along with averagesalary.
worldHello).
b. Write a PL/SQL program to accept a number and a divisor. Make sure
the divisor is less than or equal to 10. Else display an error message.
Otherwise Display the remainder in words.
CURSORS
Write a PL/SQL block that will display the name, dept no, salary of fist
highest paidemployees.
Write a PL/SQL block that will display the employee details along with
salary usingcursors.
To write a Cursor to display the list of employees who are working as a
Managers orAnalyst.
To write a Cursor to find employee with given job anddeptno.
Write a PL/SQL block using implicit cursor that will display message, the
salaries of all the employeesinthe„employee‟ tableareupdated.If noneofthe
employee‟ssalaryareupdatedwegeta message 'None of the salaries were
updated'. Else we get a message like for example, 'Salariesfor
1000employees areupdated'if thereare1000rowsin„employee‟table.
Note: Student has to execute a minimum of 10 programs in each part to complete the Lab
course
PART-A
Name Type
Empno Number
Ename Varchar2(10)
Job Varchar2(10)
Mgr Number
Sal Number
a. Add a column commission with domain to the Employee table.
b. Insert any five records into the table.
c. Update the column details of job
d. Rename the column of Employ table using alter command.
e. Delete the employee whose Empno is 19.
TABLE CREATION
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
NAME TYPE
Deptno Number
Deptname Varchar2(10)
location Varchar2(10)
TABLE CREATION:
CREATE TABLE DEPARTMENT
( DEPT_NO NUMBER,
DEPT_NAME VARCHAR2(20),
LOCATION VARCHAR2(20)
);
OUTPUT
OUTPUT
OUTPUT
SELECT COUNT(*) AS
NO_OF_EMPLOYEE,DEPARTMENT.DEPT_NAME,DEPARTMENT.DEPT_NO
FROM EMPLOYEE
INNER JOIN DEPARTMENT ON EMPLOYEE.DEPT_NO = DEPARTMENT.DEPT_NO
GROUP BY DEPARTMENT.DEPT_NAME,DEPARTMENT.DEPT_NO
OUTPUT
OUTPUT
OUTPUT
TABLE CREATION:
CREATE TABLE CUSTOMER
(
CUST_NAME VARCHAR2(30),
CUST_STREET VARCHAR2(20),
CUST_CITY VARCHAR2(20)
);
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
Name Type
Branch Name Varchar2(20)
Branch city Varchar2(20)
Asserts Number
TABLE CREATION:
OUTPUT
OUTPUT
OUTPUT
DROP:
ALTER TABLE BRANCH_TABLE
DROP COLUMN YEAR;
OUTPUT
OUTPUT
OUTPUT
OUTPUT
b. Insert the any three records in the employee table and use rollback. Check the result.
OUTPUT
INSERT:
OUTPUT
ROLLBACK:
OUTPUT
c. Add primary key constraint and not null constraint to the employee table.
d. Insert null values to the employee table and verify the result.
OUTPUT
OUTPUT
INSERT:
OUTPUT
COMMIT:
OUTPUT
c. Add constraints like unique and not null to the department table.
OUTPUT
INSERT:
OUTPUT
COMMIT:
OUTPUT
c. Delete any three records in the department table and use rollback.
OUTPUT
OUTPUT
8)
a) By using the group by clause, display the enames who belongs to deptno 10 along with
average salary.
b) Display lowest paid employee details under each department.
c) Display number of employees working in each department and their department number.
d) Using built in functions, display number of employees working in each department and
their department name from dept table. Insert deptname to dept table and insert deptname
for each row, do the required thing specified above.
e) List all employees which start with either B or C.
f) Display only these ename of employees where the maximum salary is greater than or
equal to 5000.
a) By using the group by clause, display the enames who belongs to deptno 10 along with
average salary.
OUTPUT
OUTPUT
c) Display number of employees working in each department and their department number.
SELECT COUNT(*) AS
NO_OF_EMPLOYEE,DEPARTMENT.DEPT_NAME,DEPARTMENT.DEPT_NO
FROM EMPLOYEE
INNER JOIN DEPARTMENT ON EMPLOYEE.DEPT_NO = DEPARTMENT.DEPT_NO
GROUP BY DEPARTMENT.DEPT_NAME,DEPARTMENT.DEPT_NO
OUTPUT
d) Using built in functions, display number of employees working in each department and
their department name from dept table. Insert deptname to dept table and insert deptname
for each row, do the required thing specified above.
SELECT COUNT(*) AS
NO_OF_EMPLOYEE,DEPARTMENT.DEPT_NAME,DEPARTMENT.DEPT_NO
FROM EMPLOYEE
INNER JOIN DEPARTMENT ON EMPLOYEE.DEPT_NO = DEPARTMENT.DEPT_NO
GROUP BY DEPARTMENT.DEPT_NAME,DEPARTMENT.DEPT_NO
OUTPUT
SELECT e_name FROM employee WHERE e_name IN(SELECT e_name FROM employee
WHERE e_name LIKE 'B%' or e_name LIKE 'C%');
OUTPUT
f) Display only these ename of employees where the maximum salary is greater than or
equal to 5000.
9)
a) Calculate the average salary for each different job.
b) Show the average salary of each job excluding manager.
c) Show the average salary for all departments employing more than three people.
d) Display employees who earn more than the lowest salary in department 30.
e) Show that value returned by sign (n) function.
f) How many days between day of birth to current date.
OUTPUT
OUTPUT
Show the average salary for all departments employing more than three people.
OUTPUT
Display employees who earn more than the lowest salary in department 30.
SELECT e_name, salary, dept_no
FROM employee
WHERE salary >
(SELECT MIN(salary) FROM employee
where dept_no = 10 GROUP BY dept_no );
OUTPUT
OUTPUT
OUTPUT
10)
a) Show that two substring as single string.
b) List all employee names, salary and 15% rise in salary.
c) Display lowest paid emp details under each manager.
d) Display the average monthly salary bill for each deptno.
e) Show the average salary for all departments employing more than two people.
f) By using the group by clause, display the eid who belongs to deptno 05 along with
average salary.
OUTPUT
OUTPUT
OUTPUT
GROUP BY department.dept_name,department.dept_no;
OUTPUT
e) Show the average salary for all departments employing more than two people.
SELECT count(*) as count,avg(salary),department.dept_no,department.dept_name
FROM employee
inner join department on employee.dept_no = department.dept_no
GROUP BY department.dept_name,department.dept_no
HAVING COUNT(*) >2;
OUTPUT
g) By using the group by clause, display the eid who belongs to deptno 05 along with
average salary.
11)
a) Count the number of employees in department 20
b) Find the minimum salary earned by clerk.
c) Find minimum, maximum, average salary of all employees.
d) List the minimum and maximum salaries for each job type.
e) List the employee names in descending order.
f) List the employee id, names in ascending order by empid.
g) Delete any three records in the department table and use rollback.
h) Add constraint primary key and foreign key to the table.
OUTPUT
OUTPUT
OUTPUT
d) List the minimum and maximum salaries for each job type.
SELECT JOB,MIN(SALARY),MAX(SALARY) FROM EMPLOYEE GROUP BY JOB
OUTPUT
SELECT E.E_NAME
FROM EMPLOYEE E
ORDER BY E.E_NAME DESC ;
OUTPUT
OUTPUT
g) Delete any three records in the department table and use rollback.
OUTPUT
OUTPUT
12)
a. Create a user and grant all permissions to the user.
b. Use revoke command to remove user permissions.
c. Change password of the user created.
d. Add constraint foreign key and not null.
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
13)
a. Create a user and grant all permissions to the user.
b. Update the table reserves and use savepoint and rollback.
c. Add constraint primary key, foreign key and not null to the reserves table.
d. Delete constraint not null to the table column.
PART-B
PROGRAMS ON PL/SQL
OUTPUT
DECLARE
a number:= 4;
b number:= 6;
c number:= 2;
BEGIN
dbms_output.put_line('a: '||a||', b: '||b||', c: '||c);
IF (a > b AND a > c) THEN
dbms_output.put_line ('Greatest number is ' ||a);
ELSIF (b > a AND b > c) THEN
dbms_output.put_line ('Greatest number is '||b);
ELSE
dbms_output.put_line ('Greatest number is '||c);
END IF;
END;
OUTPUT
a. Write a PL\SQL program to find the total and average of 6 subjects and display the grade.
DECLARE
mark1 number:=56;
mark2 number:=80;
mark3 number:=59;
mark4 number:=90;
mark5 number:=81;
mark6 number:=70;
score number;
average number;
grade Char(1);
BEGIN
OUTPUT
n INTEGER;
temp_sum INTEGER;
rem INTEGER;
BEGIN
n := 123456;
temp_sum := 0;
dbms_output.put_line('Number: '||n);
WHILE n <> 0 LOOP
rem := MOD(n, 10);
temp_sum := temp_sum + rem;
n := Trunc(n / 10);
END LOOP;
dbms_output.put_line('sum of digits = '|| temp_sum);
END;
OUTPUT
a INTEGER;
rev INTEGER;
rem INTEGER;
BEGIN
a := 78924;
rev := 0;
dbms_output.put_line('Number: '||a);
END LOOP;
END;
OUTPUT
n NUMBER;
i NUMBER;
temp NUMBER;
BEGIN
n:= &n;
temp:= 1;
IF MOD(n, i) = 0 THEN
temp:= 0;
EXIT;
END IF;
END LOOP;
IF temp = 1 THEN
dbms_output.put_line ('yes,'||n||' it is prime number');
ELSE
dbms_output.put_line (n||' is not a prime number');
END IF;
END;
OUTPUT
OUTPUT
b. Write a PL\SQL code blocks to calculate the area of a circle for a value of radius varying
from 3 to 7. Store the radius and the corresponding values of calculated area in an empty
table named area, consisting of two columns radius and area.
PROGRAM:
DECLARE
AREA FLOAT;
RADIUS NUMBER;
BEGIN
RADIUS:= &RADIUS;
AREA := 3.14 * RADIUS *RADIUS;
dbms_output.put_line('Radius: '||RADIUS);
dbms_output.put_line('Area of circle: '||AREA);
INSERT INTO AREAS VALUES(RADIUS,AREA);
END;
OUTPUT
a. Write a PL/SQL program to accept a string and remove the vowels from the string.
(When, "Hello" is passed to the program it should display "HLL" removing "e" and "o" from
the word "Hello")
DECLARE
name varchar(30);
temp varchar(30);
BEGIN
name:=: name;
temp:= REGEXP_REPLACE(name,'a|e|i|o|u');
dbms_output.put_line('After removing vowels: '||temp);
END;
OUTPUT
b. Write a PL/SQl program to accept a number and a divisor. Make sure the divisor is less
than or equal to 10. Else display an error message. Otherwise display the remainder in
words.
DECLARE
numerator number;
divisor number;
rem number;
BEGIN
numerator := &numerator;
divisor := &divisor;
if(divisor<=10)then
rem:= mod(numerator , divisor);
case rem
end case;
else
dbms_output.put_line('Divisor should be less than or equal to 10');
end if;
END;
OUTPUT
PROGRAM:
DECLARE
E_ID EMPLOYEE.EMP_ID%type := &E_ID;
result integer;
FUNCTION BASIC_HRA(E_ID In NUMBER)
return int
as
result integer;
BEGIN
44 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)
Database Management System_NEP_Syllabus
OUTPUT
2. Accept year as parameter and write a Function to return the total net salary spent for a
given year.
TABLE:
PROGRAM:
DECLARE
E_YEAR EMPLY.YEAR%type := &E_YEAR;
N_salary integer;
FUNCTION NET_SAALRY(E_YEAR In integer)
return int
as
result integer;
BEGIN
SELECT SUM(SALARY+hra-EXPENSE) into result from EMPLY where year =e_year ;
return result;
END;
BEGIN
N_salary := NET_SAALRY(E_year);
dbms_output.put_line('NET SALARY IN THE YEAR'||e_year||' is : '||N_salary);
END;
OUTPUT
3. Create a function to find the factorial of a given number and hence find NCR.
Factorial:
DECLARE
num int:=5;
res int;
FUNCTION fact(num IN int)
return int
AS
f int;
BEGIN
f:=1;
for i in 1..num loop
f:=f*i;
end loop;
return f;
END fact;
BEGIN
END;
OUTPUT:
NCR:
DECLARE
x NUMBER;
y NUMBER;
z NUMBER;
res NUMBER;
FUNCTION ncr(n IN int,r IN int)
return int
AS
z int;
BEGIN
if(n=0 or n=r)then
return 1;
else
x := ncr(n-1,r-1);
y:= ncr(n-1,r);
z:= x+y;
return z;
end if;
END;
BEGIN
res:=ncr(5,2);
dbms_output.put_line('NCR OF 5 AND 2 IS:'||res);
END;
OUTPUT
4. Write a PL/SQL block to print prime Fibonacci series using local function.
DECLARE
n1 NUMBER:=0;
n2 NUMBER:=1;
temp NUMBER;
res NUMBER;
FUNCTION fibonacci(n IN int)
RETURN int
as
temp int;
BEGIN
END;
BEGIN
dbms_output.put_line('Fibonacci Series: ');
dbms_output.put_line(n1);
dbms_output.put_line(n2);
res:=fibonacci(6);
END;
OUTPUT
DECLARE
n INTEGER;
rev INTEGER;
b INTEGER;
rem INTEGER;
FUNCTION reverse_of_number( a in integer)
return integer
as
rev integer;
BEGIN
rev := 0;
b:= a;
dbms_output.put_line('Number: '||a);
END LOOP;
return rev;
END;
BEGIN
n:= &n;
dbms_output.put_line('Reverse = '|| reverse_of_number(n));
END;
OUTPUT
CURSORS
1.Write a PL/SQL block that will display the name, dept_no, salary of first highest paid
employees.
TABLE:
PROGRAM:
DECLARE
e_dept_no EMPLOYEE.DEPT_NO%type;
emp_name EMPLOYEE.E_NAME%type;
e_salary EMPLOYEE.SALARY%type;
CURSOR E_CURSOR is
SELECT DEPT_NO, E_NAME, SALARY FROM EMPLOYEE
WHERE SALARY = (SELECT MAX(SALARY) FROM EMPLOYEE);
BEGIN
OPEN E_CURSOR;
FETCH E_CURSOR into E_DEPT_NO, EMP_NAME, E_SALARY;
dbms_output.put_line ('DEPT_NO: '||E_DEPT_NO);
dbms_output.put_line ('EMPLOYEE NAME: '||EMP_NAME);
dbms_output.put_line ('SALARY: '||E_SALARY);
CLOSE E_CURSOR;
END;
OUTPUT
2.Write a PL/SQL block that will display the employee detail along with salary using cursors.
DECLARE
e_emp_id EMPLOYEE.EMP_ID%type;
emp_name EMPLOYEE.E_NAME%type;
e_job EMPLOYEE.JOB%type;
e_mgr EMPLOYEE.MGR%type;
e_salary EMPLOYEE.SALARY%type;
e_dept_no EMPLOYEE.DEPT_NO%type;
CURSOR E_CURSOR is
SELECT EMP_ID, E_NAME, JOB,MGR, SALARY,DEPT_NO FROM EMPLOYEE;
BEGIN
OPEN E_CURSOR;
LOOP
FETCH E_CURSOR into e_emp_id, emp_name,e_job,e_mgr,e_salary,e_dept_no;
EXIT WHEN E_CURSOR%notfound;
dbms_output.put_line ('EMP_ID: '||e_emp_id);
dbms_output.put_line ('EMPLOYEE NAME: '||emp_name);
dbms_output.put_line ('JOB: '||e_job);
dbms_output.put_line ('MANAGER ID: '||e_mgr);
dbms_output.put_line ('SALARY: '||e_salary);
dbms_output.put_line ('DEPT_NO: '||e_dept_no);
DBMS_OUTPUT.PUT_LINE ('');
END LOOP;
CLOSE E_CURSOR;
END;
OUTPUT:
3. To write a cursor to display the list of employees who are working as a manager or
Analyst.
DECLARE
e_emp_id EMPLOYEE.EMP_ID%type;
emp_name EMPLOYEE.E_NAME%type;
e_job EMPLOYEE.JOB%type;
e_mgr EMPLOYEE.MGR%type;
e_salary EMPLOYEE.SALARY%type;
e_dept_no EMPLOYEE.DEPT_NO%type;
CURSOR E_CURSOR is
SELECT EMP_ID, E_NAME, JOB,MGR, SALARY,DEPT_NO FROM EMPLOYEE
WHERE JOB = 'MANAGER' OR JOB = 'ANALYST';
BEGIN
OPEN E_CURSOR;
LOOP
FETCH E_CURSOR into e_emp_id, emp_name,e_job,e_mgr,e_salary,e_dept_no;
EXIT WHEN E_CURSOR%notfound;
dbms_output.put_line ('EMP_ID: '||e_emp_id);
dbms_output.put_line ('EMPLOYEE NAME: '||emp_name);
dbms_output.put_line ('JOB: '||e_job);
dbms_output.put_line ('MANAGER ID: '||e_mgr);
dbms_output.put_line ('SALARY: '||e_salary);
dbms_output.put_line ('DEPT_NO: '||e_dept_no);
DBMS_OUTPUT.PUT_LINE ('');
END LOOP;
CLOSE E_CURSOR;
END;
OUTPUT
CURSOR EMP_CRSR IS
SELECT EMP_ID,E_NAME,JOB,DEPT_NO FROM EMPLOYEE
WHERE JOB = '&JOB' AND DEPT_NO= &DEPT_NO;
BEGIN
OPEN EMP_CRSR;
LOOP
FETCH EMP_CRSR INTO e_emp_id,emp_name,e_job,e_dept_no;
54 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)
Database Management System_NEP_Syllabus
OUTPUT
5.Write a PL/SQL block using implicit cursor that will display message, the salaries of all the
employees in the employees table are updated. If none of the employees salary are updated
we get a message none of the salary were updated. Else we get a message like for
example, ‘Salaries for 1000 employees are updated’ if there are 1000 rows in employees
table.
DECLARE
total_rows number(2);
BEGIN
UPDATE EMPLOYEES
SET salary = salary +1500;
IF sql%notfound THEN
dbms_output.put_line('None of the salaries were updated');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line('Salaries for ' ||total_rows || ' employees are updated ');
END IF;
END;
OUTPUT
Updated Table: