Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Ii-Ii DBMS Lab

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

Ii-Ii DBMS Lab

I DBMS LAB
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/ 36

RG 22 Regulations

GEETHANJALI INSTITUTE OF SCIENCE & TECHNOLOGY


Unit of USHODAYA EDUCATIONAL SOCIETY
An ISO 9001:2015 certified Institution: Recognized under Sec. 2(f)& 12(B) of UGC Act, 1956
3rd Mile, Bombay Highway, Gangavaram (V), Kovur(M), SPSR Nellore (Dt), Andhra Pradesh, India- 524137
Ph. No. 08622-212769, E-Mail: [email protected], Website: www.gist.edu.in

II Year-II-Semester Regulation:R22

22A0515P -
DATABASE MANAGEMENT SYSTEMS
LAB MANUAL

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


II-YEAR-II-SEMESTER
R22 REGULATION
ACADEMIC YEAR: 2023-2024

Submitted By
DVH VENUKUMAR
Asst.Professor
Department of CSE., GIST
Institute Vision and Mission Vision
 To emerge as a leading Engineering institution imparting quality education
Mission
 IM1 Implement Effective teaching-learning strategies for quality education
 IM2 Build Congenial academic ambience for progressive learning
 IM3 Facilitate Skill development through Industry-Institute initiatives
 IM4 Groom environmentally conscious and socially responsible technocrats

Department of Computer Science and Engineering


Vision
 To evolve as a leading computer science and engineering centre producing
competent technocrats to meet the demands of ever-changing industry and
society.
Mission
 DM1 : Imparting quality education through innovative teaching learning
processes
 DM2: Motivating students to upgrade their technical expertise by promoting
learner centric activities.
 DM3: Inculcating ethical values and interpersonal skills in the learners.
 DM4: upgrading knowledge in cutting edge technologies keeping pace with
industrial standards.
PROGRAM SPECIFIC OUTCOMES (PSOs
 PSO 1: Apply the expertise in adaptive algorithms to develop quality software
applications.
 PSO 2: Get employed or become an entrepreneur through their capabilities in
basic and advanced technologies.
PROGRAM EDUCATIONAL OBJECTIVES (PEOs)
Graduates of B.Tech in Computer Science and Engineering Programme shall be able to
 PEO 1: Outperform in professional career or higher learning by upgrading skills
 in Computer Science and Engineering stream.
 PEO 2: Provide computing solutions for complex problems to meet industry
demands and societal needs.
 PEO 3: Offer ethical, socially sensitive solutions as professionals and as
entrepreneurs in Computer Science and other engineering disciplines.
 PEO 4: Leverage new computing technologies by engaging themselves in
perpetual learning.
PROGRAM OUTCOMES (POs)

Engineering Graduates will be able to:


PO1.
Engineering Knowledge : Apply the knowledge of mathematics, science, engineering fundamentals,
and an engineering specialization to the solution of complex engineering problems.
PO2.
Problem Analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences,
and engineering sciences.
PO3.
Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.
PO4.
Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis of the information
to provide valid conclusions.

PO5.
Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
PO6.
The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional
engineering practice.

PO7.
Environment and sustainability: Understand the impact of the professional engineering solutions in
societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.

PO8.
Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of
the engineering practice.

PO9.
Individual and team work: Function effectively as an individual, and as a member or leader in diverse
teams, and in multidisciplinary settings.

PO10.
Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports
and design documentation, make effective presentations, and give and receive clear instructions.

PO11.
Project management and finance: Demonstrate knowledge and understanding of the engineering
and management principles and apply these to one’s own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments.

PO12.
Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
GEETHANJALI INSTITUTE OF SCIENCE & TECHNOLOGY
USHODAYA EDUCATIONAL SOCIETY
An ISO 9001:2015 certified Institution: Recognized under Sec. 2(f)& 12(B) of UGC Act, 1956
3rd Mile, Bombay Highway, Gangavaram (V), Kovur(M), SPSR Nellore (Dt), Andhra Pradesh, India- 524137
Ph. No. 08622-212769, E-Mail: [email protected], Website: www.gist.edu.in

REGULATION-R22-II-YEAR-II-SEMESTER

DEPARTMENT OF CSE
Common All (AI&ML, CS&DS, CSE)

COURSE B.Tech

DEPARTMENT CSE, AI & ML, CS & DS

YEAR II

SEMESTER II

REGULATION RG22

SUBJECT Database Management Systems

COURSE CODE

CREDITS

ROLLNO OF STUDENT

NAME OF THE STUDENT

SECTION

ACADEMIC YEAR 2023-24


RG 22 Regulations

GEETHANJALI INSTITUTE OF SCIENCE & TECHNOLOGY


Unit of USHODAYA EDUCATIONAL SOCIETY
An ISO 9001:2015 certified Institution: Recognized under Sec. 2(f)& 12(B) of UGC Act, 1956
3rd Mile, Bombay Highway, Gangavaram (V), Kovur(M), SPSR Nellore (Dt), Andhra Pradesh, India- 524137
Ph. No. 08622-212769, E-Mail: [email protected], Website: www.gist.edu.in
DATABASE MANAGEMENT SYSTEMS LAB
(Common to CSE, AI&ML, CS, DS)
Course Code L:T:P:S Credits Exam Marks Exam Duration Course Type
22A0515P 0:0:3:0 1.5 CIE: 30 SEE:70 3 Hours PCC
Course Objectives:
This course will enable students to:
 Illustrate the different issues involved in the design and implementation of a database system.
 Use data manipulation language to query, update, and manage a database.
 Design and build a simple database system and demonstrate competence with the
fundamental tasks involved with modeling, designing, and implementing a DBMS.
Course Outcomes (CO):
On completion of this course, student will be able to
 Apply database tools to perform various operations for the given database.
 Design database and retrieve information from database
 Develop ER diagrams and normalize the solution of a database.
 Implement the integrity constraints and PL/SQL programs to build efficient databases.
 Develop solutions for database applications using procedures and functions.
 Develop solutions for database applications using cursors and triggers.
Syllabus Total Hours: 48
Experiment 1: Practice session: Students should be allowed to choose appropriate DBMS software,install it,
configure it and start working on it. Create sample tables, execute some queries, use SQLPLUS features, and
use PL/SQL features like cursors on sample database.
Experiment 2: Draw E-R diagram for library management system Experiment 3:
Draw E-R diagram for university management systemExperiment 4: Draw E-R
diagram for hospital management system Experiment 5: Implement all DDL
Commands
Experiment 6: Implement all DML Commands
Experiment 7: Implement all TCL and DCL Commands
Experiment 8: a) Create relationship between the tables using Nested Queries
b) Implement different types of joins on tables
Experiment 9: Implement set operations on tables
Experiment 10: Create a table and apply various key constraints.
Experiment 11: Views – Create a Virtual table based on the result set of an SQL statement.
Experiment 12: a) Write a PL/SQL program to swap two numbers.
b) Write a PL/SQL program to find the largest of three numbers.
Experiment 13: a) Write a PL/SQL program to find the total and average of 6 subjects and displaythe grade.
b) Write a PL/SQL program to find the sum of digits in a given number.
Experiment 14: a) Write a PL/SQL program to display the number in reverse order.
b) Write a PL/SQL program to check whether the given number is prime or not.
c) Write a PL/SQL program to find the factorial of a given number.
Experiment 15: Wr it e PL/SQL pr ogr a ms to implement procedures and funct io ns.
Experiment 16: Write a PL/SQL Program on cursors
Experiment 17: Write a PL/SQL Program to implement triggers

