SlideShare a Scribd company logo
1 of 21
Database Management System Lab(CS 29006)
KALINGA INSTITUTE OF INDUSTRIAL
TECHNOLOGY
School of Computer Engineering
1 Credit
Strictly for internal circulation (within KIIT) and reference only. Not for outside circulation without permission
Course Committee
Lab Contents
School of Computer Engineering
Sr
#
Major and Detailed Coverage Area Lab#
1 PL/SQL Programming Language
 Procedure
 Function
9
2
Subprogram
School of Computer Engineering
3
A subprogram is a program unit/module that performs a particular task.
These subprograms are combined to form larger programs. This is basically
called the 'Modular design'. A subprogram can be invoked by another
subprogram or program which is called the calling program.
PL/SQL provides two kinds of subprograms:
 Functions: these subprograms return a single value, mainly used to
compute and return a value.
 Procedures: these subprograms do not return a value directly, mainly used
to perform an action.
Procedures and Functions are saved in the database as database objects.
Parts of a PL/SQL Subprogram
Like anonymous PL/SQL blocks and, the named blocks a subprograms will also
have following three parts:
 Declarative Part
 Executable Part
 Exception-handling Part
Terminologies in PL/SQL Subprograms
School of Computer Engineering
4
Parameter:
The parameter is variable or placeholder of any valid PL/SQL datatype through
which the PL/SQL subprogram exchange the values with the main code. This
parameter allows to give input to the subprograms and to extract from these
subprograms.
 These parameters should be defined along with the subprograms at the
time of creation.
 These parameters are included in the calling statement of these
subprograms to interact the values with the subprograms.
 The datatype of the parameter in the subprogram and in the calling
statement should be same.
 The size of the datatype should not mention at the time of parameter
declaration, as the size is dynamic for this type.
Based on their purpose parameters are classified as: IN, OUT and IN OUT
Terminologies cont…
School of Computer Engineering
5
IN:
 This parameter is used for giving input to the subprograms.
 It is a read-only variable inside the subprograms, their values cannot be
changed inside the subprogram.
 In the calling statement these parameters can be a variable or a literal value
or an expression, for example, it could be the arithmetic expression like
'5*8' or 'a/b' where 'a' and 'b' are variables.
 By default, the parameters are of IN type.
OUT:
 This parameter is used for getting output from the subprograms.
 It is a read-write variable inside the subprograms, their values can be
changed inside the subprograms.
 In the calling statement, these parameters should always be a variable to
hold the value from the current subprograms.
Terminologies cont…
School of Computer Engineering
6
IN OUT:
 This parameter is used for both giving input and for getting output from the
subprograms.
 It is a read-write variable inside the subprograms, their values can be changed
inside the subprograms.
 In the calling statement, these parameters should always be a variable to hold the
value from the subprograms.
RETURN:
RETURN is the keyword that actually switch the control from the subprogram to the
calling statement. In subprogram RETURN simply means that the control needs to exit
from the subprogram. Once the RETURN keyword is encountered in the subprogram, the
code after this will be skipped. Normally, parent or main block will call the subprograms,
and then the control will shift from those parent block to the called subprograms.
RETURN in the subprogram will return the control back to their parent block. In the case
of functions RETURN statement also returns the value. The datatype of this value is
always mentioned at the time of function declaration. The datatype can be of any valid
PL/SQL data type.
Procedure
School of Computer Engineering
7
Syntax:
CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
EXCEPTION
< exception handling part >
END;
Explanation:
 CREATE PROCEDURE is to create a new procedure. Keyword 'OR REPLACE' instructs is to replace
the existing procedure (if any) with the current one.
 Procedure name should be unique.
 The optional parameter list contains name, mode and types of the parameters.
 Keyword 'IS' will be used, when the procedure is nested into some other blocks. If the procedure
is standalone then 'AS' will be used.
 Procedure-body contains the executable part.
 Exception handling part contains the exception handling part.
