DBMS NEP Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Database Management System_NEP_Syllabus

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

Ramez Elamassri, Shankant B. Navathe, Fundamentals of Database Systems, Pearson, 7th


Edition, 2015
Bipin Desai, An Introduction to database systems, Galgotia Publications,2010.
C J Date: Introduction to Database System
Abraham Silberschatz, Henry Korth, S.Sudarshan, Database Systems Concepts, Sixth
Edition, McGraw Hill, 2010.
Raghu Rama krishnan and Johannes Gehrke, Database management systems, Third
Edition, 2002

Syllabus

Course Title: Database Management System Lab


Course code: 21BCA3C7P
Total Contact Hours: 52

1 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

Course Credits: 02
Formative Assessment Marks: 25
Duration of SEE/Exam: 03 Hours
Summative Assessment Marks: 25

Course Outcomes (CO’s)

At the end of the course, students will be able to:

Student would be able to create tables, execute queries and PL/SQL programs.

DSC7: Database Management System (DBMS) Lab

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.

Create department table with the following structure.


Name Type
Deptno Number
Deptname Varchar2(20)
location Varchar2(20)
Add column designation to the department table.
Insert values into the table.
List the records of emp table grouped by deptno.
Update the record where deptno is9.
Delete any column data from thetable.

Create a table called Customertable


Name Type
Cust. name Varchar2(20)
Cust. street Varchar2(20)
Cust. city Varchar2(20)
Insert records into thetable.
Add salary column to thetable.
Alter the table column domain.
Drop salary column of the customer table.

2 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

Delete the rows of customertable whose cust_city is‘CTA’

Create a table called branchtable.

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.

QUERIES USING DDL AND DML


a. Create a user and grant all permissions to theuser.
Insert the any three records in the employee table and use rollback. Check
theresult.
Add primary key constraint and not null constraint to the employee table.
Insert null values to the employee table and verify theresult.

a. Create a user and grant all permissions to theuser.


Insert values in the department table and usecommit.
Add constraints like unique and not null to the departmenttable.
Insert repeated values and null values into thetable.

a. Create a user and grant all permissions to theuser.


Insert values into the table and use commit.
c. Delete any three records in the department table and use rollback.
d. Add constraint primary key and foreign key to the table.

QUERIES USING AGGREGATE FUNCTIONS


a. By using the group by clause, display the enames who belongs to
deptno 10 alongwith average salary.
Display lowest paid employee details under eachdepartment.
Display number of employees working in each department and their
departmentnumber.
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
specifiedabove.
List all employees which start with either B orC.
Display only these ename of employees where the maximum salary is
greater than or equal to 5000.

a. Calculate the average salary for each differentjob.


Show the average salary of each job excludingmanager.

3 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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.

a. Count the number of employees in department20


Find the minimum salary earned byclerk.
Find minimum, maximum, average salary of allemployees.
List the minimum and maximum salaries for each jobtype.
List the employee names in descendingorder.
List the employee id, names in ascending order byempid.
Delete any three records in the department table and use rollback.
Add constraint primary key and foreign key to the table.

a. Create a user and grant all permissions to theuser.


Use revoke command to remove userpermissions.
Change password of the usercreated.
Add constraint foreign key and notnull.

a. Create a user and grant all permissions to theuser.


Update the table reserves and use savepoint androllback.
Add constraint primary key , foreign key and not null to the reservestable
Delete constraint not null to the tablecolumn.
Part B:
PROGRAMS ON PL/SQL

a. Write a PL/SQL program to swap twonumbers.