Text Books:
1. Raghu Ramakrishnan, Johannes Gehrke, Jeff Derstadt, Scott Selikoff and Lin Zhu,DatabaseManagement
Systems solutions manual, third Edition, 2013.
GEETHANJALI INSTITUTE OF SCIENCE & TECHNOLOGY
USHODAYA EDUCATIONAL SOCIETY
An ISO 9001:2015 certified Institution: Recognized under Sec. 2(f)& 12(B) of UGC Act, 1956
3rd Mile, Bombay Highway, Gangavaram (V), Kovur(M), SPSR Nellore (Dt), Andhra Pradesh, India- 524137
Ph. No. 08622-212769, E-Mail: [email protected], Website: www.gist.edu.in

REGULATION-R22-II-YEAR-II-SEMESTER

22A0515P - DATABASE MANAGEMENT SYSTEMS

LIST OF PROGRAMS
Program Title of the Program Date Signature
No of the
Faculty
ER Model
1 ER Diagram for Library management Systems
2 ER Diagram for Hospital management Systems
SQL
3 Perform Basic SQL Queries
4 Aggregate Functions
5 View Creation
6 Joins on tables
PL / SQL
7 Multiplication Table
8 Biggest Number
9 Swapping Number

10 Prime Number
11 Sum of Even and Odd Numbers
12 Strong Number

13 Armstrong Number
14 Number Palindrome
15 Factorial Number
16 Fibonacci Series
17 Procedure Creation

18 Employee Pay Slip

19 Electricity Bill Preparation


20 Student Marks List
21 Audit Details using Triggers
22 Trigger Creation
ER-DIAGRAM FOR LIBRARY MANAGEMENT SYSTEM

Aim:- Draw the ER Diagram for Library management Systems.

This Library ER diagram illustrates key information about the Library,


including entities such as staff, readers, books, publishers, reports, and
authentication system. It allows for understanding the relationships between
entities.

Entities and their Attributes –

 Book Entity : It has authno, isbn number, title, edition, category, price.
ISBN is the Primary Key for Book Entity.

 Reader Entity : It has UserId, Email, address, phone no, name. Name is
composite attribute of firstname and lastname. Phone no is multi valued
attribute. UserId is the Primary Key for Readers entity.

 Publisher Entity : It has PublisherId, Year of publication, name. PublisherID


is the Primary Key.

 Authentication System Entity : It has LoginId and password with LoginID


as Primary Key.

 Reports Entity : It has UserId, Reg_no, Book_no, Issue/Return date. Reg_no


is the Primary Key of reports entity.

 Staff Entity : It has name and staff_id with staff_id as Primary Key.

 Reserve/Return Relationship Set : It has three attributes: Reserve date,


Due date, Return date.

Relationships between Entities –

 A reader can reserve N books but one book can be reserved by only one
reader. The relationship 1:N.

 A publisher can publish many books but a book is published by only one
publisher. The relationship 1:N.

 Staff keeps track of readers. The relationship is M:N.

 Staff maintains multiple reports. The relationship 1:N.

 Staff maintains multiple Books. The relationship 1:N.

 Authentication system provides login to multiple staffs. The relation is 1:N


ER Diagram for Library Management System.
ER-DIAGRAM FOR HOSPITAL MANAGEMENT SYSTEM

Aim:- Draw the ER Diagram for Hospital management System, with the following
entities
1) Patient
2) Room
3) Nurse
4) Employee
5) Doctor :(Trainee, Visiting, Permanent)
6) Receptionist
7) Record
8) Treatment
9) Medicine
PERFORM BASIC SQL QUERIES

Aim:- To Perform basic SQL Queries such as Create, Alter, Insert, Select, Drop & Primary
Key, Check Constraints.

1) To create a table “Course” with the following structure


Table Structure:- COURSE
Field Name Data type
Course_id Varchar2(10)
Course_name Varchar2(20)
Fee Number(8,2)
Duration Varchar2(5)
Constraints:-
i) Course_id contains primary key constraint
ii) Fee starts from 10000 are above using check constraint

SQL COMMAND FOR CREATING TABLE

SQL> create table course(course_id varchar2(10) primary key,


course_name varchar2(20),
fee number(8,2) check(fee>=10000));
TABLE CREATED.
SQL COMMAND FOR ALTERING TABLE

