You are on page 1of 18

APEX INSTITUTE OF TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Database Management System (CST-227)


Faculty: Mr. Pramod Vishwakarma (E9758)

FUNCTIONS DISCOVER . LEARN . EMPOWER


1
DBMS: Course Objectives
COURSE OBJECTIVES
The Course aims to:
• Understand database system concepts and design databases for different applications
and to acquire the knowledge on DBMS and RDBMS.
• Implement and understand different types of DDL, DML and DCL statements.
• Understand transaction concepts related to databases and recovery/backup
techniques required for the proper storage of data.

By: Pramod Vishwakarma (E9758) 2


COURSE OUTCOMES

On completion of this course, the students shall be able to:-


Understand the database concept, system architecture and role of database
CO1
administrator
CO2 Design database for an organization using relational model
Apply relational algebra and relational calculus to query the database of
CO3
organization
CO4 Implement the package, procedures and triggers
CO5 Understand the concept of transaction processing and concurrency  control

By: Pramod Vishwakarma (E9758) 3


Types of Subprogram
PL/SQL provides two kinds of subprograms −
• Procedures − These subprograms do not return a value directly;
mainly used to perform an action.

• Functions − These subprograms return a single value; mainly


used tot compute and return a value..
Functions
• A function is same as a procedure except that it returns a value.
• Therefore, all the discussions of the previous chapter are true for
functions too.
Creating a Function
• A standalone function is created using the CREATE
FUNCTION statement.
• The simplified syntax for the CREATE OR REPLACE
PROCEDURE statement is as follows −
CREATE [OR REPLACE] FUNCTION function_name
[(parameter_name [IN | OUT | IN OUT] datatype [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];
Explanations
• function-name specifies the name of the function.
• [OR REPLACE] option allows the modification of an existing function.
• The optional parameter list contains name, mode and types of the
parameters. IN represents the value that will be passed from outside and
OUT represents the parameter that will be used to return a value outside of
the procedure.
• The function must contain a return statement.
• The RETURN clause specifies the data type you are going to return from the
function.
• function-body contains the executable part.
• The AS keyword is used instead of the IS keyword for creating a standalone
function.
Example:
• The following example illustrates how to create and call a standalone
function. This function returns the total number of EMPLOYEESS in the
EMP table.
CREATE OR REPLACE FUNCTION totalEmp
RETURN number IS
tot number(2) := 0;
BEGIN
SELECT count(*) into tot
FROM emp;

RETURN tot;
END;
/
Calling a Function
• To call a function, you simply need to pass the required parameters
along with the function name and if the function returns a value, then
you can store the returned value.
• Following program calls the function totalEmp from an anonymous
block −
DECLARE
c number(2);
BEGIN
c := totalEmp();
dbms_output.put_line('Total no. of Employees: ' || c);
END;
/
Example-2
DECLARE

a number; b number; c number;

FUNCTION findMax(x IN number, y IN number) RETURN number IS

z number;

BEGIN

IF x > y THEN z:= x;

ELSE Z:= y;

END IF;

RETURN z;

END;

BEGIN

a:= 23; b:= 45;

c := findMax(a, b); dbms_output.put_line(' Maximum of (23,45): ' || c);

END;

/
Recursive Functions
• We have seen that a program or subprogram may call another subprogram.
• When a subprogram calls itself, it is referred to as a recursive call and the
process is known as recursion.

• To illustrate the concept, let us calculate the factorial of a number. Factorial


of a number n is defined as −

n! = n*(n-1)!
= n*(n-1)*(n-2)!
...
= n*(n-1)*(n-2)*(n-3)... 1
Example Prg:-
DECLARE
n number;
f number;
FUNCTION fact(x number) RETURN number IS --definition of function
f number;
BEGIN
IF x=0 THEN f := 1;
ELSE f := x * fact(x-1);
END IF;
RETURN f;
END; --end of function
BEGIN
n:= 6;
f := fact(n); --calling the functions
dbms_output.put_line(' Factorial '|| n || ' is ' || f);
END; /
Advantages of Stored Procedure/Functions
Following are some advantages of stored procedure and function in
PL/SQL:
1. Improves Database Performance
• Compilation is automatically done by oracle engine.
• Whenever the calling of procedure or function is done ,the oracle engine
loads the compiled code into a memory area called System Global
Area(SGA) due to which execution becomes faster.
2. Provides Reusability and avoids redundancy
• The same block of code for procedure or function can be called any
number of times for working on multiple data.
• Due to which number of lines of code cannot be written repeatedly.
Advantages of Stored Procedure/Functions
3. Maintains Integrity
• Integrity means accuracy. Use of procedure or function ensures integrity
because they are stored as database objects by the oracle engine.
4. Maintains Security
• Use of stored procedure or function helps in maintaining the security of
the database as access to them and their usage can be controlled by
granting access/permission to users while the permission to change or to
edit or to manipulate the database may not be granted to users.
5. Saves Memory
• Stored procedure or function have shared memory. Due to which it saves
memory as a single copy of either a procedure or a function can be loaded
for execution by any number of users who have access permission.
References
• RamezElmasri and Shamkant B. Navathe, “Fundamentals of Database System”, The
Benjamin / Cummings Publishing Co.
• Korth and Silberschatz Abraham, “Database System Concepts”, McGraw Hall.
• C.J.Date, “An Introduction to Database Systems”, Addison Wesley.
• Thomas M. Connolly, Carolyn & E. Begg, “Database Systems: A Practical Approach
to Design, Implementation and Management”, 5/E, University of Paisley, Addison-
Wesley.

By: Pramod Vishwakarma (E9758) 16


References
• https://
docs.oracle.com/cd/B19306_01/server.102/b14200/statements_6
009.htm

17
THANK YOU

For queries
Email: pramod.e9758@cuimail.in
18

You might also like