b. Write a PL/SQL program to find the largest of three numbers.
a. Write a PL/SQL program to find the total and average of 6 subjects and
display thegrade.
b. Write a PL/SQL program to find the sum of digits in a given number.
a. Write a PL/SQL program to display the number in reverseorder.
b. Write a PL / SQL program to check whether the given number is prime
or not.
a. Write a PL/SQL program to find thefactorial of a given number.
b. Write a PL/SQL code block 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 areas, consisting of two
columns radius and area.
a. Write a PL/SQL program to accept a string and remove the vowels from
the string.
(When„hello‟passedtotheprogramitshoulddisplay„Hll‟removingeandofromthe

4 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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.

PROCEDURES AND FUNCTIONS

Write a function to accept employee number as parameter and return


Basic +HRA together assingle
column.
Accept year as parameter and write a Function to return the total net salary
spent for a givenyear.
Create a function to find the factorial of a given number and hence
findNCR.
Write a PL/SQL block o pint prime Fibonacci series using localfunctions.
Create function to the reverse of givennumber.

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

5 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

PART-A

Create a table called Employee with the following structure.

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

CREATE TABLE EMPLOYEE


(
EMP_NO NUMBER PRIMARY KEY NOT NULL,
E_NAME VARCHAR2(20) NOT NULL,
JOB VARCHAR2(20) NOT NULL,
MGR NUMBER NOT NULL,
SALARY NUMBER NOT NULL
);

OUTPUT

6 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

a. Add a column commission with domain to the Employee table.

ALTER TABLE EMPLOYEE


ADD COMMISSION NUMBER;

OUTPUT

b. Insert any five records into the table.

INSERT INTO EMPLOYEE VALUES(1,'ASHISH','DEVELOPER',1234,40000,400);


INSERT INTO EMPLOYEE VALUES(2,'AJAY','ANALYST',2345,45000,500);
INSERT INTO EMPLOYEE VALUES(3,'BINOD','MANAGER',3456,35000,0);
INSERT INTO EMPLOYEE VALUES(4,'CHAITANYA','MANAGER',4567,45000,0);
INSERT INTO EMPLOYEE VALUES(5,'DIYA','ANALYST',5678,60000,900);
INSERT INTO EMPLOYEE VALUES(6,'LAVESH','FULL DEVELOPER',6789,50000,600);
INSERT INTO EMPLOYEE VALUES(7,'MANAN','ANALYST',7890,55000,700);
INSERT INTO EMPLOYEE VALUES(8,'RANJITH','DEVELOPER',8912,30000,200);
INSERT INTO EMPLOYEE VALUES(9,'EDWARD','SALESMAN',2163,50000,800);

OUTPUT

c. Update the column details of job


update EMPLOYEE SET JOB = 'WEB DEVELOPER' WHERE EMP_NO = 6;

OUTPUT

7 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

d. Rename the column of Employ table using alter command.


ALTER TABLE EMPLOYEE
RENAME COLUMN EMP_NO to EMP_ID;

OUTPUT

e. Delete the employee whose Empno is 19.


DELETE FROM EMPLOYEE WHERE EMP_ID = 9;

OUTPUT

8 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

Create department table with the following structure.

NAME TYPE
Deptno Number
Deptname Varchar2(10)
location Varchar2(10)

a. Add column designation to the department table.


b. Insert values into the table.
c. List the records of dept table grouped by deptno.
d. Update the record where deptno is 9.
e. Delete any column data from the table.

TABLE CREATION:
CREATE TABLE DEPARTMENT
( DEPT_NO NUMBER,
DEPT_NAME VARCHAR2(20),
LOCATION VARCHAR2(20)
);

OUTPUT

a. Add column designation to the department table.

ALTER TABLE DEPARTMENT


ADD DESIGNATION VARCHAR2(30);

OUTPUT

9 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

b. Insert values into the table.


INSERT INTO DEPARTMENT VALUES(9,'RESEARCH','BANGLORE','ANALYST');
INSERT INTO DEPARTMENT VALUES(10,'ACCOUNTING','HYDERABAD','MANAGER');
INSERT INTO DEPARTMENT VALUES(11,'DEVELOPMENT','CHENNAI','DEVELOPER');
INSERT INTO DEPARTMENT VALUES(12,'SALES','MUMBAI','SALESMAN');

OUTPUT

c. List the records of dept table grouped by deptno.

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. Update the record where deptno is 9.

UPDATE DEPARTMENT SET LOCATION = 'PUNE' WHERE DEPT_NO = 9;

OUTPUT

10 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

e. Delete any column data from the table.

ALTER TABLE DEPARTMENT


DROP COLUMN LOCATION;

OUTPUT

3). Create a table called Customer table