SQL> Alter table course add duration varchar2(5);


TABLE ALTERED
SQL COMMAND FOR INSERTING RECORDS TABLE
SQL> insert into course values('&course_id','&course_name','&fee','&duration');
Enter value for course_id : 101
Enter value for course_name : M.Tech
Enter value for fee : 28000
Enter value for duration : 2Yrs
1 ROW CREATED.
SQL> / (For Insert Multiple Records)

SQL COMMAND (SELECT) FOR OUTPUT OF COURSE TABLE


SQL> Select *from Course;
COURSE_ID COURSE_NAME FEE DURATION
---------- -------------------- ---------- ----------
101 M.Tech 22000 2Yrs
102 MCA 18000 2Yrs
103 MBA 16000 2Yrs
104 B.Tech 28000 2Yrs

SQL COMMAND FOR DROP COURSE TABLE


SQL> Drop table course;
Table dropped.
AGGREGATE FUNCTIONS

Aim:- To Perform aggregate functions on Employee Table


Table Structure:- EMP1
Field Name Data type
Empno Number(5)
Ename Varchar2(20)
Job Varchar2(15)
Sal Number(10,2)
Deptno Number(5)

SQL COMMAND FOR CREATING TABLE


SQL> create table emp1(empno number(5) primary key,
ename varchar2(20),
job varchar2(15),
sal number(10,2),
deptno number(5));
TABLE CREATED.
INPUT
SQL> insert into emp1 values(&empno,'&ename','&job',&sal,&deptno);
ENTER VALUE FOR EMPNO: 100
ENTER VALUE FOR ENAME: VARSHA
ENTER VALUE FOR JOB: MANAGER
ENTER VALUE FOR SAL: 20000
ENTER VALUE FOR DEPTNO: 10

1 ROW CREATED.
SQL> / (For Insert Multiple Records)

SQL> select * from emp1;


EMPNO ENAME JOB SAL DEPTNO
---------- ---------- ---------- ------------ ----------
100 VARSHA MANAGER 20000 10
101 RAMESH CLERK 10000 20
102 KRISHNA ACCOUNTENT 8000 10
103 KARISHMA CLERK 10000 20
104 ANUSHA ANALYST 15000 10
105 VENU MANAGER 20000 20
6 ROWS SELECTED.
PERFORM AGGREGATE FUNCTIONS

SQL> SELECT SUM(SAL) FROM EMP1;


SUM(SAL)
----------
83000
SQL> SELECT MAX(SAL) FROM EMP1;
MAX(SAL)
----------
20000
SQL> SELECT MIN(SAL) FROM EMP1;
MIN(SAL)
----------
8000
SQL> SELECT COUNT(SAL) FROM EMP1;
COUNT(SAL)
----------
6
SQL> SELECT AVG(SAL) FROM EMP1;
AVG(SAL)
----------
13833.3333
SQL> SELECT EMPNO, ENAME FROM EMP1 ORDER BY DEPTNO;

EMPNO ENAME
---------- --------------------
100 VARSHA
102 KRISHNA
104 ANUSHA
101 RAMESH
103 KARISHMA
105 VENU
SQL> SELECT DEPTNO,SUM(SAL) FROM EMP1 GROUP BY DEPTNO;

DEPTNO SUM(SAL)
---------- ----------
10 43000
20 40000
VIEW CREATION

Aim:- To create a view to get Department wise total salaries in the “Employee” table.
Table Structure:- EMP1
Field Name Data type
Empno Number(5)
Ename Varchar2(20)
Job Varchar2(15)
Sal Number(10,2)
Deptno Number(5)

SQL COMMAND FOR CREATING TABLE


SQL> create table emp1(empno number(5) primary key,
ename varchar2(20),
job varchar2(15),
sal number(10,2),
deptno number(5));
TABLE CREATED.
INPUT
SQL> insert into emp1 values(&empno,'&ename','&job',&sal,&deptno);
ENTER VALUE FOR EMPNO: 100
ENTER VALUE FOR ENAME: VARSHA
ENTER VALUE FOR JOB: MANAGER
ENTER VALUE FOR SAL: 20000
ENTER VALUE FOR DEPTNO: 10
1 ROW CREATED.
SQL> / (For Insert Multiple Records)

SQL> select * from emp1;


EMPNO ENAME JOB SAL DEPTNO
---------- ---------- ---------- ------------ ----------
100 VARSHA MANAGER 20000 10
101 RAMESH CLERK 10000 20
102 KRISHNA ACCOUNTENT 8000 10
3 ROWS SELECTED.

CREATING VIEW

SQL> create view emp_view1 as select *from emp1;


VIEW CREATED.
OUTPUT
SQL> select * from emp_view1;

EMPNO ENAME JOB SAL DEPTNO


---------- ---------- ---------- ------------ ----------
100 VARSHA MANAGER 20000 10
101 RAMESH CLERK 10000 20
102 KRISHNA ACCOUNTENT 8000 10
3 ROWS SELECTED.
CREATING VIEW USING GROUP BY FUNCTION

SQL> create view emp_view as(select deptno "department no",


sum(sal) "total sal" from emp1 group by deptno);
VIEW CREATED.
OUTPUT
SQL> select * from emp_view;

DEPARTMENT NO TOTAL SAL


------------- ----------
10 43000
20 40000
DROPPING VIEW

SQL> Drop view emp_view;


VIEW DROPPED.
JOINS
Aim:- To create two tables “DEPT”.,“EMP” to perform the following join operations
i) Equi join
ii) Non-Equi join
iii) Outer join
iv) Self join
Table1 Structure:- DEPT
Field Name Data type
Deptno Number(5)
Dname Varchar2(20)
Loc Varchar2(15)
Table2 Structure:- EMP
Field Name Data type
Empno Number(5)
Ename Varchar2(20)
Job Varchar2(15)
Mgr Number(5)
Sal Number(10,2)
Deptno Number(5)
SQL COMMAND FOR CREATING TABLE
SQL> create table dept(deptno number(5) primary key,
dname varchar2(20),
loc varchar2(15));
TABLE CREATED.
SQL> insert into dept values (&deptno,'&dname','&loc');
ENTER VALUE FOR DEPTNO: 10
ENTER VALUE FOR DNAME: ACCOUNTS
ENTER VALUE FOR LOC: KAVALI
1 ROW CREATED.