Standalone Procedure Example
School of Computer Engineering
8
CREATE OR REPLACE PROCEDURE Greetings
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/
Executing the Standalone Procedure
A standalone procedure can be called in two ways:
1. Using the EXECUTE keyword as shown below
EXECUTE Greetings;
2. Calling the name of the procedure from a PL/SQL block as shown below
BEGIN
Greetings;
END;
/
Deleting Procedure
School of Computer Engineering
9
A standalone procedure is deleted with the DROP PROCEDURE statement.
Syntax for deleting a procedure is: DROP PROCEDURE procedure-name;
So you can drop greetings procedure by using the following statement: DROP
PROCEDURE Greetings;
IN & OUT mode example
DECLARE
a,b,c number;
PROCEDURE findMin(x IN number, y IN number,
z OUT number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
/*continuation of program */
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
/
Nested Procedure Example cont…
School of Computer Engineering
10
DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS
BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a);
END;
/
Methods for Passing Parameters
School of Computer Engineering
11
Assume there is an procedure findMin(a, b, c, d). Actual parameters could be
passed in three ways:
 Positional notation: the first actual parameter is substituted for the first
formal parameter; the second actual parameter is substituted for the
second formal parameter, and so on e.g. call the procedure as: findMin(m, n,
o, p);
 Named notation: the actual parameter is associated with the formal
parameter using the arrow symbol ( => ). So the procedure call would look
like: findMin(a=>m, b=>n, c=>o, d=>p);
 Mixed notation: In mixed notation, you can mix both notations in
procedure call; however, the positional notation should precede the named
notation.
This call is legal: findMin(m, n, o, d=>p);
But this is not legal: findMin(a=>x, b, c, d);
Function
School of Computer Engineering
12
A PL/SQL 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 Function Syntax:
CREATE [OR REPLACE] FUNCTION function_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
EXCEPTION
< exception handling part >
END;
Standalone Function Example
School of Computer Engineering
13
CREATE OR REPLACE FUNCTION GetTotalCustomers
RETURN number
IS
total number(2) := 0;
BEGIN
SELECT count(*) into total FROM customer;
RETURN total;
END;
/
View structure with data
Calling a Function
School of Computer Engineering
14
To call a function you simply need to pass the required parameters along with
function name and if function returns a value then you can store returned value.
Following program calls the function GetTotalCustomers from an anonymous
block:
DECLARE
c number(2);
BEGIN
c := GetTotalCustomers();
dbms_output.put_line('Total no. of Customers: ' || c);
END;
/
Nested Function Example
School of Computer Engineering
15
DECLARE
a,b,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;
/* continuation of program */
BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c);
END;
/
PL/SQL Recursive Function
School of Computer Engineering
16
When a subprogram calls itself, it is referred to as a recursive call and the process is
known as recursion. The following program calculates the factorial of a given number by
calling itself recursively:
DECLARE
num number;
factorial number;
FUNCTION Fact(x number)
RETURN number
IS
f number;
BEGIN
IF x=0 THEN
f := 1;
ELSE
f := x * Fact(x-1);
END IF;
RETURN f;
END;
/* continuation of program */
BEGIN
num:= 6;
factorial := Fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' || factorial);
END;
/
Deleting Function
School of Computer Engineering
17
Once you have created your function, you might find that you need to remove it
from the database.
Syntax: DROP FUNCTION function_name;
Example: DROP FUNCTION GetTotalCustomers;
DROP FUNCTION Fact;
Difference between Function and Procedure
School of Computer Engineering
18
Procedure Function
Used mainly to execute certain process Used mainly to perform some
calculation
Use OUT parameter to return the value Use RETURN to return the value
It is not mandatory to return the value It is mandatory to return the value
RETURN will simply exit the control
from subprogram.
RETURN will exit the control from
subprogram and also returns the
value
Return datatype is not specified at the
time of creation
Return datatype is mandatory at the
time of creation
School of Computer Engineering
19
Thank You
End of Lab 9
Assignment
School of Computer Engineering
20
Reference Tables
 EMPLOYEE table with the attributes:
ID, LAST_NAME, FIRST_NAME, MIDDLE_NAME, FATHER_NAME,
MOTHER_NAME, SEX, HIRE_DATE, ADDRESS, CITY, STATE, ZIP, PHONE,
PAGER, SUPERVISOR_ID, DOB, INJECTED_DATE
 SCHOOL table with the attributes:
ID, NAME, INJECTED_DATE
 EMPLOYEE _ALIGNMENT table with the attributes:
EMPLOYEE_ID, SCHOOL_ID, INJECTED_DATE
 JOB table with the attributes:
ID, NAME, TITLE, SALARY, BONUS , INJECTED_DATE
 EMPLOYEE _PAY table with the attributes:
EMPLOYEE_ID, JOB_ID, INJECTED_DATE
Experiment
School of Computer Engineering
21
1. Create a PL/SQL procedure that outputs the message “I am a PL/SQL expert.”
2. Create a PL/SQL function to find out if a year is a leap year.
3. Create a PL/SQL procedure that takes employee ID as the input and displays the
employee full name with name of the school.
4. Create a PL/SQL function that takes school ID as the input. If the school does not
contain school name, return a false, otherwise return a true value. Print the
appropriate message in the calling program based on the result.
5. Create a PL/SQL function to revise the salary by 20%, who works in the same school
in which “John Smith” works? Print the appropriate message in the calling program
based on the result.
6. Create a PL/SQL procedure that will present the analysis of the employee.
a. Birthdays of number of employees per each month
b. Ratio of male to female
c. Number of hires per each month
d. Number of employees working for each school
e. Number of employees with different job titles working for each school

More Related Content

Similar to 9. DBMS Experiment Laboratory PresentationPPT

Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college2017eee0459
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxvekariyakashyap
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointerskirthika jeyenth
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptxAshwini Raut
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...vekariyakashyap
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfTeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdfTeshaleSiyum
 

Similar to 9. DBMS Experiment Laboratory PresentationPPT (20)

Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Functions in c
Functions in cFunctions in c
Functions in c
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointers
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
 
Functions
FunctionsFunctions
Functions
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
C function
C functionC function
C function
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
Function
FunctionFunction
Function
 

Recently uploaded

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 