Name Type
Cust name Varchar2(20)
Cust Street Varchar2(20)
Cust city Varchar2(20)
a. Insert records into the table.
b. Add salary column to the table.
c. Alter the table column domain.
d. Drop salary column of the customer table.
e. Delete the rows of customer table whose cust_city is ‟CTA”.

TABLE CREATION:
CREATE TABLE CUSTOMER
(
CUST_NAME VARCHAR2(30),
CUST_STREET VARCHAR2(20),
CUST_CITY VARCHAR2(20)
);

OUTPUT

a. Insert records into the table.

INSERT INTO CUSTOMER VALUES('RAJESH','ANJANEYA BADAVANE','DAVANGERE');


INSERT INTO CUSTOMER VALUES('KRUPA','SHIVAJI NAGAR','BANGLORE');
INSERT INTO CUSTOMER VALUES('ANTHONY','KOTE ROAD','CHITRADURGA');

11 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

OUTPUT

b. Add salary column to the table.

ALTER TABLE CUSTOMER


ADD SALARY NUMBER;

OUTPUT

c. Alter the table column domain.


ALTER TABLE CUSTOMER
MODIFY CUST_NAME VARCHAR2(50);

OUTPUT

d. Drop salary column of the customer table.

ALTER TABLE CUSTOMER


DROP COLUMN SALARY;

12 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

OUTPUT

d. Delete the rows of customer table whose cust_city is ‟CTA”.

DELETE FROM CUSTOMER WHERE CUST_CITY LIKE 'CHITRADURGA';

OUTPUT

4). Create a table called branch table.

Name Type
Branch Name Varchar2(20)
Branch city Varchar2(20)
Asserts Number

a) Increase the size of data type for asserts to the branch.


b) Add and drop a column to the branch table.
c) Insert values to the table.
d) Update the branch name column.
e) Delete any two columns from the table.
f) Delete the row of the table with some condition.

TABLE CREATION:

CREATE TABLE BRANCH_TABLE


( BRANCH_NAME VARCHAR2(20),
BRANCH_CITY VARCHAR2(20),
ASSERTS NUMBER
);

OUTPUT

13 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

a. Increase the size of data type for asserts to the branch.


ALTER TABLE BRANCH_TABLE
MODIFY ASSERTS NUMBER(6);

OUTPUT

b. Add and drop a column to the branch table.


ADD:
ALTER TABLE BRANCH_TABLE
ADD YEAR DATE;

OUTPUT

DROP:
ALTER TABLE BRANCH_TABLE
DROP COLUMN YEAR;

c. Insert values to the table.

INSERT INTO BRANCH_TABLE VALUES('Mahesh Nagar','Indore',4000);


INSERT INTO BRANCH_TABLE VALUES('Dongri','Mumbai',6000);
INSERT INTO BRANCH_TABLE VALUES('Shivaji Nagar','Banglore',7000);
INSERT INTO BRANCH_TABLE VALUES('Lakshmi Road','Pune',5000);

14 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

OUTPUT

d. Update the branch name column.

UPDATE BRANCH_TABLE SET BRANCH_NAME = 'Mathikere' where BRANCH_CITY =


'Banglore';

OUTPUT

e. Delete any two columns from the table.

ALTER TABLE BRANCH_TABLE


DROP COLUMN ASSERTS;

ALTER TABLE BRANCH_TABLE


DROP COLUMN BRANCH_CITY;

15 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

f. Delete the row of the table with some condition.

DELETE FROM BRANCH_TABLE WHERE BRANCH_NAME = 'Dongri';

OUTPUT

QUERIES USING DDL AND DML

5) a. Create a user and grant all permissions to the user.


b. Insert the any three records in the employee table and use rollback. Check the result.
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.

a. Create a user and grant all permissions to the user.