SQL> select *from dept;


DEPTNO DNAME LOC
---------- ---------------- ----------------
10 ACCOUNTS KAVALI
20 COMPUTERS NELLORE
30 SALES BUCHI
SQL> create table emp(empno number(5) primary key,
ename varchar2(20),
job varchar2(15),
mgr number(5),
sal number(10,2),
deptno number(5) references dept(deptno));
TABLE CREATED.
SQL> insert into emp values(&empno,'&ename','&job', '&mgr', &sal,&deptno);
ENTER VALUE FOR EMPNO: 100
ENTER VALUE FOR ENAME: LAKSHMI
ENTER VALUE FOR JOB : CLERK
ENTER VALUE FOR MGR : 104
ENTER VALUE FOR SAL : 1500
ENTER VALUE FOR DEPTNO: 10
1 ROW CREATED.
SQL> select *from emp;
EMPNO ENAME JOB MGR SAL DEPTNO
---------- -------------- ------------ ------------- --------- ----------
100 LAKSHMI CLERK 104 1500 10
101 RAVI SALESMAN 103 5000 20
102 RAMESH ANALYST 100 4000 10
103 MADHURI CLERK 101 3000 20
104 MBABU SALESMAN 101 3200 20
OUTPUT
EQUI JOIN
SQL> select empno,ename,emp.deptno,dept.deptno from emp,dept
where emp.deptno=dept.deptno;
EMPNO ENAME DEPTNO DEPTNO
---------- --------------- ------------- ----------
100 LAKSHMI 10 10
101 RAVI 20 20
102 RAMESH 10 10
103 MADHURI 20 20
104 MBABU 20 20
NON-EQUI JOIN
SQL> select empno,ename,emp.deptno,dept.deptno from emp,dept
where emp.deptno<>dept.deptno;
EMPNO ENAME DEPTNO DEPTNO
---------- ------------------------ ---------- ----------------
101 RAVI 20 10
103 MADHURI 20 10
104 MBABU 20 10
100 LAKSHMI 10 20
102 RAMESH 10 20
100 LAKSHMI 10 30
101 RAVI 20 30
102 RAMESH 10 30
103 MADHURI 20 30
104 MBABU 20 30

OUTER JOIN
SQL> select empno,ename,dept.deptno,dname from emp,dept where
emp.deptno(+)=dept.deptno;
EMPNO ENAME DEPTNO DNAME
---------- --------------- ---------- --------------------
100 LAKSHMI 10 ACCOUNTS
102 RAMESH 10 ACCOUNTS
101 RAVI 20 COMPUTERS
103 MADHURI 20 COMPUTERS
104 MBABU 20 COMPUTERS
30 SALES
SELF JOIN
SQL> select a.ename "employee", b.ename "manager" from emp a, emp b
where a.mgr=b.empno;
EMPLOYEE MANAGER
-------------------- --------------
LAKSHMI MBABU
RAVI MADHURI
RAMESH LAKSHMI
MADHURI RAVI
MBABU RAVI
MULTIPLICATION TABLE
Aim:- To write a PL/SQL program to generate Multiplication table
Algorithm:-
1. Start
2. Declare the variable n
3. Read a number n
4. i1
5. if(i<=10) then
5.1. Print n “*” I “=” n*i
5.2. ii+1
5.3. Goto Step 5
6. Stop
PL/SQL PROGRAM
declare
n number(5);
begin
n:=&n;
for i in 1..10
loop
dbms_output.put_line(n||' * '||i||' = '|| n*i) ;
end loop;
end;

INPUT & OUTPUT


Enter value for n: 4
4*1=4
4*2=8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40

BIGGEST NUMBER

Aim:- To write a PL/SQL program to find the biggest no among 3 numbers


Algorithm:-
1. Start
2. Declare variables a,b,c and big
3. Read 3 number a,b,c
4. if(a>b and a>c) then
4.1 big a
5. elsif (b>c and b>a) then
5.1 bigc
6. else then
6.1. bigc
7. Print “THE BIGGEST NUMBER IS=”
8. Print big
9. Stop
PL/SQL PROGRAM
declare
a number(5);
b number(5);
c number(5);
big number(5);
begin
a:=&a;
b:=&b;
c:=&c;
if a>b and a>c then
big:=a;
elsif b>c then
big:=b;
else
big:=c;
end if;
dbms_output.put_line('THE BIGGEST NUMBER IS='||big);
end;
INPUT & OUTPUT
ENTER VALUE FOR A: 20
ENTER VALUE FOR B: 35
ENTER VALUE FOR C: 40
THE BIGGEST NUMBER IS=40

SWAPPING NUMBER

Aim:- To write a PL/SQL program for swapping of two numbers


Algorithm:-
1. Start
2. Declare variables num1,num2 ,temp
3. Read 2 numbers num1,num2
4. temp=num1
5. num1=num2
6. num2=temp
7. print num1,num2
8. Stop
PL/SQL PROGRAM

declare
num1 number;
num2 number;
temp number;
begin
num1:=&num1;
num2:=&num2;

-- print result before swapping


dbms_output.put_line('Before Swap');
dbms_output.put_line('num1 = '|| num1 ||' num2 = '|| num2);

-- swapping of numbers num1 and num2


temp := num1;
num1 := num2;
num2 := temp;

-- print result after swapping


dbms_output.put_line('After Swap');
dbms_output.put_line('num1 = '|| num1 ||' num2 = '|| num2);

end;
INPUT & OUTPUT

Enter value for num1: 15


Enter value for num2: 20