Recently uploaded (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 

9. DBMS Experiment Laboratory PresentationPPT

  • 1. Database Management System Lab(CS 29006) KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY School of Computer Engineering 1 Credit Strictly for internal circulation (within KIIT) and reference only. Not for outside circulation without permission Course Committee
  • 2. Lab Contents School of Computer Engineering Sr # Major and Detailed Coverage Area Lab# 1 PL/SQL Programming Language  Procedure  Function 9 2
  • 3. Subprogram School of Computer Engineering 3 A subprogram is a program unit/module that performs a particular task. These subprograms are combined to form larger programs. This is basically called the 'Modular design'. A subprogram can be invoked by another subprogram or program which is called the calling program. PL/SQL provides two kinds of subprograms:  Functions: these subprograms return a single value, mainly used to compute and return a value.  Procedures: these subprograms do not return a value directly, mainly used to perform an action. Procedures and Functions are saved in the database as database objects. Parts of a PL/SQL Subprogram Like anonymous PL/SQL blocks and, the named blocks a subprograms will also have following three parts:  Declarative Part  Executable Part  Exception-handling Part
  • 4. Terminologies in PL/SQL Subprograms School of Computer Engineering 4 Parameter: The parameter is variable or placeholder of any valid PL/SQL datatype through which the PL/SQL subprogram exchange the values with the main code. This parameter allows to give input to the subprograms and to extract from these subprograms.  These parameters should be defined along with the subprograms at the time of creation.  These parameters are included in the calling statement of these subprograms to interact the values with the subprograms.  The datatype of the parameter in the subprogram and in the calling statement should be same.  The size of the datatype should not mention at the time of parameter declaration, as the size is dynamic for this type. Based on their purpose parameters are classified as: IN, OUT and IN OUT
  • 5. Terminologies cont… School of Computer Engineering 5 IN:  This parameter is used for giving input to the subprograms.  It is a read-only variable inside the subprograms, their values cannot be changed inside the subprogram.  In the calling statement these parameters can be a variable or a literal value or an expression, for example, it could be the arithmetic expression like '5*8' or 'a/b' where 'a' and 'b' are variables.  By default, the parameters are of IN type. OUT:  This parameter is used for getting output from the subprograms.  It is a read-write variable inside the subprograms, their values can be changed inside the subprograms.  In the calling statement, these parameters should always be a variable to hold the value from the current subprograms.
  • 6. Terminologies cont… School of Computer Engineering 6 IN OUT:  This parameter is used for both giving input and for getting output from the subprograms.  It is a read-write variable inside the subprograms, their values can be changed inside the subprograms.  In the calling statement, these parameters should always be a variable to hold the value from the subprograms. RETURN: RETURN is the keyword that actually switch the control from the subprogram to the calling statement. In subprogram RETURN simply means that the control needs to exit from the subprogram. Once the RETURN keyword is encountered in the subprogram, the code after this will be skipped. Normally, parent or main block will call the subprograms, and then the control will shift from those parent block to the called subprograms. RETURN in the subprogram will return the control back to their parent block. In the case of functions RETURN statement also returns the value. The datatype of this value is always mentioned at the time of function declaration. The datatype can be of any valid PL/SQL data type.
  • 7. Procedure School of Computer Engineering 7 Syntax: CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > EXCEPTION < exception handling part > END; Explanation:  CREATE PROCEDURE is to create a new procedure. Keyword 'OR REPLACE' instructs is to replace the existing procedure (if any) with the current one.  Procedure name should be unique.  The optional parameter list contains name, mode and types of the parameters.  Keyword 'IS' will be used, when the procedure is nested into some other blocks. If the procedure is standalone then 'AS' will be used.  Procedure-body contains the executable part.  Exception handling part contains the exception handling part.
  • 8. Standalone Procedure Example School of Computer Engineering 8 CREATE OR REPLACE PROCEDURE Greetings AS BEGIN dbms_output.put_line('Hello World!'); END; / Executing the Standalone Procedure A standalone procedure can be called in two ways: 1. Using the EXECUTE keyword as shown below EXECUTE Greetings; 2. Calling the name of the procedure from a PL/SQL block as shown below BEGIN Greetings; END; /
  • 9. Deleting Procedure School of Computer Engineering 9 A standalone procedure is deleted with the DROP PROCEDURE statement. Syntax for deleting a procedure is: DROP PROCEDURE procedure-name; So you can drop greetings procedure by using the following statement: DROP PROCEDURE Greetings; IN & OUT mode example DECLARE a,b,c number; PROCEDURE findMin(x IN number, y IN number, z OUT number) IS BEGIN IF x < y THEN z:= x; ELSE z:= y; END IF; END; /*continuation of program */ BEGIN a:= 23; b:= 45; findMin(a, b, c); dbms_output.put_line(' Minimum of (23, 45) : ' || c); END; /
  • 10. Nested Procedure Example cont… School of Computer Engineering 10 DECLARE a number; PROCEDURE squareNum(x IN OUT number) IS BEGIN x := x * x; END; BEGIN a:= 23; squareNum(a); dbms_output.put_line(' Square of (23): ' || a); END; /
  • 11. Methods for Passing Parameters School of Computer Engineering 11 Assume there is an procedure findMin(a, b, c, d). Actual parameters could be passed in three ways:  Positional notation: the first actual parameter is substituted for the first formal parameter; the second actual parameter is substituted for the second formal parameter, and so on e.g. call the procedure as: findMin(m, n, o, p);  Named notation: the actual parameter is associated with the formal parameter using the arrow symbol ( => ). So the procedure call would look like: findMin(a=>m, b=>n, c=>o, d=>p);  Mixed notation: In mixed notation, you can mix both notations in procedure call; however, the positional notation should precede the named notation. This call is legal: findMin(m, n, o, d=>p); But this is not legal: findMin(a=>x, b, c, d);
  • 12. Function School of Computer Engineering 12 A PL/SQL 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 Function Syntax: CREATE [OR REPLACE] FUNCTION function_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] RETURN return_datatype {IS | AS} BEGIN < function_body > EXCEPTION < exception handling part > END;
  • 13. Standalone Function Example School of Computer Engineering 13 CREATE OR REPLACE FUNCTION GetTotalCustomers RETURN number IS total number(2) := 0; BEGIN SELECT count(*) into total FROM customer; RETURN total; END; / View structure with data
  • 14. Calling a Function School of Computer Engineering 14 To call a function you simply need to pass the required parameters along with function name and if function returns a value then you can store returned value. Following program calls the function GetTotalCustomers from an anonymous block: DECLARE c number(2); BEGIN c := GetTotalCustomers(); dbms_output.put_line('Total no. of Customers: ' || c); END; /
  • 15. Nested Function Example School of Computer Engineering 15 DECLARE a,b,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; /* continuation of program */ BEGIN a:= 23; b:= 45; c := findMax(a, b); dbms_output.put_line(' Maximum of (23,45): ' || c); END; /
  • 16. PL/SQL Recursive Function School of Computer Engineering 16 When a subprogram calls itself, it is referred to as a recursive call and the process is known as recursion. The following program calculates the factorial of a given number by calling itself recursively: DECLARE num number; factorial number; FUNCTION Fact(x number) RETURN number IS f number; BEGIN IF x=0 THEN f := 1; ELSE f := x * Fact(x-1); END IF; RETURN f; END; /* continuation of program */ BEGIN num:= 6; factorial := Fact(num); dbms_output.put_line(' Factorial '|| num || ' is ' || factorial); END; /
  • 17. Deleting Function School of Computer Engineering 17 Once you have created your function, you might find that you need to remove it from the database. Syntax: DROP FUNCTION function_name; Example: DROP FUNCTION GetTotalCustomers; DROP FUNCTION Fact;
  • 18. Difference between Function and Procedure School of Computer Engineering 18 Procedure Function Used mainly to execute certain process Used mainly to perform some calculation Use OUT parameter to return the value Use RETURN to return the value It is not mandatory to return the value It is mandatory to return the value RETURN will simply exit the control from subprogram. RETURN will exit the control from subprogram and also returns the value Return datatype is not specified at the time of creation Return datatype is mandatory at the time of creation
  • 19. School of Computer Engineering 19 Thank You End of Lab 9
  • 20. Assignment School of Computer Engineering 20 Reference Tables  EMPLOYEE table with the attributes: ID, LAST_NAME, FIRST_NAME, MIDDLE_NAME, FATHER_NAME, MOTHER_NAME, SEX, HIRE_DATE, ADDRESS, CITY, STATE, ZIP, PHONE, PAGER, SUPERVISOR_ID, DOB, INJECTED_DATE  SCHOOL table with the attributes: ID, NAME, INJECTED_DATE  EMPLOYEE _ALIGNMENT table with the attributes: EMPLOYEE_ID, SCHOOL_ID, INJECTED_DATE  JOB table with the attributes: ID, NAME, TITLE, SALARY, BONUS , INJECTED_DATE  EMPLOYEE _PAY table with the attributes: EMPLOYEE_ID, JOB_ID, INJECTED_DATE
  • 21. Experiment School of Computer Engineering 21 1. Create a PL/SQL procedure that outputs the message “I am a PL/SQL expert.” 2. Create a PL/SQL function to find out if a year is a leap year. 3. Create a PL/SQL procedure that takes employee ID as the input and displays the employee full name with name of the school. 4. Create a PL/SQL function that takes school ID as the input. If the school does not contain school name, return a false, otherwise return a true value. Print the appropriate message in the calling program based on the result. 5. Create a PL/SQL function to revise the salary by 20%, who works in the same school in which “John Smith” works? Print the appropriate message in the calling program based on the result. 6. Create a PL/SQL procedure that will present the analysis of the employee. a. Birthdays of number of employees per each month b. Ratio of male to female c. Number of hires per each month d. Number of employees working for each school e. Number of employees with different job titles working for each school