OUTPUT

16 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

b. Insert the any three records in the employee table and use rollback. Check the result.

OUTPUT

INSERT:

OUTPUT

17 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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.

ADDING NULL TO NOT

18 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

OUTPUT

6) a. Create a user and grant all permissions to the user.


b. Insert values in the department table and use commit.
c. Add constraints like unique and not null to the department table.
d. Insert repeated values and null values into the table.

a. Create a user and grant all permissions to the user.

OUTPUT

19 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

b. Insert values in the department table and use commit.

INSERT:

OUTPUT

COMMIT:
OUTPUT

c. Add constraints like unique and not null to the department table.

20 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

d. Insert repeated values and null values into the table.

21 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

7) a. Create a user and grant all permissions to the user.


b. Insert values into the table and use commit.
c. Delete any three records in the department table and use rollback.
d. Add constraint primary key and foreign key to the table.

a. Create a user and grant all permissions to the user.

OUTPUT

b. Insert values into the table and use commit.

INSERT:

OUTPUT

22 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

COMMIT:

OUTPUT

c. Delete any three records in the department table and use rollback.

OUTPUT

23 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

d. Add constraint primary key and foreign key to the table.

OUTPUT

QUERIES USING AGGREGATE FUNCTIONS

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.

Select emp.e_name ,avg(emp.salary)


from employee emp
where dept_no = 10
group by e_name ;

OUTPUT

b) Display lowest paid employee details under each department.

SELECT e.e_name ,e.salary, d.dept_name


FROM employee e, department d
WHERE e.dept_no = d.dept_no and (e.dept_no,e.salary) IN
( SELECT dept_no,MIN(salary)
FROM employee
GROUP BY dept_no);

24 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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

e) List all employees which start with either B or C.

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

25 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

f) Display only these ename of employees where the maximum salary is greater than or
equal to 5000.

SELECT E_NAME FROM EMPLOYEE WHERE(SELECT MAX(SALARY) FROM


EMPLOYEE)>5000;
OUTPUT:

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.

Calculate the average salary for each different job.

SELECT JOB ,AVG(SALARY) AS


AVERAGE_SALARY FROM EMPLOYEE
GROUP BY JOB;

OUTPUT

Show the average salary of each job excluding manager.

SELECT JOB, AVG(salary) as salary


FROM employee
WHERE JOB <> 'MANAGER'
GROUP BY JOB;

26 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

OUTPUT

Show the average salary for all departments employing more than three people.

SELECT DEPARTMENT.DEPT_NO, AVG(SALARY), COUNT(*)AS


COUNT,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(*) >= 3;

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

Show that value returned by sign (n) function.

The SIGN() function returns the sign of a number.


This function will return one of the following:
If number > 0, it returns 1
If number = 0, it returns 0

27 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

If number < 0, it returns -1


SELECT SIGN(commission)AS SIGN from employee;

OUTPUT

f) How many days between day of birth to current date.


SELECT E_NAME,DOB,DOB - TO_DATE('05-DEC-2022')AS DAYS FROM EMPLOYEE ;

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.

28 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

f) By using the group by clause, display the eid who belongs to deptno 05 along with
average salary.

a. Show that two substring as single string.

SELECT CONCAT(SUBSTR(E_NAME, 0, 6), SUBSTR(JOB, 1,7)) AS SUBSTRINGS FROM


EMPLOYEE WHERE EMP_ID = 1;

OUTPUT

b. List all employee names, salary and 15% rise in salary.


SELECT salary,salary + (salary * 15/100) as NewSalary, e_name
FROM employee;

OUTPUT

c. Display lowest paid emp details under each manager.

SELECT DISTINCT department.dept_no, min(salary),department.dept_name


from employee
inner join department on employee.dept_no = department.dept_no
GROUP BY department.dept_name,department.dept_no;

OUTPUT

d. Display the average monthly salary bill for each deptno.

SELECT count(*) as count,avg(salary),department.dept_no,department.dept_name


FROM employee
inner join department on employee.dept_no = department.dept_no