Before Swap
num1 = 15 num2 = 20

After Swap
num1 = 20 num2 = 15
PRIME NUMBER

Aim:- To write a PL/SQL program to find given number is prime number or not
Algorithm:-
1. Start
2. Declare variables n,i ,temp
3. Read n
4. For i=2 to n/2
5. If n mod i=0
5.1 temp=0
6. If temp=1
6.1 print Given number is prime
7. else
7.1 print Given number is not prime
8. Stop

PL/SQL PROGRAM

--Prime Number Or Not


declare
n number;
i number:=2;
temp number:=1;

begin
n := &n;
for i in 2..n/2
loop
if mod(n, i) = 0 then
temp := 0;
exit;
end if;
end loop;
if temp = 1 then
dbms_output.put_line('GIVEN NUMBER IS PRIME NUMBER');
else
dbms_output.put_line('GIVEN NUMBER IS NOT PRIME NUMBER');
end if;
end;
INPUT & OUTPUT

Enter value for n: 45

GIVEN NUMBER IS NOT PRIME NUMBER


SUM OF EVEN AND ODD NUMBERS

Aim:- To write a PL/SQL program to display sum of even and sum of odd numbers
in the given range.
Algorithm:-
1. Start
2. Declare variables n, s1, s2
3. Read a number n
4. s10
5. s20
6. i1
7. if (i<=n) then
7.1. if (mod (i,2)=0) then
7.1.1. s1s1+i
7.2. else then
7.2.1. s2s2+i
7.3. ii+1
7.4. goto step 7
8. print “SUM OF EVEN NUMBERS=”
9. print s1
10. print “SUM OF ODD NUMBERS=”
11. print s2
12. stop

PL/SQL PROGRAM:-
declare
n number(5);
s1 number(5):=0;
s2 number(5):=0;
begin
n:=&n;
for i in 1..n
loop
if(mod(i,2) = 0) then
s1:=s1+i;
else
s2:=s2+i;
end if;
end loop;
dbms_output.put_line('SUM OF EVEN NUMBERS='||s1);
dbms_output.put_line('SUM OF ODD NUMBERS='||s2);
end;
INPUT & OUTPUT

ENTER VALUE FOR N: 5

SUM OF EVEN NUMBERS=6

SUM OF ODD NUMBERS=9


STRONG NUMBER
Aim:- To write a PL/SQL program to check the given number is strong or not
Algorithm:-
1. Start
2. Declare variables n,m,rem,f,s
3. Read a number n
4. mn
5. s0
6. if (n!=0) then
6.1 f1
6.2 remmod (n,10)
6.3 i1
6.4 if (i<=rem) then
6.4.1 ff*i
6.4.2 ii+1
6.4.3 goto step 6.4
6.5 n  n / 10
6.6 s  s + f
6.7 goto step 6
7. if (s=m) then
7.1 print m “IS A STRONG NUMBER”
8. else then
8.1 print m “IS NOT A STRONG NUMBER”
9. Stop
PL/SQL PROGRAM
declare
n number(5);
m number(5);
rem number(5);
f number(5);
s number(5):=0;
begin
n:=&n;
s:=0;
m:=n;
while(n!=0)
loop
f:=1;
rem:=mod(n,10);
for i in 1..rem
loop
f:=f*i;
end loop;
s:=s+f;
n:=floor(n/10);
end loop;
if (m=s) then
dbms_output.put_line(m||' IS A STRONG NUMBER');
else
dbms_output.put_line(m||' IS NOT A STRONG NUMBER');
end if;
end;
INPUT & OUTPUT

ENTER VALUE FOR N: 145

145 IS A STRONG NUMBER


ARMSTRONG NUMBER

Aim:- To write a PL/SQL program to check the given number is strong or not
Algorithm:-
1. Start
2. Declare n,rem,m,s
3. Read a number n
4. mn
5. s0
6. if (n != 0) then
6.1 rem  mod (n,10)
6.2 s  s+rem*rem*rem
6.3 n  n/10
6.4 goto step 6
7. if (m=s) then
7.1 print m “IS ARMSTRONG NO”
8. else then
8.1 print m “IS NOT ARMSTRONG NO”
9. Stop
PL/SQL PROGRAM:-
declare
n number(5);
rem number(5);
m number(5);
s number(5):=0;
begin
n:=&n;
m:=n;
while(n!=0)
loop
rem:=mod(n,10);
s:=s+rem*rem*rem;
n:=floor(n/10);
end loop;
if (m=s) then
dbms_output.put_line(m||'IS ARMSTRONG NO');
else
dbms_output.put_line(m||'IS NOT ARMSTRONG NO');
end if;
end;
INPUT & OUTPUT

ENTER VALUE FOR N: 407

407 IS ARMSTRONG NO
NUMBER PALINDROME

Aim:- To write a PL/SQL program to check the given number is palindrome or not

Algorithm:-

1. Start
2. Declare n, rem, m, rev
3. Read a number n
4. mn
5. rev0
6. if (n!=0) then
6.1 remmod(n,10)
6.2 revrevv*10+rem
6.3 n<-n/10
6.4 goto step 6
7. if(m=rev) then
7.1 print m “IS PALINDROME NO”
8. else then
8.1 print m “IS NOT PALINDROME NO”
9. Stop

PL/SQL PROGRAM

declare
n number(5);
rem number(5);
m number(5);
rev number(5):=0;
begin
n:=&n;
m:=n;
while(n!=0)
loop
rem:=mod(n,10);
rev:=rev*10+rem;
n:=floor(n/10);
end loop;
if (m=rev) then
dbms_output.put_line(m||' is palindrome no');
else
dbms_output.put_line(m||' is not palindrome no');
end if;
end;
INPUT & OUTPUT

ENTER VALUE FOR N: 555

555 IS PALINDROME NO
FACTORIAL NUMBER

Aim:- To write a PL/SQL program for factorial number of a given number

Algorithm:-

1. Start
2. Declare n, fact=1
3. Read a number n
4. While n>0
4.1 fact=n*fact
4.2 n=n-1
5. print fact
6. Stop

PL/SQL PROGRAM
declare
fact number :=1;
n number := &n;
begin
while n > 0 loop
fact:=n*fact;
n:=n-1;
end loop;
dbms_output.put_line(fact);
end;
INPUT & OUTPUT
Enter value for n: 5

120
FIBONACCI SERIES
Aim:- To write a PL/SQL program to generate Fibonacci series upto n
Algorithm:-
1. Start
2. Declare n, f1, f2, temp
3. read a number n
4. f10
5. f21
6. print “FIBONACCI SERIES ARE”
7. print f1, f2
8. i3
9. if (i<=n) then
9.1 temp f1+f2
9.2 print temp
9.3 f1f2
9.4 f2 temp
9.5 goto step 9
10. Stop

PL/SQL PROGRAM
declare
n number(5);
f1 number(5):=0;
f2 number(5):=1;
temp number(5);
begin
n:=&n;
dbms_output.put_line('fibonacci series are');
dbms_output.put_line(f1);
dbms_output.put_line(f2);
for i in 3..n
loop
temp:=f1+f2;
dbms_output.put_line(temp);
f1:=f2;
f2:=temp;
end loop;
end;
INPUT & OUTPUT
ENTER VALUE FOR N: 5
FIBONACCI SERIES ARE 0
1
1
2
3
PROCEDURE CREATION

Aim:- To create a procedure to perform the following string operations


i) Find the Length of strings
ii) Concatenate two Strings
Algorithm for Procedure:-
1. Start
2. Declare len, res
3. len length(s1)
4. res s1||s2
5. print “LENGTH OF STRING=” len
6. pring “CONCATENATION=” res
7. Return
Algorithm:-
1. Start
2. Declare str1, str2
3. Read str1m str2
4. Call procedure string op (str1, str2)
5. Stop
PROCEDURE
Create or replace procedure stringop (s1 varchar2, s2 varchar2) is
len number(10);
res varchar2(40);
Begin
len:=length(s1);
res:=s1||s2;
dbms_output.put_line('length of string1= '||len);
dbms_output.put_line('concatenation= '||res);
End;
PL/SQL PROGRAM
Declare
Str1 varchar2(20);
Str2 varchar2(20);
Begin
Str1:='&str1';
Str2:='&str2';
Stringop(str1,str2);
End;
INPUT & OUTPUT

ENTER VALUE FOR STR1: VENU

ENTER VALUE FOR STR2: KUMAR

LENGTH OF STRING1= 4

CONCATENATION= VENUKUMAR
EMPLOYEE PAY SLIP

Aim:- To write a PL/SQL program to calculate Employee pay slip with the following
conditions
i) HRA is 20%of Basic Salary
ii) DA is 10% of Basic Salary
iii) IT is 5% of Basic Salary
iv) Gsal = BSal + HRA + DA
v) Nsal = Gsal – IT
Table Structure:-
Table Structure:- EMPLOYE
Field Name Data type
Empno Number(10)
Ename Varchar2(20)
Bsal Number(10)
Hra Number(10)
Da Number(10)
It Number(10)
Gsal Number(10)
Nsal Number(10)

Algorithm:-

1. Start
2. Repeat Step3 to 8 until end of the file
3. Hra Bsal * 20/100
4. Da Bsal * 10/100
5. It  Bsal * 5/100
6. Gsal  Bsal + Hra + Da
7. Nsal  Gsal – It
8. Update Hra, Da, It, Gsal, Nsal in the Employe table

TABLE CREATION
SQL> CREATE TABLE EMPLOYE(EMPNO NUMBER(10) PRIMARY KEY,
ENAME VARCHAR2(20), BSAL NUMBER(10), HRA NUMBER(10),
DA NUMBER(10), IT NUMBER(10), GSAL NUMBER(10), NSAL NUMBER(10));

SQL> INSERT INTO EMPLOYE(EMPNO,ENAME,BSAL)VALUES(100,'VEERA',8000)


PL/SQL PROGRAM
declare
cursor c1 is select * from employe;
rec employe%rowtype;
begin
open c1;
loop
fetch c1 into rec;
exit when c1%notfound;
rec.hra:=rec.bsal*0.2;
rec.da:=rec.bsal*0.1;
rec.it:=rec.bsal*0.05;
rec.gsal:=rec.bsal+rec.hra+rec.da;
rec.nsal:=rec.gsal-rec.it;
update employe set hra=rec.hra, da=rec.da,
it=rec.it,gsal=rec.gsal,nsal=rec.nsal
where empno=rec.empno;
commit;
end loop;
close c1;
end;
INPUT:-

SQL> SELECT *FROM EMPLOYE;

EMPNO ENAME BSAL HRA DA IT GSAL NSAL


-------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
100 VEERA 8000
101 ANIL 5000
102 RAVI 4000
103 SUNEETHA 6000
104 SRINIVAS 15000

OUTPUT:-

SQL> SELECT *FROM EMPLOYE;

EMPNO ENAME BSAL HRA DA IT GSAL


-------- ---------- ---------- -------- ---------- -------------------
100 VEERA 8000 1600 800 400 10400
101 ANIL 5000 1000 500 250 6500
102 RAVI 4000 800 400 200 5200
103 SUNEETHA 6000 1200 600 300 7800
104 SRINIVAS 15000 3000 1500 750 19500
ELECTRICITY BILL PREPARATION
Aim:- To write a PL/SQL program to generate Electricity bill using cursor with the
following table

Table Structure:-
Table Structure:- ELECT
Field Name Data type
Mno Number(10)
Cname Varchar(10)
Cur_read Number(5)
Prev_read Number(5)
Amount Number(10,3)
Sar_tax Number(5)
Net_amt Number(6)
Conditions:-
i) If No units are more then 300 then unit rate is Rs. 5.50
ii) If No units are more then 200 then unit rate is Rs. 4.75
iii) If No units are more then 100 then unit rate is Rs. 3.05
iv) If No units are more then 50 then unit rate is Rs. 2.80
v) If No units are less then 50 then unit rate is Rs. 1.45