29 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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.

Select emp.emp_id ,avg(emp.salary)


from employee emp
where dept_no = 9
group by emp_name;

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.

a) Count the number of employees in department 20


SELECT COUNT(DEPT_NO) AS COUNT_OF_EMPLOYEES FROM EMPLOYEE WHERE
DEPT_NO = 20;

30 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

OUTPUT

b) Find the minimum salary earned by clerk.


SELECT MIN(SALARY) FROM EMPLOYEE WHERE JOB = 'CLERK';

OUTPUT

c) Find minimum, maximum, average salary of all employees.


SELECT dept_no, SUM(salary), AVG(salary), MAX(salary), MIN(salary)
FROM employee
GROUP BY dept_no;

OUTPUT

d) List the minimum and maximum salaries for each job type.
SELECT JOB,MIN(SALARY),MAX(SALARY) FROM EMPLOYEE GROUP BY JOB

OUTPUT

e) List the employee names in descending order.

SELECT E.E_NAME
FROM EMPLOYEE E
ORDER BY E.E_NAME DESC ;

31 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

OUTPUT

f) List the employee id, names in ascending order by empid.

SELECT e.EMP_ID ,e.E_NAME


FROM employee e
ORDER BY e.EMP_ID ASC ,e.E_NAME ASC;

OUTPUT

g) Delete any three records in the department table and use rollback.

OUTPUT

32 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

h) Add constraint primary key and foreign key to the table.

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.

a. Create a user and grant all permissions to the user.

OUTPUT

33 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

b. Use revoke command to remove user permissions.

OUTPUT

c. Change password of the user created.

OUTPUT

d. Add constraint foreign key and not null.

OUTPUT

34 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

Use of savepoint and rollback.

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.

35 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

PART-B

PROGRAMS ON PL/SQL

a. Write a PL/SQL program to swap two numbers.


DECLARE
a number;
b number;
temp number;
BEGIN
a:=5;
b:=10;
dbms_output.put_line ('Before swapping');
dbms_output.put_line ('a = '||a||' b ='||b);
temp:=a;
a:=b;
b:=temp;
dbms_output.put_line ('After swapping:');
dbms_output.put_line ('a = '||a||' b = '||b);
END;

OUTPUT

c. Write a PL\SQL program to find the largest of three numbers.

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;

36 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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

dbms_output.put_line('Sub-1 marks: '||mark1);


dbms_output.put_line('Sub-2 marks: '||mark2);
dbms_output.put_line('Sub-3 marks: '||mark3);
dbms_output.put_line('Sub-4 marks: '||mark4);
dbms_output.put_line('Sub-5 marks: '||mark5);
dbms_output.put_line('Sub-6 marks: '||mark6);
score:= mark1+mark2+mark3+mark4+mark5+mark6;
average:= score/6;
IF (average < 95 AND average>=85)THEN
grade:= 'A';
ELSIF (average<85 AND average>=70) THEN
grade:= 'B';
ELSIF (average<70 AND average>=50) THEN
grade:= 'C';
ELSIF (average<35)THEN
grade:= 'D';
ELSE
grade:= 'E';
END IF;
dbms_output.put_line('-----------------------');
dbms_output.put_line('Total marks: '||score);
dbms_output.put_line('Average: '||average);
dbms_output.put_line('Your grade is: ' ||grade);
END;

37 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

OUTPUT

b. Write a PL/SQL program to find the sum of digits in a given number.


DECLARE

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

38 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

a. Write a PL\SQL program to display the number in reverse order.


DECLARE

a INTEGER;
rev INTEGER;
rem INTEGER;

BEGIN

a := 78924;
rev := 0;

dbms_output.put_line('Number: '||a);

WHILE a <> 0 LOOP

rem := MOD(a, 10);


rev := (rev*10) + rem;
a := Trunc(a / 10);

END LOOP;

dbms_output.put_line('Reverse = '|| rev);

END;

OUTPUT

b. Write a PL\SQL program to check whether given number is prime or not.