Algorithm:-

1. Start
2. Repeat step3 to step 12 until the end of loop
3. No_units  Cur_read – Prev_read
4. If No_units > 300 then
4.1 Rate  5.50
5. Else if No_units >200 then
5.1 Rate  4.75
6. Else if No_units >100 then
6.1 Rate  3.05
7. Else if No_units > 50 then
7.1 Rate  2.80
8. Else
8.1 Rate  1.45
9. Amount  No_units * rate
10. Sar_tax  20
11. Net_amount  Amount + Sar_tax
12. Update No_units, Amount, Sar_tax, Net_amount in the Elect table
13. Stop

TABLE CREATION
SQL> CREATE TABLE ELECT(MNO NUMBER(10),CNAME VARCHAR(10),
CUR_READ NUMBER(5),PREV_READ NUMBER(5), NO_UNITS NUMBER(5),
AMOUNT NUMBER(10,3),SAR_TAX NUMBER(5),NET_AMT NUMBER(6));
TABLE CREATED.

SQL> INSERT INTO ELECT (MNO,CNAME,CUR_READ,PREV_READ) VALUES


(1000, 'ANUSHA',100,40);
PL/SQL PROGRAM
Declare
Cursor c1 is select * from elect;
Rec elect%rowtype;
Rate number(4,2);
Begin
Open c1;
Loop
Fetch c1 into rec;
Exit when c1%notfound;
Rec.no_units:=rec.cur_read-rec.prev_read;
if rec.no_units>300 then
rate:=5.50;
elsif rec.no_units>200 then
rate:=4.75;
elsif rec.no_units>100 then
rate:=3.05;
elsif rec.no_units>50 then
rate:=2.80;
else
rate:=1.45;
end if;
rec.amount:=rec.no_units*rate;
Rec.sar_tax:=20;
Rec.net_amt:=rec.amount+rec.sar_tax;
Update elect set no_units=rec.no_units,
Amount=rec.amount,sar_tax=rec.sar_tax,
Net_amt= rec.net_amt where mno=rec.mno;
commit;
end loop;
close c1;
end;
INPUT:-
SQL> SELECT *FROM ELECT;
MNO CNAME CUR_READ PREV_READ NO_UNITS AMOUNT SAR_TAX NET_AMT
------- ------------- ------------- ---------- ---------- ---------- ---------- ----------
1000 ANUSHA 100 40
1001 SUJANA 140 78
1002 SYAM 200 150
1003 SNEHA 250 100
1004 SRINU 500 270

OUTPUT:-

SQL> SELECT *FROM ELECT;


MNO CNAME CUR_READ PREV_READ NO_UNITS AMOUNT SAR_TAX NET_AMT
---------- ---------- ---------- ---------- ---------- ---------- ---------- -------------- --------------
1000 ANUSHA 100 40 60 168 20 188
1001 SUJANA 140 78 62 173.6 20 194
1002 SYAM 200 150 50 72.5 20 93
1003 SNEHA 250 100 150 457.5 20 478
1004 SRINU 500 270 230 1092.5 20 1113
STUDENT MARKS LIST
Aim:- To write a PL/SQL program to generate student mark list using cursors with
the following table
Table Structure:-
Table Structure:- STUDENT
Field Name Data type
Sno Number(10)
Sname Varchar2(20)
M1 Number(5)
M2 Number(5)
M3 Number(5)
Tot Number(5)
Result Varchar2(10)
Grade Varchar2(20)

Algorithm:-
1. Start
2. Repeat step3 to step 9 until the end of loop
3. Tot  m1+m2+m3
4. A tot/3
5. if (m1>=35 and m2>=35 and m3>=35) then
5.1 Result  “PASS”
6. Else
6.1 Result  “FAIL”
7. if result = “PASS” then
7.1 if A>=60 then
7.1.1 Grade  “FIRST CLASS”
7.2 If A>=50 then
7.2.1 Grade  “SECOND CLASS”
7.3 Else then
7.3.1 Grade  “THIRD CLASS”
8. else then
8.1 Grade  “ *********** ”
9. Update tot, result, grade in the student table
10. Stop

TABLE CREATION

SQL> CREATE TABLE STUDENT(SNO NUMBER(10),SNAME VARCHAR2(20),


M1 NUMBER(5),M2 NUMBER(5), M3 NUMBER(5), TOT NUMBER(5),
RESULT VARCHAR2(10), GRADE VARCHAR2(20));
TABLE CREATED.
SQL> INSERT INTO STUDENT (SNO,SNAME,M1,M2,M3) VALUES
(308180, 'KRISHNA',90,80,70);
PL/SQL PROGRAM
Declare
cursor c1 is select * from student;
rec student%rowtype;
A number(6,2);
Begin
Open c1;
Loop
Fetch c1 into rec;
Exit when c1%notfound;
Rec.tot:=rec.m1+rec.m2+rec.m3;
A:=rec.tot/3;
If rec.m1>=35 and rec.m2>=35 and rec.m3>=35 then
rec.result:='pass';
Else
rec.result:='fail';
End if;
If rec.result='pass' then
if a>=60 then
Rec.grade:='first class';
Elsif a>=50 then
Rec.grade:='second class';
Else
Rec.grade:='third class';
end if;
Else
rec.grade:='***';
End if;
Update student set tot=rec.tot,result=rec.result,
grade=rec.grade where sno=rec.sno;
Commit;
End loop;
Close c1;
End;
INPUT:-
SQL>SELECT * FROM STUDENT;
SNO SNAME M1 M2 M3 TOT RESULT GRADE
----- -------- ------ ------ ----- ------ -------- -------
308180 KRISHNA 90 80 70
308181 ANUSHA 80 45 67
308182 RADHA 36 43 35
308183 SRAVANI 98 76 67
308184 SUSMITHA 87 54 34
308185 KAVYA 40 36 55
OUTPUT:-
SQL>SELECT * FROM STUDENT;

SNO SNAME M1 M2 M3 TOT RESULT GRADE


----- -------- ------ ------ ----- ------ -------- -------
308180 KRISHNA 90 80 70 240 PASS FIRST CLASS
308181 ANUSHA 80 45 67 192 PASS FIRST CLASS
308182 RADHA 36 43 35 114 PASS THIRD CLASS
308183 SRAVANI 98 76 67 241 PASS FIRST CLASS
308184 SUSMITHA 87 54 34 175 FAIL ***
308185 KAVYA 40 36 55 131 PASS THIRD CLASS
AUDIT DETAILS
Aim:- To Create a procedure to raise the Employee salary by 10% for department
number 30 and also maintain the raised details into another table “AUDITS” using
Triggers.
Table Structure:-
Table Structure:- EMP
Field Name Data type
Empno Number(10)
Empname Varchar2(10)
Job Number(5)
Sal Number(5)
Deptno Number(5)

Table Structure:-AUDITS
Field Name Data type
Empno Number(10)
Empname Varchar2(10)
Prev_sal Number(10)
Cur_sal Number(10)

Procedure:-
Table Creation 1:- EMP
SQL> CREATE TABLE EMP(EMPNO NUMBER(10),
EMPNAME VARCHAR2(10), JOB NUMBER(5), SAL NUMBER(5),
DEPTNO NUMBER(5));
TABLE CREATED.

Enter 5 records into the table by using insert command is as follows


SQL> Insert into emp values(‘&empno’, ‘&empname’, ‘&job’, ‘&sal’, ‘&sal’, ‘&deptno’);

Table Creation 2:- AUDITS


SQL> CREATE TABLE AUDITS MPNO NUMBER(10),
EMPNAME VARCHAR2(10), PREV_SAL NUMBER(10),
CUR_SAL NUMBER(10));
TABLE CREATED.
TRIGGER:-
CREATE OR REPLACE TRIGGER EMP_TRIG AFTER UPDATE ON EMP FOR EACH ROW
BEGIN
IF UPDATING THEN
INSERT INTO AUDITS VALUES(:OLD.EMPNO, :OLD.EMPNAME, :OLD.SAL, :NEW. SAL);
END IF;
END;
Algorithm:
1. Start
2. Create a trigger “Emp_Trig” on emp table
3. If updating then
3.1 Insert empno,empname, salary and incremented salary in audits table
4. Stop
INPUT:-

SQL>SELECT * FROM EMP;

EMPNO EMPNAME JOB SAL DEPTNO


---------- ----------- ------------- --------- ----------
100 RAVI CLERK 8000 10
101 ANIL MANAGER 10000 30
102 RAVI SALESMAN 5000 30
103 KAMAKSHI CLERK 7000 30
104 RAMESH ANALYST 6500 10
105 SUMANA SALESMAN 5600 30

SQL>UPDATE EMP1 SET SAL=SAL+(SAL*0.1) WHERE DEPTNO=30;

OUTPUT:-

SQL> SELECT * FROM AUDITS;

EMPNO EMPNAME PREV_SAL CUR_SAL


---------- ----------- ------------- ---------
101 ANIL 10000 11000
102 RAVI 5000 5500
103 KAMAKSHI 7000 7700
105 SUMANA 5600 6160

TRIGGER CREATION
Aim:- To Create a trigger on Student table so that the date should not be
manipulated on the Sundays

Table Structure:-
Table Structure:- STU
Field Name Data type
Sno Number(5)
Sname Varchar2(20)
Address Varchar 2(20)
Fee Number(10,2)
Procedure:-

SQL> CREATE TABLE STU(SNO NUMBER(5),SNAME VARCHAR2(10), ADDRESS


VARCHAR2(20) FEE NUMBER(10,2));

Enter 5 records into the table by using insert command is as follows


SQL> Insert into stu values(‘&sno’, ‘&sname’, ‘&address’, ‘&fee’);

Algorithm:
1. Start
2. Create a trigger “Sun_Trig” on Stu table
3. Declare a
4. a To_CHAR(SYSDATE, ‘DY’)
5. If a= ‘SUN’ then
5.1 Print “SORRY..! TRANSACTIONS ON SUNDAY IS NOT VALID ”
6. Stop
TRIGGER:-

CREATE OR REPLACE TRIGGER SUN_TRIG AFTER INSERT OR DELETE OR UPDATE ON


STU FOR EACH ROW
DECLARE
A VARCHAR2(15);
BEGIN
A:=TO_CHAR(SYSDATE,'DY');
IF A='SUN' THEN
RAISE_APPLICATION_ERROR(-20001,'SORRY..! TRANSACTIONS ON
SUNDAY IS NOT VALID');
END IF;
END;
INPUT:-

SQL> SELECT * FROM STU;

SNO SNAME ADDRESS FEE


---------- ----------- ------------- ----------
100 ANIL BUCHI 7000
101 RAVI NELLORE 8000
102 SRINIVAS SANGAM 7500
103 RAMESH REBALA 6000

ON SUNDAY INPUT:-

SQL> UPDATE STU SET ADDRESS=’NELLORE’ WHERE SNO=101;

OUTPUT:-

ORA-20001: SORRY..! TRANSACTIONS ON SUNDAY IS NOT VALID

Prepared by

Venukumar DVH
M.Sc.,M.Ed.,M.Tech(CSE)
Asst.Professor., Dept.of CSE., Geethanjali Institute of Science and Technology : Gangavaram : Nellore

Dear !

 To fight without a weapon is to step forward with determination! There is time! If you think
miracles happen, your life is dark! Although there is light around you, you see darkness

Submitted By
DVH VENUKUMAR
Asst.Professor
Department of CSE., GIST

You might also like