DECLARE

n NUMBER;
i NUMBER;
temp NUMBER;

BEGIN

n:= &n;
temp:= 1;

FOR i IN 2..n/2 LOOP

IF MOD(n, i) = 0 THEN
temp:= 0;
EXIT;

39 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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

a. Write a PL\SQL program to find factorial of a number.


DECLARE
n number;
fac number:=1;
i number;
BEGIN
n:=&n;
FOR i IN 1..n LOOP
fac:=fac*i;
END LOOP;
dbms_output.put_line('Factorial of '||n||' : '||fac);
END;

OUTPUT

40 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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.

TABLE AREAS CREATION:

CREATE TABLE AREAS


( RADIUS NUMBER(1) NOT NULL,
AREA FLOAT NOT NULL
);

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

41 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

OUTPUT STORED IN TABLE

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

42 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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

when 0 then dbms_output.put_line('Remainder is zero');


when 1 then dbms_output.put_line('Remainder is one');
when 2 then dbms_output.put_line('Remainder is two');
when 3 then dbms_output.put_line('Remainder is three');
when 4 then dbms_output.put_line('Remainder is four');
when 5 then dbms_output.put_line('Remainder is five');
when 6 then dbms_output.put_line('Remainder is six');
when 7 then dbms_output.put_line('Remainder is seven');
when 8 then dbms_output.put_line('Remainder is eight');
else dbms_output.put_line('Remainder is nine');

end case;
else
dbms_output.put_line('Divisor should be less than or equal to 10');
end if;

END;

OUTPUT

43 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

PROCEDURES AND FUNCTIONS


1.Write a function to accept employee number as parameter and return Basic +HRA
together as single column.
TABLE:

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

SELECT SALARY+HRA INTO result from EMPLOYEE where EMP_ID =E_ID ;


return result;
END;
BEGIN
result := BASIC_HRA(E_ID);
dbms_output.put_line('BASIC + HRA OF EMPLOYEE ID '||E_ID||' is: '||result);
END;

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;

45 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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

dbms_output.put_line('FINDING FACTORIAL OF:'||num);


res:=fact(num);
dbms_output.put_line('FACTORIAL IS:'||res);

END;

46 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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

47 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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

for i in 2..n loop


temp:= n1+n2;
n1 := n2;
n2 := temp;
dbms_output.put_line(temp);
end loop;
return temp;

END;

BEGIN
dbms_output.put_line('Fibonacci Series: ');
dbms_output.put_line(n1);
dbms_output.put_line(n2);
res:=fibonacci(6);

END;

OUTPUT

48 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

5.Create function to reverse of given number.

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

WHILE b <> 0 LOOP

rem := MOD(b, 10);


rev := (rev*10) + rem;
b := Trunc(b / 10);

END LOOP;

return rev;

END;

BEGIN

n:= &n;
dbms_output.put_line('Reverse = '|| reverse_of_number(n));
END;

OUTPUT

49 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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

50 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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:

51 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

52 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

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

53 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

4. To write a cursor to find employee with given job and dept_no.


DECLARE
e_emp_id EMPLOYEE.EMP_ID%type;
emp_name EMPLOYEE.E_NAME%type;
e_job EMPLOYEE.JOB%type;
e_dept_no EMPLOYEE.DEPT_NO%type;

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

EXIT WHEN EMP_CRSR%NOTFOUND;


DBMS_OUTPUT.PUT_LINE('EMP_ID: '||e_emp_id);
DBMS_OUTPUT.PUT_LINE('EMP_NAME: '||emp_name);
DBMS_OUTPUT.PUT_LINE('JOB: '||e_job);
DBMS_OUTPUT.PUT_LINE('DEPT_NO: '||e_dept_no);
DBMS_OUTPUT.PUT_LINE(' ');
END LOOP;
END;

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

55 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

Updated Table:

56 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)


Database Management System_NEP_Syllabus

57 INTERFACE BCA COLLEGE, DVG (Site: www.iccadvg.org , Phone no: 8884768574)

You might also like